RSA在android棒棒糖上解密

inv*_*mer 7 ssl rsa public-key android-4.4-kitkat android-5.0-lollipop

使用RSA解密时出错.该代码适用于android 4.4 kit kat,但相同的应用程序不适用于Android 5.0棒棒糖.

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");
}
Run Code Online (Sandbox Code Playgroud)

错误是:java.security.SignatureException:错误:04067084:rsa例程:RSA_EAY_PUBLIC_DECRYPT:数据对于模数而言太大.

我的目标是:

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> 对于Android 4.4

你知道这是什么问题吗?

编辑: 我注意到我有两个不同长度的公钥!Android 5:我有382/383位(太小)Android 4.4:我有384位(确定)

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

Rob*_*ert 1

一个错误是您创建 RSAPublicKeySpec 的方式:

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

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

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

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