将X509证书存储在数据库中

时间:2011-02-08 13:42:53

标签: c# .net certificate x509certificate pfx

为了访问Web服务,我需要一个证书。

我生成了我的证书:

openssl genrsa 1024 > private.key
openssl req -new -x509 -nodes -sha1 -days 1100 -key private.key > public.cer

然后通过

将这两者合并为一个pfx证书
openssl pkcs12 -in public.cer -inkey private.key -export -out client.pfx

然后将我的pfx文件加载为X509Certificate2

X509Certificate2 clientCert = new X509Certificate2("cert.pfx", "password");

现在,我想在数据库中创建一个包含以下字段的表:

PrivateKey NVARCHAR  
PublicCer NVARCHAR  
Password NVARCHAR  

然后从private.key文件中复制内容,以及----- BEGIN CERTIFICATE -----和----- END CERTIFICATE -----,同样用于public.cer,并设置密码。 现在,如何通过从DB读取此数据来获取X509Certificate2的正确实例? 换句话说,如何根据私钥和证书从代码生成pfx文件?

我会尝试更精确:

 string connectionString; string query; string cert;

    connectionString = ConfigurationManager.ConnectionStrings[0].ConnectionString;
    query = "SELECT clientcert FROM settings_services WHERE ID = 1";

    using (SqlConnection cn = new SqlConnection(connectionString))
    {
        SqlCommand cmd = new SqlCommand(query, cn);
        cn.Open();
        cert = (string)cmd.ExecuteScalar();
    }

    X509Certificate2  serverCert = new X509Certificate2(Encoding.UTF8.GetBytes(cert));

此代码将正确加载证书字符串(x509证书,以----- BEGIN CERTIFICATE -----开头,结束----- END CERTIFICATE -----)。

现在我需要获取私钥:

我的私钥是RSA格式(----- BEGIN RSA PRIVATE KEY ----等等)

我需要加载它,并将其分配给serverCert,以便能够使用此证书在Web服务上进行身份验证。

有关如何做到这一点的任何建议吗?

2 个答案:

答案 0 :(得分:6)

所以这实际上很简单,虽然我没有找到它的简单描述。我把证书字符串留在了我的要点(样本证书,没有安全数据)

https://gist.github.com/BillKeenan/5435753

[TestMethod]
public void TestCertificate()
{
    const string publicCert = @"MIIBrzCCARigAwIBAgIQEkeKoXKDFEuzql5XQnkY9zANBgkqhkiG9w0BAQUFADAYMRYwFAYDVQQDEw1DZXJ0QXV0aG9yaXR5MB4X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5aQ==</DQ><InverseQ>KrrKkN4IKqqhrcpZbYIWH4rWoCcnfTI5jxMfUDKUac+UFGNxHCUGLe1x+rwz4HcOA7bKVECyGe6C9xeiN3XKuQ==</InverseQ><D>Fsp6elUr6iu9V6Vrlm/lk16oTmU1rTNllLRCZJCeUlN/22bHuSVo27hHyZ1f+Q26bqeL9Zpq7rZgXvBsqzFt9tBOESrkr+uEHIZwQ1HIDw2ajxwOnlrj+zjn6EKshrMOsEXXbgSAi6SvGifRC2f+TKawt9lZmGElV4QgMYlC56k=</D></RSAKeyValue>";

    var certificate = new X509Certificate2(Convert.FromBase64String(publicCert));

    var crypto = new RSACryptoServiceProvider();

    crypto.FromXmlString(privateCert);

    certificate.PrivateKey = crypto;

    //export a private key
    var exportedPrivate = certificate.PrivateKey.ToXmlString(true);
    var exportedPublic = Convert.ToBase64String(certificate.RawData);

    Assert.AreEqual(publicCert, exportedPublic);
    Assert.AreEqual(privateCert, exportedPrivate);
 }

答案 1 :(得分:4)

你有一个带有byte []的构造函数。因此,您可以将证书存储为字节数组并使用

加载证书
public X509Certificate2(
    byte[] rawData
)

link:http://msdn.microsoft.com/en-us/library/ms148413%28v=VS.100%29.aspx