我有一个来自成功的LetsEncrypt证书请求的.cer文件输出。
我有用于创建LetsEncrypt的证书签名请求(CSR)的原始私钥。
现在,我们需要使用.NET将这两个文件以编程方式组合到IIS的PFX捆绑软件中
由于我们正在尝试以编程方式执行pvk2pfx,因此不切实际,如果可能,我们希望避免使用openssl。
尽管为了演示,我们正在尝试复制此功能,但使用CS .NET而不是pvk2pfx:pvk2pfx.exe -pvk Server.pvk -spc Server.cer -pfx Server.pfx
我已经进行了详尽的研究,这是我看到的可能性:
一种方法似乎正在使用X509Certificate2之类的东西:
// Import the certificate
X509Certificate2 cert = new X509Certificate2("c:\\cert.cer");
// Import the private key
X509Certificate2 cert = new X509Certificate2("c:\\key.pvk");
// Or import the private key - Alternative method
X509DecryptString(token, @"c:\CA.pvk", "mypassword");
// Export the PFX file
certificate.Export(X509ContentType.Pfx, "YourPassword");
File.WriteAllBytes(@"C:\YourCert.pfx", certificateData);
这里有一些其他方法,但是它们似乎都忽略了有关私钥的部分,或者它们需要pvk2pfx.exe
从证书文件转换为pfx文件https://stackoverflow.com/a/4797392/3693688
如何以编程方式创建X509Certificate2?http://www.wiktorzychla.com/2012/12/how-to-create-x509certificate2.html
选择,创建和查找X509证书:http://www.wou.edu/~rvitolo06/WATK/Demos/HPCImageRendering/code/ImageRendering/AppConfigure/CertHelper.cs
无法将生成的带有私钥的证书导出到字节数组Cannot export generated certificate with a private key to byte array in .NET 4.0/4.5
如何以编程方式将带有证书链的pfx导入证书存储。https://stackoverflow.com/a/9152838/3693688
以编程方式在C#中导入.cer和.pvk证书文件,以便与netsh http add sslcert
一起使用https://gist.github.com/BrandonLWhite/235fa12247f6dc827051
将cer转换为pfx证书的方法https://gist.github.com/domgreen/988684
CryptoGuy建议我们需要此链接:https://gist.github.com/BrandonLWhite/235fa12247f6dc827051
这是否意味着这样会很好?
CSP零件是否必要?
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
var PublicKey = AssemblyUtility.GetEmbeddedFileAsByteArray("Cert.cer");
var PrivateKey = AssemblyUtility.GetEmbeddedFileAsByteArray("PrivateKey.pvk");
var certificate = new X509Certificate2(PublicKey, string.Empty,
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
var cspParams = new CspParameters
{
ProviderType = 1,
Flags = CspProviderFlags.UseMachineKeyStore,
KeyContainerName = Guid.NewGuid().ToString().ToUpperInvariant()
};
var rsa = new RSACryptoServiceProvider(cspParams);
rsa.ImportCspBlob(ExtractPrivateKeyBlobFromPvk(PrivateKey));
rsa.PersistKeyInCsp = true;
certificate.PrivateKey = rsa;
certificate.Export(X509ContentType.Pfx, "YourPassword");
File.WriteAllBytes(@"C:\YourCert.pfx", certificateData);
CryptoGuy的回答确实很有帮助,并向我们指出了正确的方向。
我们仍在努力导入Binary DER文件,但此代码解决了该问题:
var oc = OpenSSL.X509.X509Certificate.FromDER(bio);
这些页面很有用:
https://github.com/openssl-net/openssl-net/blob/master/ManagedOpenSsl/X509/X509Certificate.cs
非常感谢您的帮助:)