Java 使用CryptoRandomFactory类的getCryptoRandom方法时获取异常。ApacheCommons加密库

Java 使用CryptoRandomFactory类的getCryptoRandom方法时获取异常。ApacheCommons加密库,java,apache-commons,Java,Apache Commons,我正在尝试使用ApacheCommons加密库。我使用apache官方网站的示例代码来使用这个库的随机API。 以下是它的链接- 我在使用getCryptoRandom方法时遇到了这个异常 Exception in thread "main" java.security.GeneralSecurityException: CryptoRandom: [org.apache.commons.crypto.random.OpenSslCryptoRandom] failed wi

我正在尝试使用ApacheCommons加密库。我使用apache官方网站的示例代码来使用这个库的随机API。 以下是它的链接-

我在使用getCryptoRandom方法时遇到了这个异常

Exception in thread "main" java.security.GeneralSecurityException: CryptoRandom: [org.apache.commons.crypto.random.OpenSslCryptoRandom] failed with java.lang.reflect.InvocationTargetException
    at org.apache.commons.crypto.random.CryptoRandomFactory.getCryptoRandom(CryptoRandomFactory.java:217)
    at com.nix.apps.crypto.Main.main(Main.java:25)
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at org.apache.commons.crypto.utils.ReflectionUtils.newInstance(ReflectionUtils.java:90)
    at org.apache.commons.crypto.random.CryptoRandomFactory.getCryptoRandom(CryptoRandomFactory.java:198)
    ... 1 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.apache.commons.crypto.utils.ReflectionUtils.newInstance(ReflectionUtils.java:88)
    ... 2 more
Caused by: java.security.GeneralSecurityException: Native library could not be initialised
    at org.apache.commons.crypto.random.OpenSslCryptoRandom.<init>(OpenSslCryptoRandom.java:88)
    ... 7 more
Caused by: java.lang.UnsatisfiedLinkError: ENGINE_load_rdrand
    at org.apache.commons.crypto.random.OpenSslCryptoRandomNative.initSR(Native Method)
    at org.apache.commons.crypto.random.OpenSslCryptoRandom.<clinit>(OpenSslCryptoRandom.java:56)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.apache.commons.crypto.utils.ReflectionUtils.getClassByNameOrNull(ReflectionUtils.java:138)
    at org.apache.commons.crypto.utils.ReflectionUtils.getClassByName(ReflectionUtils.java:103)
    at org.apache.commons.crypto.random.CryptoRandomFactory.getCryptoRandom(CryptoRandomFactory.java:197)
    ... 1 more
线程“main”java.security.GeneralSecurityException中的异常:CryptoRandom:[org.apache.commons.crypto.random.OpenSslCryptoRandom]因java.lang.reflect.InvocationTargetException失败 在org.apache.commons.crypto.random.CryptoRandomFactory.getCryptoRandom(CryptoRandomFactory.java:217)上 位于com.nix.apps.crypto.Main.Main(Main.java:25) 原因:java.lang.RuntimeException:java.lang.reflect.InvocationTargetException 位于org.apache.commons.crypto.utils.ReflectionUtils.newInstance(ReflectionUtils.java:90) 在org.apache.commons.crypto.random.CryptoRandomFactory.getCryptoRandom(CryptoRandomFactory.java:198)上 ... 还有一个 原因:java.lang.reflect.InvocationTargetException 位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 位于sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 位于java.lang.reflect.Constructor.newInstance(Constructor.java:423) 位于org.apache.commons.crypto.utils.ReflectionUtils.newInstance(ReflectionUtils.java:88) ... 还有两个 原因:java.security.GeneralSecurityException:无法初始化本机库 位于org.apache.commons.crypto.random.OpenSslCryptoRandom.(OpenSslCryptoRandom.java:88) ... 还有7个 原因:java.lang.UnsatifiedLinkError:引擎负载 位于org.apache.commons.crypto.random.OpenSslCryptoRandomNative.initSR(本机方法) 位于org.apache.commons.crypto.random.OpenSslCryptoRandom.(OpenSslCryptoRandom.java:56) 位于java.lang.Class.forName0(本机方法) 位于java.lang.Class.forName(Class.java:348) 位于org.apache.commons.crypto.utils.ReflectionUtils.getClassByNameOrNull(ReflectionUtils.java:138) 位于org.apache.commons.crypto.utils.ReflectionUtils.getClassByName(ReflectionUtils.java:103) 在org.apache.commons.crypto.random.CryptoRandomFactory.getCryptoRandom(CryptoRandomFactory.java:197)上 ... 还有一个 进程已完成,退出代码为1

如何解决这个问题

据我所知,问题在于加载openSSL引擎或类似的东西:

Caused by: java.lang.UnsatisfiedLinkError: ENGINE_load_rdrand
    at org.apache.commons.crypto.random.OpenSslCryptoRandomNative.initSR(Native Method)
    at org.apache.commons.crypto.random.OpenSslCryptoRandom.<clinit>(OpenSslCryptoRandom.java:56)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.apache.commons.crypto.utils.ReflectionUtils.getClassByNameOrNull(ReflectionUtils.java:138)
    at org.apache.commons.crypto.utils.ReflectionUtils.getClassByName(ReflectionUtils.java:103)
    at org.apache.commons.crypto.random.CryptoRandomFactory.getCryptoRandom(CryptoRandomFactory.java:197)
由以下原因引起:java.lang.UnsatifiedLinkError:ENGINE\u load\r随机
位于org.apache.commons.crypto.random.OpenSslCryptoRandomNative.initSR(本机方法)
位于org.apache.commons.crypto.random.OpenSslCryptoRandom.(OpenSslCryptoRandom.java:56)
位于java.lang.Class.forName0(本机方法)
位于java.lang.Class.forName(Class.java:348)
位于org.apache.commons.crypto.utils.ReflectionUtils.getClassByNameOrNull(ReflectionUtils.java:138)
位于org.apache.commons.crypto.utils.ReflectionUtils.getClassByName(ReflectionUtils.java:103)
在org.apache.commons.crypto.random.CryptoRandomFactory.getCryptoRandom(CryptoRandomFactory.java:197)上

我收到了同样的错误。用法:PROPS.setProperty(CryptoRandomFactory.CLASSES_KEY,CryptoRandomFactory.RandomProvider.OPENSSL.getClassName());CryptoRandom=CryptoRandomFactory.getCryptoRandom(道具);我修复了我的问题PROPS.setProperty(CryptoRandomFactory.CLASSES_KEY,CryptoRandomFactory.RandomProvider.OPENSSL.getClassName());setProperty(CryptoCipherFactory.CLASSES_KEY,CryptoCipherFactory.CipherProvider.OPENSSL.getClassName());CryptoRandom=CryptoRandomFactory.getCryptoRandom(道具);工厂似乎不仅需要随机提供程序,还需要密码提供程序。