0

I have this code to encrypt a string input. If i encrypt with same input on other devices os lower 8.1 then i get same value. However, when I try on the 8.1 devices, I get a completely different string. No exeption thrown. I found the Android 8.1 Cryptography updates with KeyGenerator:AES. How can i fix to generate a same value with other devices Os lower 8.1 ?

public static String cryptAESGungHo(String input)
{   
    byte[] gh_key = getKeyGungho("gh_key").getBytes();
    byte[] gh_iv  = getKeyGungho("gh_iv").getBytes();
    IvParameterSpec ivSpecs = new IvParameterSpec(gh_iv);
    byte[] crypted = null;
    try{
        SecretKeySpec skey = new SecretKeySpec(gh_key, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, skey, ivSpecs);
        try
        {
     crypted = cipher.doFinal(input.getBytes("UTF-16"));
        }
        catch(Exception e)
        {}
    }catch(Exception e){
    }
    String cr=  new String(crypted);
    return cr;
}
  • Are you using 128 or 256 bits to decrypt? After updating it to 1.1.3 and changing the implementation to encrypt it using 256 bits, I could decrypt it without any problem. It seems to be an issue in the library and not in the OS. – Dilip Dec 14 '17 at 07:26
  • I using 128 bits. I still can decrypt it. However, the string return is not same as the other OS – Đức Nguyễn Dec 14 '17 at 07:33
  • change it to 256 bit and try again and let me know what happen... – Dilip Dec 14 '17 at 07:35
  • For server reasons I can not switch to 256 bits. Is there any other way to solve it ? – Đức Nguyễn Dec 14 '17 at 07:52

2 Answers2

0

try changing UTF-16 to UTF-16LE or UTF-16BE

crypted = cipher.doFinal(input.getBytes("UTF-16LE"));

You can use

byte[] data = str.getBytes("UTF-16LE");
Log.i("api", Arrays.toString(data));

To Check the cipher input on Android 8.1.0 whether is the same as on other Android OS.

-1

please change this line and check

From

 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

To

Cipher cipher = Cipher.getInstance("AES");
Haresh Ramani
  • 390
  • 1
  • 16