Hi I want to encrypt a string in android inorder to store and later for showing I have to decrypt it. Is it possible to do md5 hashing or any other hashing in android. Please provide me an example.
Encryption is the process of encoding all user data on an Android device using symmetric encryption keys. Once a device is encrypted, all user-created data is automatically encrypted before committing it to disk and all reads automatically decrypt data before returning it to the calling process.
The only way to decode the data is with a secret key. This key is a number generated on yours and your recipient's phones, with a new one being created for each message. Once they are used to decrypt the message, they are deleted.
android.util.Base64
encoding is good enough if you want to store something e.g. in a share preferences file:
Here is what I do:
Storing:
public void saveSomeText (String text) {
SharedPreferences.Editor editor = prefs.edit();
if (Utils.isEmpty( text ))
text = null;
else
text = Base64.encodeToString( text.getBytes(), Base64.DEFAULT );
editor.putString( "some_text", text );
editor.commit();
}
Retrieval:
public String getSomeText () {
String text = prefs.getString( "some_text", null );
if (!Utils.isEmpty( passwd )) {
text = new String( Base64.decode( text, Base64.DEFAULT ) );
}
return text;
}
The javax.crypto package does everything you need
http://developer.android.com/reference/javax/crypto/package-summary.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With