[英]OpenPGP decryption in C# with X.509 Certificate .pfx file private key
朋友們,
我們從證書頒發機構采購了 class 3 證書 .pfx 和 .cer 證書。 並將 shared.cer(public key) 分享給我們的合作伙伴。
加密(Java)
我們的合作伙伴使用 Java 充氣城堡 openpgp 標准加密了消息(使用我們的公鑰),並分享了如下加密消息,-----BEGIN PGP MESSAGE----- 版本:x v2hQEMAzFXJ94q1Nm8AQf/Tld0/3dAvgFKPQVBS8bmbXChXeApeReo1ydNS+.... .. -----結束 PGP 消息-----
解密: (C#)
我們需要使用我們的.pfx 文件解密消息。
我瀏覽了以下文章http://burnignorance.com/c-coding-tips/pgp-encryption-decryption-in-c/似乎正在生成新的 PGPKeyPair 並用於加密和解密。
但就我而言,我有.pfx 文件我們如何從.pfx 文件中提取用於解密的pgpprivate 密鑰? 您能否分享一些關於我們如何做到這一點的想法。 提前感謝您為此付出的所有時間。
2020 年 13 月 12 日
我已將 X509Certificate.pfx 導入到如下所示的存儲中並嘗試轉換 pgpprivate 密鑰,
string certPath = @"C:\Users\test.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 class 文件讀取 pfx 文件,然后將密鑰轉換為 PgpSecretKey。
閱讀有關 --> pkcs12.GetKey() 和 PgpSecretKey class 的文檔。
方法一。
public static void GetPriveKey(String pfxFile, String pfxPassword) { //加載 PKCS12 文件 Pkcs12Store pkcs12 = new Pkcs12Store(new FileStream(pfxFile, FileMode.Open, FileAccess.Read), pfxPassword.ToArray()); 字符串 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());
方法二
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.