我正在试用来自 maths.uncommon.org 的 Dan Dyer 的 AESCounterRNG 对象并遇到异常。我正在使用在 Java 项目中运行良好的代码,并试图了解为什么我在 Android 中得到不同的结果。
每次我尝试使用提供的构造函数之一创建新的 AESCounterRNG 时都会抛出异常。异常错误信息为“java.security.InvalidKeyException: Only SecretKey is supported”。
我正在使用的代码(在 onCreate 方法中)如下。除了单个主要 Activity 和重写的 onCreate 和 onMenuCreate 方法外,没有其他代码。
byte[] seed = {-15, -67, -127, -15, -118, -45, -38, -5, 50, 49, 111, -29, 18, 81, -6, -104};
try {
AESCounterRNG rng = new AESCounterRNG(seed);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
maths.uncommon.org 上的 API 说:
AESCounterRNG
public AESCounterRNG(byte[] seed)
throws GeneralSecurityException
Creates an RNG and seeds it with the specified seed data.
Parameters:
seed - The seed data used to initialise the RNG.
Throws:
GeneralSecurityException - If there is a problem initialising the AES cipher.
谁能告诉我为什么我在 Android 和 Java 中得到不同的结果?我认为这是因为底层库。堆栈跟踪显示调用了 Android 的 OpenSSLCipher 以及 javax.crypto.Cipher:
05-08 13:51:33.993: W/System.err(1335): java.security.InvalidKeyException: Only SecretKey is supported
05-08 13:51:33.993: W/System.err(1335): at com.android.org.conscrypt.OpenSSLCipher.engineInitInternal(OpenSSLCipher.java:248)
05-08 13:51:33.993: W/System.err(1335): at com.android.org.conscrypt.OpenSSLCipher.engineInit(OpenSSLCipher.java:300)
05-08 13:51:33.993: W/System.err(1335): at javax.crypto.Cipher.init(Cipher.java:519)
05-08 13:51:33.993: W/System.err(1335): at javax.crypto.Cipher.init(Cipher.java:479)
05-08 13:51:33.993: W/System.err(1335): at org.uncommons.maths.random.AESCounterRNG.<init>(AESCounterRNG.java:114)
05-08 13:51:33.993: W/System.err(1335): at com.example.securerandomtest.MainActivity.onCreate(MainActivity.java:46)
最佳答案
我很高兴地说 Dan Dyer 给我发送了一个更新的非普通数学库,它现在在 Windows Java、Linux Java 和 Android Java 下是一致的。问题解决了。谢谢,丹。
关于java - 尝试在 Android 中使用 AESCounterRNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23544415/