Android Cipher在Android 8.1中加密

时间:2017-12-14 07:20:43

标签: android encryption

我有这段代码来加密字符串输入。如果我在其他设备上使用相同的输入进行加密,那么我得到相同的值。但是,当我尝试8.1设备时,我得到一个完全不同的字符串。 没有抛出任何异常。我发现使用KeyGenerator:AES进行Android 8.1加密更新。如何修复与其他设备生成相同的值Os降低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;
}

2 个答案:

答案 0 :(得分:0)

尝试将UTF-16更改为UTF-16LE或UTF-16BE

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

您可以使用

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

检查Android 8.1.0上的密码输入是否与其他Android操作系统相同。

答案 1 :(得分:-1)

请更改此行并检查

来自

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

Cipher cipher = Cipher.getInstance("AES");