简体   繁体   English

如何安装.cer文件并使用私钥导出到.pfx文件

[英]How to Install a .cer file and Export to a .pfx file with private key

I have a requirement from my Client to Install .cer file without a private key and export it to a .pfx file with Private key using C# 我的客户要求安装没有私钥的.cer文件,并使用C#将其导出到带有私钥的.pfx文件

I tried all the different solutions provided which allowed me to export .cer to .pfx but without a key and .pfx file is empty 我尝试了所有提供的不同解决方案,允许我将.cer导出到.pfx但没有密钥和.pfx文件为空

string file = @"C:\TestCert1.cer";
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
X509Certificate2 test = new X509Certificate2(X509Certificate2.CreateFromCertFile(file));
string name = test.SerialNumber;
store.Certificates.Insert(0, test);
store.Add(test);
store.Close();

byte[] certData = store.Certificates.Export(X509ContentType.Pfx, "MyPassword");
File.WriteAllBytes(@"C:\MyCert.pfx", certData);

This part is OK: 这部分还可以:

string file = @"C:\TestCert1.cer";
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);

Instead of 代替

X509Certificate2 test = new X509Certificate2(X509Certificate2.CreateFromCertFile(file));

you want the much simpler 你想要更简单

X509Certificate2 test = new X509Certificate2(file);

Not sure what this was for, you never used the variable. 不知道这是为了什么,你从来没有使用过这个变量。

string name = test.SerialNumber;

This line does nothing useful. 这条线没有任何用处。 ( store.Certificates returns a new collection that's a copy of the state of the store at that time... you add to that collection then let it fall out of scope). store.Certificates返回一个新集合,该集合是当时商店状态的副本...您添加到该集合然后让它超出范围)。

store.Certificates.Insert(0, test);

This one actually adds the cert to the store, so it's OK, except not necessary for your goals. 这个实际上是将证书添加到商店,所以它没关系,除了你的目标没有必要。

store.Add(test);

So now you close the store then try to export it. 所以现在关闭商店然后尝试导出它。 Which doesn't make sense, the closed store's Certificates property always returns an empty collection. 这没有意义,关闭商店的Certificates属性总是返回一个空集合。 (It should have thrown, but that ship sailed long ago). (它应该抛出,但很久以前这艘船就航行了)。

store.Close();

byte[] certData = store.Certificates.Export(X509ContentType.Pfx, "MyPassword");
File.WriteAllBytes(@"C:\MyCert.pfx", certData);

What you probably want is to open the file, then look through the store and see if there's a match, and export the match. 您可能想要的是打开文件,然后浏览商店并查看是否匹配,并导出匹配项。

using (X509Certificate2 filter = new X509Certificate2(file))
using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
    store.Open(OpenFlags.ReadOnly);

    // The bytes making up the certificate, in DER/binary form.
    byte[] filterRawData = filter.RawData;

    foreach (X509Certificate2 storeCert in store.Certificates)
    {
        if (storeCert.RawData.SequenceEquals(filterRawData))
        {
            File.WriteAllBytes(outputFileName, storeCert.Export(X509ContentType.Pfx, password));
            return;
        }
    }   
}

Console.WriteLine("No match found...");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM