在给定 SecureString 密码的情况下,从 PFX 文件保存和加载 CngKey 的最简单方法是什么?
What's the simplest way to save and load a CngKey from a PFX file, given a SecureString password?
给定新生成的可导出 public/private 密钥对 CngKey
和 SecureString
,我需要进行哪些 API 调用才能创建包含 public/private 用密码保护内容?给定一个 PFX 文件和一个 SecureString
密码,需要什么 API 调用才能将内容加载到 CngKey
?
看起来基础 class 库没有提供这个,但我更喜欢 p/invoke 而不是依赖第三方库。如果有办法(类似于 SecureString
)在 PFX 和 CngKey
.
之间保持安全,我也想避免将解密的私钥存储在内存中,即使是暂时的
当我试图围绕这些概念思考时,我能找到的所有示例都涉及自签名证书。这不是证书,只是我想用密码保护的 public/private blob。我不想将 blob 导入商店,我想从 PFX 文件中使用它。
这是我自己得到的:
using (var key = CngKey.Create(CngAlgorithm.ECDsaP521, null, new CngKeyCreationParameters { ExportPolicy = CngExportPolicies.AllowExport, KeyUsage = CngKeyUsages.Signing }))
using (var algorithm = new ECDsaCng(key))
{
var container = new X509Certificate2();
container.PrivateKey = algorithm; // Exception: m_safeCertContext is an invalid handle
var pfxFileContents = container.Export(X509ContentType.Pkcs12, password);
using (var pfxFile = File.Create("text.pfx"))
pfxFile.Write(pfxFileContents, 0, pfxFileContents.Length);
}
这种情况下最好的解决办法是参考Security.Cryptography.dll。这个开源库是稳定的,如果我理解正确的话,它可能会被合并到 .NET Framework 中;它背后的开发者来自团队。
要将 CngKey
存储在 SecureString
受保护的 PFX 流中:
// NB! X509Certificate2 does not implement IDisposable in .NET 4.5.2, but it does in 4.6.1.
using (var cert = key.CreateSelfSignedCertificate(new X509CertificateCreationParameters(new X500DistinguishedName("CN=Example Name"))
{
StartTime = DateTime.Now,
EndTime = DateTime.MaxValue,
TakeOwnershipOfKey = true,
SignatureAlgorithm = X509CertificateSignatureAlgorithm.ECDsaSha256 // Manually match your CngKey type (RSA/ECDSA)
}))
{
File.WriteAllBytes(pfxPath, cert.Export(X509ContentType.Pkcs12, pfxPassword));
}
从 SecureString
受保护的 PFX 流加载 CngKey
:
// NB! X509Certificate2 does not implement IDisposable in .NET 4.5.2, but it does in 4.6.1.
using (var cert = new X509Certificate2(pfxPath, pfxPassword))
return cert.GetCngPrivateKey();
给定新生成的可导出 public/private 密钥对 CngKey
和 SecureString
,我需要进行哪些 API 调用才能创建包含 public/private 用密码保护内容?给定一个 PFX 文件和一个 SecureString
密码,需要什么 API 调用才能将内容加载到 CngKey
?
看起来基础 class 库没有提供这个,但我更喜欢 p/invoke 而不是依赖第三方库。如果有办法(类似于 SecureString
)在 PFX 和 CngKey
.
当我试图围绕这些概念思考时,我能找到的所有示例都涉及自签名证书。这不是证书,只是我想用密码保护的 public/private blob。我不想将 blob 导入商店,我想从 PFX 文件中使用它。
这是我自己得到的:
using (var key = CngKey.Create(CngAlgorithm.ECDsaP521, null, new CngKeyCreationParameters { ExportPolicy = CngExportPolicies.AllowExport, KeyUsage = CngKeyUsages.Signing }))
using (var algorithm = new ECDsaCng(key))
{
var container = new X509Certificate2();
container.PrivateKey = algorithm; // Exception: m_safeCertContext is an invalid handle
var pfxFileContents = container.Export(X509ContentType.Pkcs12, password);
using (var pfxFile = File.Create("text.pfx"))
pfxFile.Write(pfxFileContents, 0, pfxFileContents.Length);
}
这种情况下最好的解决办法是参考Security.Cryptography.dll。这个开源库是稳定的,如果我理解正确的话,它可能会被合并到 .NET Framework 中;它背后的开发者来自团队。
要将 CngKey
存储在 SecureString
受保护的 PFX 流中:
// NB! X509Certificate2 does not implement IDisposable in .NET 4.5.2, but it does in 4.6.1.
using (var cert = key.CreateSelfSignedCertificate(new X509CertificateCreationParameters(new X500DistinguishedName("CN=Example Name"))
{
StartTime = DateTime.Now,
EndTime = DateTime.MaxValue,
TakeOwnershipOfKey = true,
SignatureAlgorithm = X509CertificateSignatureAlgorithm.ECDsaSha256 // Manually match your CngKey type (RSA/ECDSA)
}))
{
File.WriteAllBytes(pfxPath, cert.Export(X509ContentType.Pkcs12, pfxPassword));
}
从 SecureString
受保护的 PFX 流加载 CngKey
:
// NB! X509Certificate2 does not implement IDisposable in .NET 4.5.2, but it does in 4.6.1.
using (var cert = new X509Certificate2(pfxPath, pfxPassword))
return cert.GetCngPrivateKey();