Pet*_*y B 10 c# bouncycastle x509certificate private-key
通常,当我X509Certificate2从我的密钥库中取出时,我可以调用.PrivateKey以检索证书的私钥AsymmetricAlgorithm.但是我已经决定使用Bouncy Castle并且它的实例X509Certificate只有一个getPublicKey();我看不到从私有密钥中取出证书的方法.有任何想法吗?
我从我的Windows-MY密钥库中获取了X509Certificate2,然后使用:
//mycert is an X509Certificate2 retrieved from Windows-MY Keystore
X509CertificateParser certParser = new X509CertificateParser();
X509Certificate privateCertBouncy = certParser.ReadCertificate(mycert.GetRawCertData());
AsymmetricKeyParameter pubKey = privateCertBouncy.GetPublicKey();
//how do i now get the private key to make a keypair?
Run Code Online (Sandbox Code Playgroud)
无论如何将AsymmetricAlgorithm(C#私钥)转换为AsymmetricKeyParameter(bouncycastle私钥)?
maj*_*tor 26
Akp = Org.BouncyCastle.Security.DotNetUtilities.GetKeyPair(this.Certificate.PrivateKey).Private;
Run Code Online (Sandbox Code Playgroud)
Cri*_*GoT 19
不知道BouncyCastle那么多,但在我看来,简单的事情是根据关键参数重新创建密钥.
public static AsymmetricKeyParameter TransformRSAPrivateKey(AsymmetricAlgorithm privateKey)
{
RSACryptoServiceProvider prov = privateKey as RSACryptoServiceProvider;
RSAParameters parameters = prov.ExportParameters(true);
return new RsaPrivateCrtKeyParameters(
new BigInteger(1,parameters.Modulus),
new BigInteger(1,parameters.Exponent),
new BigInteger(1,parameters.D),
new BigInteger(1,parameters.P),
new BigInteger(1,parameters.Q),
new BigInteger(1,parameters.DP),
new BigInteger(1,parameters.DQ),
new BigInteger(1,parameters.InverseQ));
}
您可以使用调用代码
AsymmetricKeyParameter bouncyCastlePrivateKey = TransformRSAPrivateKey(mycert.PrivateKey);
显然,这假设证书包含RSA密钥,但是使用DSACryptoServiceProvider和可以为DSA实现相同的结果DSAParameters