Heim > Fragen und Antworten > Hauptteil
插入USB-KEY, 想通过HttpClient来向服务器发送https请求。现在厂商已提供的USB-KEY的驱动程序PKCS#11接口,于是通过java程序访问USB-KEY。
// 厂商提供的UKEY型号名称
private static final String UKEY_PROVIDER = "SJK1217";
// UKEY驱动lib库文件
private static final String UKEY_LIB = "E:/cer/System32/SJK1217Pkcs.dll";
// UKEY的PIN码
private static final String UKEY_PIN = "1234";
private static String keystorePath = "E:/cer/hxx_sign.keystore";
private static String keystorePass = "12345678";
public static void main(String[] args) throws Exception {
String pkcs11config = "name = " + UKEY_PROVIDER + "\nlibrary = "+ UKEY_LIB + "\n";
byte[] pkcs11configbytes = pkcs11config.getBytes();
ByteArrayInputStream configStream = new ByteArrayInputStream(pkcs11configbytes);
SunPKCS11 ps = new sun.security.pkcs11.SunPKCS11(configStream);
System.out.println(ps.toString());
Security.addProvider(ps);
KeyStore.CallbackHandlerProtection pinHandler = new KeyStore.CallbackHandlerProtection(new TextCallbackHandler());
KeyStore ks = KeyStore.Builder.newInstance("PKCS11", ps, pinHandler)
.getKeyStore();
ks.load(null, UKEY_PIN.toCharArray());
printKeyStoreInfo(ks);
}
// 打印 KeyStore中的信息
public static void printKeyStoreInfo(KeyStore keystore) throws Exception {
System.out.println("Provider : " + keystore.getProvider().getName());
System.out.println("Type : " + keystore.getType());
System.out.println("Size : " + keystore.size());
Enumeration en = keystore.aliases();
while (en.hasMoreElements()) {
String alias = (String) en.nextElement();
System.out.println("Alias: " + alias);
X509Certificate cert = (X509Certificate) keystore
.getCertificate(alias);
System.out.println("Certificate: " + cert);
PrivateKey privateKey = (PrivateKey) keystore.getKey(alias, null);
System.out.println("Private key: " + privateKey);
}
}
打印的结果
SunPKCS11-SJK1217 version 1.8
Provider : SunPKCS11-SJK1217
Type : PKCS11
Size : 0
获取到的KeyStore 的size为0,是什么原因? 国内这方面的资料好难搜索得到,有这方面的大牛 能帮忙解答一下么?