How to get KeyStore from usb token in Java

前端 未结 2 1099
悲&欢浪女
悲&欢浪女 2021-02-06 15:20

I have a SafeNet 5100 eToken already with a valid certificate in it that I use to access a web application from my company that requires it (multi-factor authentication).

<
2条回答
  •  情歌与酒
    2021-02-06 15:31

    Try the below code to get the keystore from usb token using java

    class Test {
        public static void main(String args[]) throws IOException, GeneralSecurityException, DocumentException, CertificateVerificationException{
            
            // Create instance of SunPKCS11 provider
            String pkcs11Config = "name=eToken\nlibrary=C:\\Windows\\System32\\eps2003csp11.dll";
            java.io.ByteArrayInputStream pkcs11ConfigStream = new java.io.ByteArrayInputStream(pkcs11Config.getBytes());
            sun.security.pkcs11.SunPKCS11 providerPKCS11 = new sun.security.pkcs11.SunPKCS11(pkcs11ConfigStream);
            java.security.Security.addProvider(providerPKCS11);
    
            // Get provider KeyStore and login with PIN
            String pin = "12345678";
            java.security.KeyStore keyStore = java.security.KeyStore.getInstance("PKCS11", providerPKCS11);
            keyStore.load(null, pin.toCharArray());
    
            // Enumerate items (certificates and private keys) in th KeyStore
            java.util.Enumeration aliases = keyStore.aliases();
            String alias = null;
            while (aliases.hasMoreElements()) {
                alias = aliases.nextElement();
                System.out.println(alias);
            }
        }
    }
    

提交回复
热议问题