sun.security.pkcs11.wrapper.PKCS11Exception:CKR_ATTRIBUTE_TYPE_INVALID

Posted

技术标签:

【中文标题】sun.security.pkcs11.wrapper.PKCS11Exception:CKR_ATTRIBUTE_TYPE_INVALID【英文标题】:sun.security.pkcs11.wrapper.PKCS11Exception: CKR_ATTRIBUTE_TYPE_INVALID 【发布时间】:2015-02-05 23:55:49 【问题描述】:

我是 SmartCard 的新手,需要一些帮助。我尝试运行一个测试程序(见下文),但不断收到此异常,sun.security.pkcs11.wrapper.PKCS11Exception: CKR_ATTRIBUTE_TYPE_INVALID

我的系统是Windows 7 64-bit,java版本是1.8.0_31

提前感谢您的帮助!

public static void main(String[] args) throws Exception  
    final StringBuilder buff = new StringBuilder(); 
    buff.append("name=").append("SmartCard").append("\n"); 
    buff.append("library=").append("C:\\Progra~1\\ActivIdentity\\ActivClient\\acpkcs211.dll").append("\n");
    buff.append("showInfo=true").append("\n"); 
    buff.append("slotListIndex=0").append("\n"); 

    final SunPKCS11 provider = new sun.security.pkcs11.SunPKCS11(new ByteArrayInputStream(buff.toString().getBytes())); 
    Security.addProvider(provider);  

    KeyStore.ProtectionParameter pp = new KeyStore.PasswordProtection("######".toCharArray());  
    KeyStore.Builder builder = KeyStore.Builder.newInstance("PKCS11", provider, pp);
    KeyStore ks = builder.getKeyStore(); 
    ks.load(null, null); 
 

输出:

Exception in thread "main" java.security.KeyStoreException: KeyStore instantiation failed
    at java.security.KeyStore$Builder$2.getKeyStore(Unknown Source)
    at myTests.P11Test.main(P11Test.java:38)
Caused by: java.io.IOException: load failed
    at sun.security.pkcs11.P11KeyStore.engineLoad(P11KeyStore.java:843)
    at java.security.KeyStore.load(Unknown Source)
    at java.security.KeyStore$Builder$2$1.run(Unknown Source)
    at java.security.KeyStore$Builder$2$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    ... 2 more
Caused by: sun.security.pkcs11.wrapper.PKCS11Exception: CKR_ATTRIBUTE_TYPE_INVALID
    at sun.security.pkcs11.wrapper.PKCS11.C_FindObjectsInit(Native Method)
    at sun.security.pkcs11.P11KeyStore.findObjects(P11KeyStore.java:2658)
    at sun.security.pkcs11.P11KeyStore.mapLabels(P11KeyStore.java:2396)
    at sun.security.pkcs11.P11KeyStore.engineLoad(P11KeyStore.java:835)
    ... 6 more

【问题讨论】:

【参考方案1】:

我相信 CKR_ATTRIBUTE_TYPE_INVALID 错误表明传递的属性之一(可能是 showInfo 或 slotListINdex)无效。尝试一一删除它们,看看它是否修复了错误。

【讨论】:

以上是关于sun.security.pkcs11.wrapper.PKCS11Exception:CKR_ATTRIBUTE_TYPE_INVALID的主要内容,如果未能解决你的问题,请参考以下文章

升级Java后无法找到Sun.security