How to encrypt and decrypt file in Android?

Use a CipherOutputStream or CipherInputStream with a Cipher and your FileOutputStream / FileInputStream. I would suggest something like Cipher. GetInstance("AES/CBC/PKCS5Padding"); to the constructor call.

If you want to use a password, please make sure you do use a good key derivation mechanism (look up password based encryption). For multiple files, use an NONCE for the IV (see wikipedia).

Use a CipherOutputStream or CipherInputStream with a Cipher and your FileOutputStream / FileInputStream. I would suggest something like Cipher. GetInstance("AES/CBC/PKCS5Padding"); to the constructor call.

If you want to use a password, please make sure you do use a good key derivation mechanism (look up password based encryption). For multiple files, use an NONCE for the IV (see wikipedia). Always specify the character encoding when encoding/decoding strings, or you'll be in trouble when the platform encoding differs from the previous one.In other words, don't use String.getBytes() but use String.

GetBytes(Charset. ForName("UTF-8")). To make it more secure, please add a secure checksum (MAC or HMAC) and don't forget to add the NONCE at the start of the calculation.

Here is a sample code how to Encrypt/Decrypt Strings. It is using AES to encrypt any plain text data with a master password. I've used it in my app and it's working fine.

Here is another link just to know whats happening with some parts in the code. Bit. Ly/c3TesZ – Rijndael Nov 25 '10 at 10:20 thanks for your reply.

We have done string encryption but that same is not working for files. If you have any idea just let us know. – Pratik Nov 25 '10 at 12:50 Just convert the files to a bytearray.

Also, you shouldn't use a random number as the key per that example (unless you plan to store it somewhere secure) because there is not guarantee that it will always be the same random number. – timothyjc Jul 30 at 12:14 Those are terrible examples, they do not even specify the full algorithm (and probably defaults to ECB, not what you want for files). Furthermore, it uses the platform encoding instead of a specified encoding for the String (.getBytes()).SetSeed() DOES NOT generate the same key each time, it adds the seed bytes to the random pool.To top it off, they use lingua far from the standard lingua, so you cannot look anything up ("clear" instead of "plain").

The guys that wrote those articles should be keelhauled at the minimum. – owlstead Nov 7 at 19:02.

To do the same for a file, You're just modifying a bitstream, not a String...there are more similarities than differences, figure it out.

2 This is a comment at best, and shouldn't have been posted as an answer. – meagar Dec 6 '10 at 15:54 This is an answer as it instructs the user how to procede - Pratik commented that they have string encryption working, so now all they have to do is learn to extend that to file data. Do people now expect to write their entire program by pasting in code from stack overflow?

– Chris Stratton Dec 6 '10 at 15:56.

Can check the following link which can be helpful exampledepot.com/egs/javax.crypto/DesFil....

– Beginner Feb 11 at 15:00 You need to add the decryption code on the server side , where the server will handle the request, i.e. When the server receives the request from the client application , the first thing it should do is it decrypt the response . The decryption code has already been mentioned.

– chiranjib Feb 11 at 15:07 Any example how to do this? As I don't know much about encrypting – Beginner Feb 11 at 15:10 I have provided the sample code for encryption - decryption. I don't have server coding experience , but I am aware of the fact that the server code can contain both the encryption & decryption code.

The reason being , when the server is sending any response back to the client application , it should also send that in an encrypted format & you should be able to decrypt the response using the decryption code. So ,on either case , the request - response should be on any encrypted format. – chiranjib Feb 11 at 15:14.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions