解密在Android设备上加密的字符串

在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"); 

在一次运行期间,encryptedString解析为“1ee3c42c9b986d30cd88da37f29bc3b9e93e3defdb76a2b23 72a47276152e2bd”。

然后将该字符串发布到运行在JDK 1.6.0_32上的tomcat 7服务器上的spring web应用程序(请注意JCE Unlimited Strength Jurisdiction策略文件已经安装)。 然后我尝试使用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”算法。 然而,加密器类使用“PBKDF2WithHmacSHA1”算法。

有没有人有任何建议,以前进的方向?

我正在使用Android

 compile group: 'org.springframework.security', name: 'spring-security-crypto', version: '3.1.0.RELEASE' 

在Spring-Backend我使用

 compile "org.springframework.security:spring-security-core:4.2.3.RELEASE" 

我的例子你看起来像:

 val password = "MY_PASSWORD" val salt = KeyGenerators.string().generateKey() val encryptor = Encryptors.text(password, salt) val textToEncrypt = "kotlin-rocks" val encryptedText = encryptor.encrypt(textToEncrypt) val decryptor = Encryptors.text(password, salt) val decryptedText = decryptor.decrypt(encryptedText) println("Salt: \"" + salt + "\"") println("Original text: \"" + textToEncrypt + "\"") println("Encrypted text: \"" + encryptedText + "\"") println("Decrypted text: \"" + decryptedText + "\"") 

如果你在Android上运行代码,你会得到如下的随机结果:

 Salt: "2578bfa1cb682f17" Original text: "kotlin-rocks" Encrypted text: "bdfdfff122accfadc0ad5449bb9c93ceedd2380b2e7f8cd19d2ce03daec4218e" Decrypted text: "kotlin-rocks" 

现在你必须把salt加密的文本发送到服务器。 服务器将使用这两个,现在可以解密密码。 密码的实现可以是相同的。