Android跨版本new String()或加密错误

Android cross version new String() or encryption error

我有这个测试代码:

byte[] aesPass1 = { (byte) 204, (byte) 204, (byte) 204, (byte) 204, (byte) 204};

encrypted = encryptAES256("Anything".getBytes("UTF8"), new String(aesPass1, "UTF8"), salt, 1000, iv_b);

加密一些数据,测试代码:

byte[] aesPass1 = { (byte) 204, (byte) 204, (byte) 204, (byte) 204, (byte) 204};

decryptAES256(encrypted, new String(aesPass1, "UTF8"), salt, 100, iv_b);

解密数据。

如果我在 Android 4.4.4 上加密,我可以在同一设备 (Android 4.4.4) 上解密,没有问题, 但如果我尝试在 Android 4.2.2 上解密,我会收到“pad 块已损坏”错误。

类似,如果我在 Android 4.2.2 上加密,我可以在同一设备 (Android 4.2.2) 上解密,没有问题, 但是在 Android 4.4.4 上我得到“error06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt”错误。

为了让这对我来说更加陌生,如果我使用这个代码:

encrypted = encryptAES256("Anything".getBytes("UTF8"), "pass", salt, 1000, iv_b);

加密,并且:

decryptAES256(encrypted, "pass", salt, 100, iv_b);

在 Android 4.2.2 和 Android 4.4.4.

上一切正常

我能看到的唯一区别是使用 new String(aesPass1, "UTF8") 函数。

我的加解密函数:

public static byte[] encryptAES256(byte[] data, String passphrase, byte[] salt, int iterations, byte[] ivbytes) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException  {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec specKey = new PBEKeySpec(passphrase.toCharArray(), salt, iterations, 256);
    SecretKey secretKey = factory.generateSecret(specKey);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    cipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(ivbytes));
    return cipher.doFinal(data);
}

public static byte[] decryptAES256(byte[] data, String passphrase, byte[] salt, int iterations, byte[] ivbytes) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException  {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec specKey = new PBEKeySpec(passphrase.toCharArray(), salt, iterations, 256);
    SecretKey secretKey = factory.generateSecret(specKey);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivbytes));
    return cipher.doFinal(data);
}

你能帮忙吗?

提前致谢!

如果在解密过程中使用了错误的 password/key,

BadPaddingException 是您应该期望的结果。

The exception that is thrown when a padding mechanism is expected for the input data, but the input data does not have the proper padding bytes.

我可以打赌你的代码有正确的密码,问题一定是关于 new String()hash 反对将其声明为不同设备的 "normalString"

IMO Android 4.4.4 设备必须使用较新版本的 Java (8) 其中 String 内存使用已更改,如您在 中所见,因此哈希是差异和密码校验和不匹配。

我还是不明白Android跨版本的问题,但是我找到了问题的根源。

byte[] aesPass1必须有可以被new String()转换为可读字符的值,所以:

byte[] aesPass1 = { (byte) 204, (byte) 204, (byte) 204, (byte) 204, (byte) 204 };

不起作用,但是:

byte[] aesPass1 = { (byte) 65, (byte) 33, (byte) 126, (byte) 77, (byte) 66 };

有效。