我尝试使用 j2pkcs11.dll(与 jdk1.8.0 - 64 位一起打包)来访问存储在智能卡上的证书,但不能使其工作。
--- 动态添加 SunPKCS11 提供程序的示例代码 ---
String pkcs11ConfigSettings = "name = " + "TestSmartCard" + "\n" + "library = " + "C:/jdk1.8.0_11/jre/bin/j2pkcs11.dll";
byte[] pkcs11ConfigBytes = pkcs11ConfigSettings.getBytes();
ByteArrayInputStream confStream = new ByteArrayInputStream(pkcs11ConfigBytes);
Provider p = new sun.security.pkcs11.SunPKCS11(confStream);
----我得到的异常(exception)---
java.security.ProviderException: Initialization failed
at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:376)
at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:103)
at scpoc.SmartCard.main(SmartCard.java:28)
Caused by: java.io.IOException: The specified procedure could not be found.
at sun.security.pkcs11.wrapper.PKCS11.connect(Native Method)
at sun.security.pkcs11.wrapper.PKCS11.<init>(PKCS11.java:138)
at sun.security.pkcs11.wrapper.PKCS11.getInstance(PKCS11.java:151)
at sun.security.pkcs11.SunPKCS11.<init>(SunPKCS11.java:313)
JEP 131 声称在 Java 8 (http://openjdk.java.net/jeps/131) 中支持 PKCS11,但我无法让它在使用 java8 64 位的 Windows 7 上工作。注意:我还在 Windows 7 上尝试了 java 8 32 位 - 但也没有运气。
有没有人使用带有 java 8 (Windows 7) 的 SunPKCS11 提供程序取得任何成功?
最佳答案
SunPKCS11 提供程序仅存在于 JRE 的 32 位 Windows 版本中,直到 JRE7。从 JRE8 开始,它也出现在 64 位 Windows 版本的 JRE 中。这是您在 JEP131 中看到的信息.
如果您需要在早于 JRE8 的 64 位 Windows 版本的 JRE 中使用 PKCS#11 API,那么您将不得不使用替代的第 3 方实现之一 - 例如 IAIK-JCE .
我还在您的代码示例中注意到您试图直接使用“j2pkcs11.dll”作为 PKCS#11 库,这是错误的,因为它只是位于 JRE 和实现 PKCS#11 接口(interface)的库之间的 JNI 包装器。您需要加载智能卡或 HSM 供应商提供的 PKCS#11 库,而不是加载“j2pkcs11.dll”。
关于pkcs#11 - 在 Windows 7(64 位)上使用带有 java 8(64 位)的 j2pkcs11.dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28640506/