RSA解密android棒棒糖



我有一个错误与RSA解密。该代码适用于android 4.4 kit kat,但同样的应用程序不适用于android 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例程:RSA_EAY_PUBLIC_DECRYPT:data too large for modulus.

我的sdk目标是:

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> for android 4.4

你知道是什么问题吗?

编辑:

我注意到我有2个不同长度的公钥!!Android 5:我有382/383位(太小)Android 4.4: i have 384 bit (ok)

EDIT2: 我发现android 5.0在TLS/SSL方面存在差异:https://developer.android.com/about/versions/android-5.0-changes.html但我不知道如何解决这个问题。

一个错误是您如何创建RSAPublicKeySpec:

new RSAPublicKeySpec(new BigInteger(modulusBytes), new BigInteger(exponentBytes));

如果modulusBytes或exponentBytes的第一个位是1,则该数字将被解释为负值。

当您使用RSA数字和BigInteger时,始终使用构造函数BigInteger (int signum, byte[] magnitude)signum=1来指定数字是正的:

new RSAPublicKeySpec(new BigInteger(1, modulusBytes), new BigInteger(1, exponentBytes));

最新更新