欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

RSA非对称 私钥加密

程序员文章站 2023-10-16 19:11:33
RSA生成公钥和私钥对 1 /// 2 /// RSA生成公钥和私钥 3 /// 4 /// 5 public static string[] GenerateKeys() 6 { 7 try 8 { 9 string ......

rsa生成公钥和私钥对

 1 /// <summary>
 2         /// rsa生成公钥和私钥
 3         /// </summary>
 4         /// <returns></returns>
 5         public static string[] generatekeys()
 6         {
 7             try
 8             {
 9                 string[] skeys = new string[2];
10                 rsacryptoserviceprovider rsa = new rsacryptoserviceprovider(1024);
11                 skeys[0] = rsa.toxmlstring(true);//私钥
12                 skeys[1] = rsa.toxmlstring(false);//公钥
13                 return skeys;
14             }
15             catch (exception)
16             {
17                 return null;
18             }
19         }

rsa私钥格式转换

 1 public class rsakeyconvert
 2     {
 3         /// <summary>
 4         /// rsa私钥格式转换,java->.net
 5         /// </summary>
 6         /// <param name="privatekey">java生成的rsa私钥</param>
 7         /// <returns></returns>
 8         public static string rsaprivatekeyjava2dotnet(string privatekey)
 9         {
10             rsaprivatecrtkeyparameters privatekeyparam = (rsaprivatecrtkeyparameters)privatekeyfactory.createkey(convert.frombase64string(privatekey));
11 
12             return string.format("<rsakeyvalue><modulus>{0}</modulus><exponent>{1}</exponent><p>{2}</p><q>{3}</q><dp>{4}</dp><dq>{5}</dq><inverseq>{6}</inverseq><d>{7}</d></rsakeyvalue>",
13                 convert.tobase64string(privatekeyparam.modulus.tobytearrayunsigned()),
14                 convert.tobase64string(privatekeyparam.publicexponent.tobytearrayunsigned()),
15                 convert.tobase64string(privatekeyparam.p.tobytearrayunsigned()),
16                 convert.tobase64string(privatekeyparam.q.tobytearrayunsigned()),
17                 convert.tobase64string(privatekeyparam.dp.tobytearrayunsigned()),
18                 convert.tobase64string(privatekeyparam.dq.tobytearrayunsigned()),
19                 convert.tobase64string(privatekeyparam.qinv.tobytearrayunsigned()),
20                 convert.tobase64string(privatekeyparam.exponent.tobytearrayunsigned()));
21         }
22 
23         /// <summary>
24         /// rsa私钥格式转换,.net->java
25         /// </summary>
26         /// <param name="privatekey">.net生成的私钥</param>
27         /// <returns></returns>
28         public static string rsaprivatekeydotnet2java(string privatekey)
29         {
30             xmldocument doc = new xmldocument();
31             doc.loadxml(privatekey);
32             biginteger m = new biginteger(1, convert.frombase64string(doc.documentelement.getelementsbytagname("modulus")[0].innertext));
33             biginteger exp = new biginteger(1, convert.frombase64string(doc.documentelement.getelementsbytagname("exponent")[0].innertext));
34             biginteger d = new biginteger(1, convert.frombase64string(doc.documentelement.getelementsbytagname("d")[0].innertext));
35             biginteger p = new biginteger(1, convert.frombase64string(doc.documentelement.getelementsbytagname("p")[0].innertext));
36             biginteger q = new biginteger(1, convert.frombase64string(doc.documentelement.getelementsbytagname("q")[0].innertext));
37             biginteger dp = new biginteger(1, convert.frombase64string(doc.documentelement.getelementsbytagname("dp")[0].innertext));
38             biginteger dq = new biginteger(1, convert.frombase64string(doc.documentelement.getelementsbytagname("dq")[0].innertext));
39             biginteger qinv = new biginteger(1, convert.frombase64string(doc.documentelement.getelementsbytagname("inverseq")[0].innertext));
40 
41             rsaprivatecrtkeyparameters privatekeyparam = new rsaprivatecrtkeyparameters(m, exp, d, p, q, dp, dq, qinv);
42 
43             privatekeyinfo privatekeyinfo = privatekeyinfofactory.createprivatekeyinfo(privatekeyparam);
44             byte[] serializedprivatebytes = privatekeyinfo.toasn1object().getencoded();
45             return convert.tobase64string(serializedprivatebytes);
46         }
47 
48         /// <summary>
49         /// rsa公钥格式转换,java->.net
50         /// </summary>
51         /// <param name="publickey">java生成的公钥</param>
52         /// <returns></returns>
53         public static string rsapublickeyjava2dotnet(string publickey)
54         {
55             rsakeyparameters publickeyparam = (rsakeyparameters)publickeyfactory.createkey(convert.frombase64string(publickey));
56             return string.format("<rsakeyvalue><modulus>{0}</modulus><exponent>{1}</exponent></rsakeyvalue>",
57                 convert.tobase64string(publickeyparam.modulus.tobytearrayunsigned()),
58                 convert.tobase64string(publickeyparam.exponent.tobytearrayunsigned()));
59         }
60 
61         /// <summary>
62         /// rsa公钥格式转换,.net->java
63         /// </summary>
64         /// <param name="publickey">.net生成的公钥</param>
65         /// <returns></returns>
66         public static string rsapublickeydotnet2java(string publickey)
67         {
68             xmldocument doc = new xmldocument();
69             doc.loadxml(publickey);
70             biginteger m = new biginteger(1, convert.frombase64string(doc.documentelement.getelementsbytagname("modulus")[0].innertext));
71             biginteger p = new biginteger(1, convert.frombase64string(doc.documentelement.getelementsbytagname("exponent")[0].innertext));
72             rsakeyparameters pub = new rsakeyparameters(false, m, p);
73 
74             subjectpublickeyinfo publickeyinfo = subjectpublickeyinfofactory.createsubjectpublickeyinfo(pub);
75             byte[] serializedpublicbytes = publickeyinfo.toasn1object().getderencoded();
76             return convert.tobase64string(serializedpublicbytes);
77         }
78     }

私钥加密数据

 1 /// <summary>
 2         /// 用私钥给数据进行rsa加密  
 3         /// </summary>  
 4         /// <param name="xmlprivatekey">私钥</param>  
 5         /// <param name="strencryptstring">待加密数据</param>  
 6         /// <returns>加密后的数据(base64)</returns>  
 7         public static string rsaencryptbyprivatekey(string xmlprivatekey, string strencryptstring)
 8         {
 9             //加载私钥  
10             rsacryptoserviceprovider privatersa = new rsacryptoserviceprovider(1024);
11             privatersa.fromxmlstring(xmlprivatekey);
12 
13             //转换密钥  
14             asymmetriccipherkeypair keypair = dotnetutilities.getkeypair(privatersa);
15 
16             ibufferedcipher c = cipherutilities.getcipher("rsa/ecb/pkcs1padding");// 参数与java中加密解密的参数一致
17             //第一个参数为true表示加密,为false表示解密;第二个参数表示密钥
18             c.init(true, keypair.private);
19 
20             byte[] datatoencrypt = encoding.utf8.getbytes(strencryptstring);
21             byte[] outbytes = c.dofinal(datatoencrypt);//加密  
22             string strbase64 = convert.tobase64string(outbytes);
23 
24             return strbase64;
25         }

需要引用bouncycastle.crypto.dll