开发者

loading a certificate from keystore

Load a certificate and keys from keystore which is password protected and then use it for cert verification and digita开发者_C百科l signing


To read the certificate is really trivial.

CertificateFactory factory = CertificateFactory.getInstance("X.509");  
X509Certificate certificate = (X509Certificate) factory.generateCertificate(new FileInputStream("file.pem"));  

This is with standard APIs (in try/catch) etc and you have loaded your certificate.
Now the toString method of certificate is not suitable for you since it just captures the "user's" view of the certificate e.g. you would use it for println for instance
Can't you send the certificate object itself?
Not sure what your server expects so you can look into the various methods of certificate
X509Certificate


I use this code

    PEMReader pr=new PEMReader(new StringReader(trust_certs));
    KeyStore trustStore  = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null, null);
    Object o;
    int i=0;
    while((o=pr.readObject())!=null){
        if(o instanceof X509Certificate){
            i++;
            X509Certificate c=(X509Certificate)o;
            trustStore.setCertificateEntry(Integer.toString(i), c);
        }
    }

http://www.bouncycastle.org/docs/docs1.6/org/bouncycastle/openssl/PEMReader.html


A pem file is read as any other text file. Read the Java tutorial about IO (and concentrate on character streams, since a pem file contains text, and on File IO, since this is what you want to do)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜