【问题标题】:RSA decrypt on android lollipop在安卓棒棒糖上进行 RSA 解密
【发布时间】:2015-08-07 02:48:46
【问题描述】:

我在使用 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:rsaroutines:RSA_EAY_PUBLIC_DECRYPT:data too large for modules

我的sdk目标是:

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> 适用于安卓 4.4

你知道是什么问题吗?

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

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

【问题讨论】:

  • 我在某处读到 SSL 初始化在某些 android 旧版本上不正确,并且已修复,所以这可能以某种方式相关吗?
  • @Yazan 我不知道。我发现可能的提供程序是不同的(Provider[] providers = Security.getProviders();),但应用程序始终使用“AndroidOpenSSL”。不同之处在于,对于 android 4.4,提供程序的大小是 127,对于 android 5,是 143。然后我有两个不同的密钥大小。你知道是否有一种方法可以在 android 5 上使用与 android 4.4 相同的 keyfactory?

标签: ssl rsa android-5.0-lollipop android-4.4-kitkat public-key


【解决方案1】:

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

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

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

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

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

【讨论】:

  • 非常感谢!这解决了问题! @Robert 但我不明白为什么在 android kitkat 上可以正常工作......无论如何谢谢!
  • 第一位为 1 的概率为 50%。也许在 KitKat 上,您有一个 RSA 密钥,其模数和指数的第一位设置为 0?在这种情况下,您的代码可以正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 2015-09-10
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 2014-12-15
相关资源
最近更新 更多