java - 安卓 BigInteger ArithmeticException

标签 java android rsa biginteger arithmeticexception

我正在尝试在 Android 应用程序中实现 RSA 算法。我正在使用 java.math.BigInteger.modPow() 函数进行加密/解密,它适用于我的计算机(Windows 和 Xubuntu)和我的 Raspberry Pi(也是 Debian)。 当在我的手机 (Android 4.4.4) 上执行相同的代码时,在第二次调用 modPow() 时抛出以下异常:

java.jang.ArithmeticException: error:0306B06B:bignum routines:BN_div:not initialized
    at java.math.NativeBN.BN_mod_exp(NativeMethod)
    at java.math.BigInt.modExp(BigInt.java:327)
    at java.math.BigInteger.modPow(BigInteger.java:997)
    at "where I call java.math.BigInteger.modPow()"

我检查了指数和模数:两者都是正数,因此文档并没有真正帮助。减小 key 的大小(指数和模数)也没有改变任何东西。不幸的是,我找不到 native 函数的源代码,也不知道会发生什么。

您是否知道为什么会抛出此异常或错误代码的含义?

最佳答案

因为它告诉你没有初始化,所以 BigInteger 的创建一定是不知何故失败了。

截至libcrypto :

The BIGNUM library generally lives in libcrypto, which comes with OpenSSL. Its API is defined in openssl/bn.h. This library exports the BIGNUM type. BIGNUM objects always need to be initialized before use, even if they're statically declared.

因此,请检查您是否可以从您的代码中初始化它或尝试使用较低的 api 版本,因为我对此并不深入。

还要检查 bundle 的库是否与您的 32/64 位平台架构相对应。

另一个猜测:如果 SSL 错误队列不为空,Android 4.4.4 在创建 BigIntegers 时可能存在 [bug] (code.google.com/p/android/issues/detail?id=77262),也许就是这样你遇到了。

关于java - 安卓 BigInteger ArithmeticException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26489195/

相关文章:

java - 在数据库中存储数组

java堆栈帧,可以通过字节码检查线程的所有堆栈帧

java - 使用正则表达式从使用 xeger java 库生成的字符串中解析 id

json - 如何使用 sha512 为 JWT 生成 RSA key ?

javascript - 如何在 Node.js 中获取 RSA 公钥的模数和指数

java - 如何创建 AES 加密的 PKCS #8 文件?

在事件监听器中启用 Java 菜单项

java - Android 不接收广播 UDP,但接收定向 UDP

android - 如何在 AOSP 源中将系统音量设置为最大

java - Android - 使联系人在手机上不可编辑