使用X.509证书.pfx文件私钥在C#中进行OpenPGP解密



朋友,

我们从证书颁发机构获得了.pfx和.cer证书。并将.cer(公钥(共享给我们的合作伙伴。

加密(Java(

我们的合作伙伴使用Java bouncy castle openpgp标准加密了消息(使用我们的公钥(,并分享了加密消息,如下所示,-----开始PGP消息-----版本:xv2hQEMAzFXJ94q1Nm8AQf/Tld0/3dAvgFKPQVBS8bmbXChXeApeReo1ydNS+。。。。。。-----结束PGP消息-----

解密:(C#(

我们需要使用.pfx文件对消息进行解密。

我浏览了以下文章,http://burnignorance.com/c-coding-tips/pgp-encryption-decryption-in-c/新的PGPKeyPair似乎正在生成并用于加密和解密。

但就我而言,我有.pfx文件我们如何从.pfx文件中提取用于解密的pgpprivate密钥?你能谈谈我们如何做到这一点吗。提前感谢您在这方面花费的所有时间。

2020年12月13日

我已经将X509Certificate.pfx导入到如下存储中,并试图转换pgpprivate密钥

string certPath = @"C:Userstest.pfx";
string certPass = "apples";
// Create a collection object and populate it using the PFX file
X509Certificate2Collection collection = new X509Certificate2Collection();
collection.Import(certPath, certPass, X509KeyStorageFlags.PersistKeySet);
X509Certificate2 certificate = collection[0];
AsymmetricAlgorithm x509PrivateKey = certificate.PrivateKey;
PgpPrivateKey pK = x509PrivateKey; //Here i am gettting the invalid conversion error.

我正在尝试使用X.509证书私钥作为PGPrivatekey进行解密。但是,在将私钥分配给pgpprivatekey时,得到了无效的强制转换异常。

有什么办法做到这一点吗?

谨致问候,斯大林

您可以尝试使用BouncyCastle API使用PKCS12类文件读取pfx文件,然后将密钥转换为PgpSecretKey。

阅读上的文档->pkcs12.获取密钥((和PgpSecretKey类。

public static void GetPriveKey(String pfxFile, String pfxPassword)
{
//Load PKCS12 file
Pkcs12Store pkcs12 = new Pkcs12Store(new FileStream(pfxFile, FileMode.Open, FileAccess.Read), pfxPassword.ToArray());
string keyAlias = null;
foreach (string name in pkcs12.Aliases)
{
if (pkcs12.IsKeyEntry(name))
{
keyAlias = name;
break;
}
}
//
AsymmetricKeyParameter Privatekey = pkcs12.GetKey(keyAlias).Key;
X509CertificateEntry[] ce = pkcs12.GetCertificateChain(keyAlias);
AsymmetricKeyParameter PublicKey= ce[0].Certificate.GetPublicKey();

PgpSecretKey mySecretKey = new PgpSecretKey(PgpSignature.DefaultCertification,
PublicKeyAlgorithmTag.RsaGeneral,
PublicKey,
Privatekey,
DateTime.UtcNow,
keyAlias,
SymmetricKeyAlgorithmTag.Cast5,
pfxPassword.ToCharArray(), 
true,
null, 
null,
new SecureRandom());
}

最新更新