以编程方式创建带有私钥的虚拟证书以进行测试
Programmatically creating a dummy certificate with private key for testing
我有一个 C# 单元测试项目,我想在其中测试我编写的一些加密扩展。我想使用虚拟证书进行测试,但我有以下限制:
- 构建和测试运行是在我无法访问的机器上远程完成的。因此,我无法在构建机器上安装证书。
- 我不能将机密(例如
.pfx
文件)作为 git 存储库或构建的一部分,因为这样做会违反安全协议。因此,我无法从项目中包含的文件中读取证书。
因为要加密解密,所以需要私钥信息。我如何在这些限制下以编程方式创建此证书?
一种方法是创建一个用于测试的证书,将其转换为 base 64 字符串,然后在代码中从该字符串中读取证书。这需要四个步骤:
1.创建 .cer 和 .pvk 文件
为此,可以使用 MakeCert.exe
工具(注意 this tool is deprecated, and Microsoft recommends that you use the New-SelfSignedCertificate PowerShell cmdlet. I haven't tried this, but presumably either method would work.). This is a .NET Framework tool, and is included as part of the Windows SDK. I happened to have the Windows 7 SDK installed on my machine, so for me this exe was located under C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\
, but your results may vary. This tool does exactly what we want! From the documentation:
The Certificate Creation tool generates X.509 certificates for testing purposes only. It creates a public and private key pair for digital signatures and stores it in a certificate file. This tool also associates the key pair with a specified publisher's name and creates an X.509 certificate that binds a user-specified name to the public part of the key pair.
为了这个例子,我们将证书称为 TestCert
。要创建 .cer 和 .pvk 文件,运行 以下命令:
MakeCert.exe -sv TestCert.pvk -n "cn=Test Certificate" TestCert.cer -r
-sv
标志指示要创建的 .pvk 文件的名称,-n
标志是我们证书的符合 X.500 的名称(使用 "cn=CertName" 格式如上),-r
标志表示将自签名。如果要指定证书的开始和结束日期,请使用 -b
和 -e
标志,并将日期格式设置为 mm/dd/yyyy
(默认情况下,证书从当天开始有效创作至 2039 年)。如果您的证书将用于加密和解密(如我的),则必须指定 -sky Exchange
和 -pe
标志。在此过程中,系统会提示您为证书创建密码。
2。创建 .pfx 文件
这可以使用 pvk2pfx.exe
工具完成,该工具应该与 MakeCert.exe
位于同一位置。此工具将 .pvk 和 .cer 文件转换为 .pfx 文件。要使用此工具,运行 以下命令:
pvk2pfx.exe -pvk TestCert.pvk -spc TestCert.cer -pfx TestCert.pfx
-pvk
标志是要使用的.pvk文件的文件名(在步骤1中创建),-spc
标志是要使用的.cer文件的文件名(在步骤1中创建)在步骤 1) 中,-pfx
标志是将要创建的 .pfx 文件的名称。在此过程中,系统会提示您输入在步骤 1 中创建的密码。
3。获取 .pfx 文件的 base 64 字符串表示
这非常简单,可以使用 Get-Content
Powershell cmdlet and the System.Convert.ToBase64String
方法完成。为此,打开 Powershell window 和 运行 以下内容:
$content = Get-Content TestCert.pfx -Encoding Byte
[System.Convert]::ToBase64String($content) | Out-File "TestCert.txt"
现在我们在 TestCert.txt.
中有了 .pfx 文件的 base 64 字符串
4.以编程方式创建证书
现在我们可以像这样在代码中创建证书:
namespace MyTests
{
using System;
using System.Security.Cryptography.X509Certificates;
public class MyTests
{
// Copy and paste the string from TestCert.txt here.
private const string CertText = "<text>";
// Use the password you created in steps 1 and 2 here.
private const string Password = "p4ssw0rd";
// Create the certificate object.
private readonly X509Certificate2 TestCert = new X509Certificate2(
Convert.FromBase64String(MyTests.CertText),
MyTests.Password);
}
}
我有一个 C# 单元测试项目,我想在其中测试我编写的一些加密扩展。我想使用虚拟证书进行测试,但我有以下限制:
- 构建和测试运行是在我无法访问的机器上远程完成的。因此,我无法在构建机器上安装证书。
- 我不能将机密(例如
.pfx
文件)作为 git 存储库或构建的一部分,因为这样做会违反安全协议。因此,我无法从项目中包含的文件中读取证书。
因为要加密解密,所以需要私钥信息。我如何在这些限制下以编程方式创建此证书?
一种方法是创建一个用于测试的证书,将其转换为 base 64 字符串,然后在代码中从该字符串中读取证书。这需要四个步骤:
1.创建 .cer 和 .pvk 文件
为此,可以使用 MakeCert.exe
工具(注意 this tool is deprecated, and Microsoft recommends that you use the New-SelfSignedCertificate PowerShell cmdlet. I haven't tried this, but presumably either method would work.). This is a .NET Framework tool, and is included as part of the Windows SDK. I happened to have the Windows 7 SDK installed on my machine, so for me this exe was located under C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\
, but your results may vary. This tool does exactly what we want! From the documentation:
The Certificate Creation tool generates X.509 certificates for testing purposes only. It creates a public and private key pair for digital signatures and stores it in a certificate file. This tool also associates the key pair with a specified publisher's name and creates an X.509 certificate that binds a user-specified name to the public part of the key pair.
为了这个例子,我们将证书称为 TestCert
。要创建 .cer 和 .pvk 文件,运行 以下命令:
MakeCert.exe -sv TestCert.pvk -n "cn=Test Certificate" TestCert.cer -r
-sv
标志指示要创建的 .pvk 文件的名称,-n
标志是我们证书的符合 X.500 的名称(使用 "cn=CertName" 格式如上),-r
标志表示将自签名。如果要指定证书的开始和结束日期,请使用 -b
和 -e
标志,并将日期格式设置为 mm/dd/yyyy
(默认情况下,证书从当天开始有效创作至 2039 年)。如果您的证书将用于加密和解密(如我的),则必须指定 -sky Exchange
和 -pe
标志。在此过程中,系统会提示您为证书创建密码。
2。创建 .pfx 文件
这可以使用 pvk2pfx.exe
工具完成,该工具应该与 MakeCert.exe
位于同一位置。此工具将 .pvk 和 .cer 文件转换为 .pfx 文件。要使用此工具,运行 以下命令:
pvk2pfx.exe -pvk TestCert.pvk -spc TestCert.cer -pfx TestCert.pfx
-pvk
标志是要使用的.pvk文件的文件名(在步骤1中创建),-spc
标志是要使用的.cer文件的文件名(在步骤1中创建)在步骤 1) 中,-pfx
标志是将要创建的 .pfx 文件的名称。在此过程中,系统会提示您输入在步骤 1 中创建的密码。
3。获取 .pfx 文件的 base 64 字符串表示
这非常简单,可以使用 Get-Content
Powershell cmdlet and the System.Convert.ToBase64String
方法完成。为此,打开 Powershell window 和 运行 以下内容:
$content = Get-Content TestCert.pfx -Encoding Byte
[System.Convert]::ToBase64String($content) | Out-File "TestCert.txt"
现在我们在 TestCert.txt.
中有了 .pfx 文件的 base 64 字符串4.以编程方式创建证书
现在我们可以像这样在代码中创建证书:
namespace MyTests
{
using System;
using System.Security.Cryptography.X509Certificates;
public class MyTests
{
// Copy and paste the string from TestCert.txt here.
private const string CertText = "<text>";
// Use the password you created in steps 1 and 2 here.
private const string Password = "p4ssw0rd";
// Create the certificate object.
private readonly X509Certificate2 TestCert = new X509Certificate2(
Convert.FromBase64String(MyTests.CertText),
MyTests.Password);
}
}