2014-01-05 221 views
1

在我的Java应用程序中,我有一个带有自签名证书/密钥的JKS密钥库。我需要加载它们并将它们转换为BouncyCastle类型。如何将JKS证书/密钥转换为BouncyCastle证书/密钥

我正在使用java.security.KeyStore加载证书/密钥,它给了我java.security.cert.Certificate和java.security.Key。

如何再转换这些到BouncyCastle的用途(org.bouncycastle.asn1.x509.Certificate等)的格式

如果我使用Security.addProvider(新BouncyCastleProvider());这会使KeyStore返回不同​​的类型吗?

BC是否拥有自己的KeyStore API(注意:密钥库是JKS/SUN格式)。

谢谢

回答

2

我想通了,这里是一些伪代码。

要转换证书:

byte data[] = java.security.cert.Certificate.getEncoded(); 
org.bouncycastle.asn1.x509.Certificate.getInstance(data); 

将密钥:

 byte data[] = java.securty.Key.getEncoded(); 
     if (isRSA) { 
     RSAPrivateKey rsa = RSAPrivateKey.getInstance(data); 
     return new RSAPrivateCrtKeyParameters(rsa.getModulus(), rsa.getPublicExponent(), 
      rsa.getPrivateExponent(), rsa.getPrime1(), rsa.getPrime2(), rsa.getExponent1(), 
      rsa.getExponent2(), rsa.getCoefficient()); 
     } else { 
     return PrivateKeyFactory.createKey(data); 
     }