Monday, 21 May 2018

android cipher aes-256-cbc

below code works

https://gist.github.com/dealforest/1949873

to run it then you can use below code:


String key = "12345678901234567890123456789012"; //32-length
byte[] keyBytes = key.getBytes("UTF-8");
Random randomno = new Random();
byte[] ivBytes = new byte[16];
randomno.nextBytes(ivBytes);
  String plainText; byte[] encryptedData;   //encryption: plainText = "xxx111xxx"; encryptedData = AES256Cipher.encrypt(ivBytes, keyBytes, plainText.getBytes("UTF-8"));
//decryption:
cipherData = AES256Cipher.decrypt(ivBytes, keyBytes, encryptedData); plainText = new String(cipherData, "UTF-8");

However, if you trying run this code on your android studio with junit, then you will find below error:

java.security.InvalidKeyException: Illegal key size or default parameters 

please check below link for detail:
http://baiduhix.blogspot.co.uk/2018/05/javasecurityinvalidkeyexception-illegal.html

No comments:

Post a comment