我试图使用密钥库访问我的证书的私钥。在Java中访问硬件令牌的私钥
这里是我的代码:
package test.java.com;
import java.security.Key;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.Enumeration;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
KeyStore ks;
try {
ks = KeyStore.getInstance("Windows-MY");
ks.load(null, null);
Enumeration<String> en = ks.aliases();
while (en.hasMoreElements()) {
String aliasKey = (String) en.nextElement();
Certificate c = ks.getCertificate(aliasKey);
Key key = ks.getKey(aliasKey, "1753".toCharArray()); // Here I try to access the private key of my hardware certificate
byte[] bt = key.getEncoded();
if(bt != null) {
System.out.println("private key correctly accessed");
} else {
System.out.println("Can't access private key");
}
System.out.println("---> alias : " + aliasKey);
if (ks.isKeyEntry(aliasKey)) {
Certificate[] chain = ks.getCertificateChain(aliasKey);
System.out.println("---> chain length: " + chain.length);
X509Certificate Cert = null;
for (Certificate cert : chain) {
System.out.println(cert);
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
但是,当我试图让私钥(为了与它签约数据),私钥为空。
我做错了吗?如何使用其私钥对该证书签名数据?
注意:我必须使用密钥库(我不能使用硬件令牌驱动程序的DLL,因为我可能有许多不同的驱动程序来到服务器)。
我在Java代码中有空的密码。 当我第一次向USB插入令牌并运行Java应用程序时,会显示来自令牌中间件的弹出窗口,并且私钥是可访问的。 但是,当我第二次运行应用程序和更多次,应用程序冻结。 我必须再次移除并插入标记。 – 2016-11-04 08:22:43