【发布时间】:2012-01-22 07:05:39
【问题描述】:
在查看了如何从Creating a self-signed certificate in C# 生成自签名数字签名之后,我可以调用 CreateSelfSignCertificatePfx 并返回字节数组中的 PXF 数据,然后可以在 X509Certificate2 对象中使用这些数据进行签名和验证。示例...
byte[] pfx = Certificate.CreateSelfSignCertificatePfx("O=Company,CN=Firstname,SN=Lastname", DateTime.Now, DateTime.Now.AddYears(1), "password");
X509Certificate2 cert = new X509Certificate2(pfx, "password");
byte[] publicBytes = cert.RawData;
RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)cert.PrivateKey;
byte[] signedData = rsa.SignData(new System.Text.UTF8Encoding().GetBytes("Test"), new SHA1CryptoServiceProvider());
RSACryptoServiceProvider rsa2 = (RSACryptoServiceProvider)new X509Certificate2(publicBytes).PublicKey.Key;
bool verified = rsa2.VerifyData(new System.Text.UTF8Encoding().GetBytes("Test"), new SHA1CryptoServiceProvider(), signedData);
这行得通。不过,我担心的是原始字节 byte[] pfx 来自上面,需要存储在数据库中(用于签名)。问题变成了,这种格式的字节有多安全?我知道您需要密码才能使用私钥构造新的 X509Certificate2,但一般来说,没有密码的字节有多安全?将这些字节加密为附加层没有问题,但有必要吗?
根据X509Certificate2.X509Certificate2(Byte[], String) Constructor
使用正确的密码调用此构造函数会解密私钥并将其保存到密钥容器中。
我只是想确保私钥在没有密码的情况下是安全的。
【问题讨论】: