【问题标题】:Trying to use AESCounterRNG in Android尝试在 Android 中使用 AESCounterRNG
【发布时间】:2014-05-08 14:16:46
【问题描述】:

我正在尝试使用来自 maths.uncommon.org 的 Dan Dyer 的 AESCounterRNG 对象并遇到异常。我正在使用在 Java 项目中运行良好的代码,并试图了解为什么我在 Android 中得到不同的结果。

每次我尝试使用提供的构造函数之一创建新的 AESCounterRNG 时都会引发异常。异常错误信息为“java.security.InvalidKeyException: Only SecretKey is supported”。

我正在使用的代码(在 onCreate 方法中)如下。除了单一的主要活动和重写的 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)

【问题讨论】:

    标签: java android exception random


    【解决方案1】:

    我很高兴地说,Dan Dyer 向我发送了一个更新的 uncommons-maths 库,它现在在 Windows Java、Linux Java 和 Android Java 下是一致的。问题解决了。谢谢,丹。

    【讨论】:

    • 没问题。为了澄清这一点,任何人都可以从 GitHub 获得修复。受影响的版本是 1.2.3,所以当我开始发布 1.2.4 时,它将包含修复。
    猜你喜欢
    • 2015-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多