Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encrypt and decrypt a string in android

Tags:

android

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.

like image 364
Kikki Avatar asked Jun 23 '11 01:06

Kikki


People also ask

What is encryption and decryption in Android?

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.

How do I decrypt text messages on Android?

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.


2 Answers

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;
}
like image 166
Sileria Avatar answered Oct 20 '22 15:10

Sileria


The javax.crypto package does everything you need

http://developer.android.com/reference/javax/crypto/package-summary.html

like image 29
mhr Avatar answered Oct 20 '22 14:10

mhr