我在使用RSA解密时遇到了错误。 这段代码在安卓4.4 KitKat上可以运行,但是同样的应用在安卓5.0 Lollipop上无法工作。
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(modulusBytes), new BigInteger(exponentBytes));
RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(pubKeySpec);
byte[] decrypted = null;
try {
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance("RSA/None/NoPadding");
// decrypt the text using the public key
cipher.init(Cipher.DECRYPT_MODE, publicKey);
decrypted = cipher.doFinal(area_fissa_byte);
} catch (Exception ex) {
ex.printStackTrace();
Log.d("error","error");
}
错误信息为:java.security.SignatureException: error:04067084:rsa routines:RSA_EAY_PUBLIC_DECRYPT:data too large for modulus。
我的SDK目标版本是:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
适用于Android 4.4。您知道问题出在哪里吗?
编辑: 我注意到我有两个不同长度的公钥!!! Android 5:我有382/383位(太小了) Android 4.4:我有384位(可以)
编辑2: 我发现在TLS/SSL方面,Android 5.0存在差异:https://developer.android.com/about/versions/android-5.0-changes.html 但我不知道如何解决这个问题。