1.Create a Class AESHelper
Use these method with following parameters
public class AESHelper {
public static byte[] ivBytes = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
public static final String UTF8 = "UTF-8";
public final static String AES = "AES";
public static final String PKCS5 = "AES/CBC/PKCS5Padding";
/**
* --Function for AES Encryption with 32 bytes key---
* @param key
* @param str
* @return
*/
public static String AES_Encode(String key, String str){
byte[] textBytes;
String encodeStr = null;
Cipher cipher = null;
try {
textBytes = str.getBytes(UTF8);
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
SecretKeySpec newKey = new SecretKeySpec(key.getBytes(UTF8),AES);
cipher = Cipher.getInstance(PKCS5);
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
encodeStr = Base64.encodeToString(cipher.doFinal(textBytes), 0);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return encodeStr;
}
/**
* --Function for AES Decryption with 32 bytes key---
* @param key
* @param str
* @return
*/
public static String AES_Decode(String key, String str){
byte[] textBytes = Base64.decode(str, 0);
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
SecretKeySpec newKey;
String decodeStr = null;
try {
newKey = new SecretKeySpec(key.getBytes(UTF8), AES);
Cipher cipher = Cipher.getInstance(PKCS5);
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
decodeStr = new String(cipher.doFinal(textBytes), UTF8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return decodeStr;
}
}
2.Now use these in your MainActivity
public class MainActivity extends Activity {
public static final char[] KEY_ENCRYPTION = { '1', '2', '3', '4', '5','6','d','b','h','s','h','e','q','l','e','1','8', 's', 'o', 'e', 'n','p','d','s','p','d','s','n','n','p','e','1' };
String strtextsample = "Sample";
String strencrypted;
String strdecrypted;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String strKeyEncryption = String.valueOf(KEY_ENCRYPTION);
strencrypted= AESHelper.AES_Encode(strKeyEncryption, strtextsample );
System.out.println("Encrypted text--"+strencrypted);
strdecrypted= AESHelper.AES_Decode(strKeyEncryption, strencrypted);
System.out.println("Decrypted text--"+strdecrypted);
}
}
Now, are able to encrypt and decrypt a string in a android application.