如何使用.net core c#保存pfx文件
How to save pfx file using .net core c#
我已经创建了使用 pem 和私钥安装 pfx 证书的代码。 pfx 证书安装成功。但我需要将该 pfx 证书保存在指定位置(本地文件夹)。我的代码如下,
private static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("AppSettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
IConfigurationRoot configuration = builder.Build();
var pfxConfig = new AppSettings();
configuration.GetSection(Constants.AppSettings).Bind(pfxConfig);
string pemFilePath = pfxConfig.PemFilePath;
string privateKeyPath = pfxConfig.PrivateKeyPath;
string pfxSavePath = pfxConfig.PfxSavePath;
string pfxPassword = pfxConfig.PfxPassword;
byte[] publicPemBytes = File.ReadAllBytes(pemFilePath);
using var publicX509 = new X509Certificate2(publicPemBytes);
var privateKeyText = File.ReadAllText(privateKeyPath);
var privateKeyBlocks = privateKeyText.Split("-", StringSplitOptions.RemoveEmptyEntries);
var privateKeyBytes = Convert.FromBase64String(privateKeyBlocks[1]);
using RSA rsa = RSA.Create();
if (privateKeyBlocks[0] == Constants.BeginPrivateKey)
{
rsa.ImportPkcs8PrivateKey(privateKeyBytes, out _);
}
else if (privateKeyBlocks[0] == Constants.BeginRsaPrivateKey)
{
rsa.ImportRSAPrivateKey(privateKeyBytes, out _);
}
var keyPair = publicX509.CopyWithPrivateKey(rsa);
X509Certificate2 pfxCertificate = new X509Certificate2(keyPair.Export(X509ContentType.Pfx));
//pfxCertificate.Export(pfxSavePath, pfxPassword);
//Installation of pfx certificate.
X509Certificate2 x509Certificate2 = new X509Certificate2(pfxCertificate);
X509Store x509Store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
x509Store.Open(OpenFlags.ReadWrite);
x509Store.Add(x509Certificate2);
x509Store.Close();
Console.WriteLine(Constants.PfxInstallationSuccessMessage);
}
应用设置json文件如下,
{
"AppSettings": {
"PemFilePath": "F:\Certificate\123certificate.pem.crt",
"PrivateKeyPath": "F:\Certificate\123private.pem.key",
"PfxSavePath": "F:\Certificate\sample.pfx",
"PfxPassword": "password123"
}
}
如何使用c#将pfx证书保存在指定位置(本地系统)
导出 returns 表示您的证书的字节数组
将其写入文件:
byte[] certData = pfxCertificate.Export(X509ContentType.Pfx, "MyPassword");
File.WriteAllBytes(@"C:\MyCert.pfx", certData);
我已经创建了使用 pem 和私钥安装 pfx 证书的代码。 pfx 证书安装成功。但我需要将该 pfx 证书保存在指定位置(本地文件夹)。我的代码如下,
private static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("AppSettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
IConfigurationRoot configuration = builder.Build();
var pfxConfig = new AppSettings();
configuration.GetSection(Constants.AppSettings).Bind(pfxConfig);
string pemFilePath = pfxConfig.PemFilePath;
string privateKeyPath = pfxConfig.PrivateKeyPath;
string pfxSavePath = pfxConfig.PfxSavePath;
string pfxPassword = pfxConfig.PfxPassword;
byte[] publicPemBytes = File.ReadAllBytes(pemFilePath);
using var publicX509 = new X509Certificate2(publicPemBytes);
var privateKeyText = File.ReadAllText(privateKeyPath);
var privateKeyBlocks = privateKeyText.Split("-", StringSplitOptions.RemoveEmptyEntries);
var privateKeyBytes = Convert.FromBase64String(privateKeyBlocks[1]);
using RSA rsa = RSA.Create();
if (privateKeyBlocks[0] == Constants.BeginPrivateKey)
{
rsa.ImportPkcs8PrivateKey(privateKeyBytes, out _);
}
else if (privateKeyBlocks[0] == Constants.BeginRsaPrivateKey)
{
rsa.ImportRSAPrivateKey(privateKeyBytes, out _);
}
var keyPair = publicX509.CopyWithPrivateKey(rsa);
X509Certificate2 pfxCertificate = new X509Certificate2(keyPair.Export(X509ContentType.Pfx));
//pfxCertificate.Export(pfxSavePath, pfxPassword);
//Installation of pfx certificate.
X509Certificate2 x509Certificate2 = new X509Certificate2(pfxCertificate);
X509Store x509Store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
x509Store.Open(OpenFlags.ReadWrite);
x509Store.Add(x509Certificate2);
x509Store.Close();
Console.WriteLine(Constants.PfxInstallationSuccessMessage);
}
应用设置json文件如下,
{
"AppSettings": {
"PemFilePath": "F:\Certificate\123certificate.pem.crt",
"PrivateKeyPath": "F:\Certificate\123private.pem.key",
"PfxSavePath": "F:\Certificate\sample.pfx",
"PfxPassword": "password123"
}
}
如何使用c#将pfx证书保存在指定位置(本地系统)
导出 returns 表示您的证书的字节数组
将其写入文件:
byte[] certData = pfxCertificate.Export(X509ContentType.Pfx, "MyPassword");
File.WriteAllBytes(@"C:\MyCert.pfx", certData);