首页 新闻 会员 周边 捐助

C# RSA公钥与私钥的读取写入问题?

0
悬赏园豆:50 [待解决问题]
1  RSACryptoServiceProvider rsaProvider;
2  rsaProvider = new RSACryptoServiceProvider(1024);
3  PublicKey = rsaProvider.ToXmlString(false); //将RSA算法的公钥导出到字符串PublicKey中,参数为false表示不导出私钥
4  PrivateKey = rsaProvider.ToXmlString(true);//将RSA算法的私钥导出到字符串PrivateKey中,参数为true表示导出私钥

将Publickey和PrivateKey写入xml中。

publickey.xml:

<RSAKeyValue>

<Modulus>

3bpJFQs8hsSUrKALPqIFSK/0/YuW8t6U0LhQUTcB7NTYY0HZhPQoNDvQ4PTVrTiU6xSjP4AqDJN7GnOQhDZMZyKrmbN7vMVd6RhFbpKd4kofwJGYQDzXQIuk1O1snIHTwnKMK7owzvHyKV7Bl4OI5EUcgkWHWK4cHWEaTHVBQKU=

</Modulus>

<Exponent>AQAB</Exponent>

</RSAKeyValue>

 

PrivateKey.xml:

 

<RSAKeyValue><Modulus>3bpJFQs8hsSUrKALPqIFSK/0/YuW8t6U0LhQUTcB7NTYY0HZhPQoNDvQ4PTVrTiU6xSjP4AqDJN7GnOQhDZMZyKrmbN7vMVd6RhFbpKd4kofwJGYQDzXQIuk1O1snIHTwnKMK7owzvHyKV7Bl4OI5EUcgkWHWK4cHWEaTHVBQKU=</Modulus>
<Exponent>AQAB</Exponent>

<P>
91wY+w4+UmHncwFRO/d95s5ul+Dn6HBi+ElSRs2ePFZxXXgCD5zc5B1h+QRgwZmfc5hGWcZ5ikwcbQ7dR0OZlQ==
</P>

<Q>
5Xj8ErsPdYJ/Qt/UX9dwpRkymyPkbB6SqvnXkSjsWv2mAHM+CE6KmKHEg5VYxutHI1PsWmmTfJuLDq9xy4Hm0Q==
</Q>

<DP>1Mhs1MpT4hpGNzAS6TrlGv7JM+1MkS7cdCjelfma/Nx/eKAbZcLSYYjFFqklcmTsPUvoeKG7O7RtX4NB5mPIYQ==
</DP>

<DQ>
WAoq7CTIsZT3rnihyoOlvDIGsG783F5+dwqUIiVJST+3yMRuMFGx6w1lj/ZksSzjhjJUo/uNkmk62M+HIUUdgQ==
</DQ>
<InverseQ>
zcgCM9otg4uoxMURNPd7xQ7zZUORJg7668KDgHEYZjF7r0CTylEPGHYdXS/MyBtHHDIVQ7qgOpSzHnaz7d4IFQ==

</InverseQ>

<D>

n5L4EdsaXmuQDITKUvNMFjn8eZVCt9wVAfoEY87Q5d9xNE4p/BOz85OxCpVqStZT5rPdoU/d81sY17qgIWbqqGAQuTp/Jn3qOzoQrqph2u3lej4YpOxhSvffgr9ZUB+Sbk/UWIFardCdO9ADnTCQeLwqLLmPN/VIPikpgTylj4E=

</D>

</RSAKeyValue>

Certificates.cer:

<License>
  <Key>
    <Host>localhost</Host>
    <LicenseDate>2010/08/08</LicenseDate>
    <Expires>2099/12/12</Expires>
    <SiteQty>99</SiteQty>
   <Signature>QVlu0UNlbmjQ75yaFZuHk0fFn4wphcZf4ofiHsPA4PUS2Jp1tbPIKCPSjPLe50QOemq7f/5jjyoEC2cXJta1u1Xpwfp8zzMXjJhTSX+15faPpvCypcYawuZnRVih9z/JkTuCCekZAh2kCBa/g86YymRjpZAdgcMtHbcX1ZsTdXY=</Signature>
  </Key>
</License>

 

这里私钥包涵多个节点,请问如何将私钥写入Certificates.cer文件中?

kitsune的主页 kitsune | 初学一级 | 园豆:105
提问于:2012-07-05 08:49

对xml数字签名的具体代码能贴出来吗 大佬

大哥写代码 2年前
< >
分享
所有回答(1)
0

.Cer 证书文件不包含私钥,.pfx 才包含私钥。

可用 X509Certificate2 导出 .pfx 证书。

证书文件通过 makecert.exe 生成。

通过代码直接编写.cer 或 .pfx 的证书文件会比较困难,有C/C++的dll可用。

Launcher | 园豆:45050 (高人七级) | 2012-07-05 09:18

我们的项目中时这样的:

......

 1   XmlNode node = document.DocumentElement.SelectSingleNode("//Host");
 2    XmlNode node2 = document.DocumentElement.SelectSingleNode("//LicenseDate");
 3    XmlNode node3 = document.DocumentElement.SelectSingleNode("//Expires");
 4    XmlNode node4 = document.DocumentElement.SelectSingleNode("//SiteQty");
 5    XmlNode node5 = document.DocumentElement.SelectSingleNode("//Signature");//以上便是从.cer证书文件中获取的节点
 6    if (string.Compare(node.InnerText, masterSettings.SiteUrl, true, CultureInfo.InvariantCulture) == 0)
 7    {
 8    string s = string.Format(
 9           CultureInfo.InvariantCulture, 
10           "Host={0}&Expires={1}&SiteQty={2}&LicenseDate={3}", 
11           new object[] { masterSettings.SiteUrl, node3.InnerText, node4.InnerText, node2.InnerText });
12    using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider())
13    {
14     provider.FromXmlString(LicenseHelper.GetPublicKey());//载入公钥
15     RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(provider);
16     deformatter.SetHashAlgorithm("SHA1");//指定哈希算法
17     byte[] rgbSignature = Convert.FromBase64String(node5.InnerText);//获得私钥
18     byte[] rgbHash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(s));
19     isValid = deformatter.VerifySignature(rgbHash, rgbSignature);
20     }
21     expired = DateTime.Now > DateTime.Parse(node3.InnerText);
22     if (!(!isValid ? true : expired))
23     {
24        int.TryParse(node4.InnerText, out siteQty);
25     }

 

 1 XmlNode node = document.DocumentElement.SelectSingleNode("//Host");
 2             XmlNode node2 = document.DocumentElement.SelectSingleNode("//LicenseDate");
 3             XmlNode node3 = document.DocumentElement.SelectSingleNode("//Expires");
 4             XmlNode node4 = document.DocumentElement.SelectSingleNode("//SiteQty");
 5             XmlNode node5 = document.DocumentElement.SelectSingleNode("//Signature");
 6             if (string.Compare(node.InnerText, masterSettings.SiteUrl, true, CultureInfo.InvariantCulture) == 0)
 7             {
 8                 string s = string.Format(
 9                     CultureInfo.InvariantCulture, 
10                     "Host={0}&Expires={1}&SiteQty={2}&LicenseDate={3}", 
11                     new object[] { masterSettings.SiteUrl, node3.InnerText, node4.InnerText, node2.InnerText });
12                 using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider())
13                 {
14                     provider.FromXmlString(LicenseHelper.GetPublicKey());//载入公钥
15                     RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(provider);
16                     deformatter.SetHashAlgorithm("SHA1");//指定哈希算法
17                     byte[] rgbSignature = Convert.FromBase64String(node5.InnerText);//获得私钥
18                     byte[] rgbHash = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(s));
19                     isValid = deformatter.VerifySignature(rgbHash, rgbSignature);
20                 }
21                 expired = DateTime.Now > DateTime.Parse(node3.InnerText);
22                 if (!(!isValid ? true : expired))
23                 {
24                     int.TryParse(node4.InnerText, out siteQty);
25                 }

......

这里提取"node5"的值与公钥验证字符串"S",只是不知道怎么将私钥保存到.cer中的,本人小菜,望指点。

支持(0) 反对(0) kitsune | 园豆:105 (初学一级) | 2012-07-06 09:45

@i'm: .cer不包含私钥信息,Signature是签名值,不是私钥。

支持(0) 反对(0) Launcher | 园豆:45050 (高人七级) | 2012-07-06 10:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册