How to access certificate from eToken in java

2019-04-01 15:23发布

问题:

I want to read certificate from eToken when it plugged-in, when I store that certificate on local machine I can read it through my java application but, I don't know how to read it from eToken.

RSAPublicKey pub;
            String fileName = "C:\\myCert.cer";

             InputStream inStream = new FileInputStream(fileName);
             CertificateFactory cf = CertificateFactory.getInstance("X.509");
             X509Certificate cert =
        (X509Certificate)cf.generateCertificate(inStream);
             inStream.close();

             pub = (RSAPublicKey) cert.getPublicKey();
             System.out.println(cert.getIssuerDN());
             System.out.println(cert.getSubjectDN());
             System.out.println(cert.getSubjectAlternativeNames());
             byte [] tempPub = pub.getEncoded();
             String sPub = new String( tempPub );

回答1:

One way to do this is by using the PKCS#11 provider. It comes with examples, too.