RSA非对称 私钥加密

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         }
View Code

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     }
View Code

私钥加密数据

 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         }
View Code

需要引用BouncyCastle.Crypto.dll

原文地址:https://www.cnblogs.com/liuchangxu/p/11573327.html