在 Android 4.4 设备上,已使用 spring-android-auth 1.0.1.RELEASE 模块中的 org.springframework.security.crypto.encrypt.AndroidEncryptors 类对字符串进行加密。那么例如...
// naturally, salt and password are normally different
String salt = "75f4c92894b2f3e7";
String password = "password";
org.springframework.security.crypto.encrypt.TextEncryptor encryptor = org.springframework.security.crypto.encrypt.AndroidEncryptors.text(password, salt);
String encryptedString = encryptor.encrypt("hello");
在一次运行期间,加密字符串解析为“1ee3c42c9b986d30cd88da37f29bc3b9e93e3defdb76a2b23 72a47276152e2bd”。
然后,该字符串被发布到托管在 tomcat 7 服务器上、在 JDK 1.6.0_32 上运行的 Spring Web 应用程序(请注意,已安装 JCE 无限强度管辖权策略文件)。然后,我尝试使用 spring-security-crypto 3.2.0.RELEASE 模块中的 org.springframework.security.crypto.encrypt.Encryptors 类解密该字符串...
// naturally, the salt and password values used here are the same as the ones used on the android device
String salt = "75f4c92894b2f3e7";
String password = "password";
org.springframework.security.crypto.encrypt.TextEncryptor encryptor = org.springframework.security.crypto.encrypt.Encryptors.text(password, salt);
String decryptedString = encryptor.decrypt("1ee3c42c9b986d30cd88da37f29bc3b9e93e3defdb76a2b2372a47276152e2bd");
不幸的是,当调用解密方法时,会引发以下异常......
java.lang.IllegalStateException: Unable to invoke Cipher due to bad padding
at org.springframework.security.crypto.encrypt.CipherUtils.doFinal(CipherUtils.java:125)
at org.springframework.security.crypto.encrypt.AesBytesEncryptor.decrypt(AesBytesEncryptor.java:75)
at org.springframework.security.crypto.encrypt.HexEncodingTextEncryptor.decrypt(HexEncodingTextEncryptor.java:40)
at local.encryption.Decryption.main(Decryption.java:18)
Caused by: javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)
at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)
at javax.crypto.Cipher.doFinal(DashoA13*..)
at org.springframework.security.crypto.encrypt.CipherUtils.doFinal(CipherUtils.java:121)
... 3 more
如果我在服务器上加密和解密字符串,则一切正常。这似乎表明 AndroidEncryptor 和 Encryptor 类没有使用相同的算法,尽管 API 表示它们都使用 256 位 AES 算法,并且它们都使用 PKCS #5 的 PBKDF2(基于密码的密钥导出函数)导出密钥#2)。
当我深入研究 AndroidEncryptor 类时,我发现它使用“PBEWITHSHA256AND256BITAES-CBC-BC”算法。然而,Encryptor 类使用“PBKDF2WithHmacSHA1”算法。
有人对前进的方向有什么建议吗?