RSAForJava.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Org.BouncyCastle.Asn1.Pkcs;
  6. using Org.BouncyCastle.Asn1.X509;
  7. using Org.BouncyCastle.Crypto.Generators;
  8. using Org.BouncyCastle.Crypto.Parameters;
  9. using Org.BouncyCastle.Math;
  10. using Org.BouncyCastle.Pkcs;
  11. using Org.BouncyCastle.Security;
  12. using Org.BouncyCastle.Crypto.Engines;
  13. using Org.BouncyCastle.X509;
  14. using Org.BouncyCastle.Crypto;
  15. using Org.BouncyCastle.Asn1;
  16. using Org.BouncyCastle.Crypto.Encodings;
  17. namespace MySystem
  18. {
  19. public class RSAForJava
  20. {
  21. public RSAForJava()
  22. {
  23. }
  24. /// <summary>
  25. /// KEY 结构体
  26. /// </summary>
  27. public struct RSAKEY
  28. {
  29. /// <summary>
  30. /// 公钥
  31. /// </summary>
  32. public string PublicKey
  33. {
  34. get;
  35. set;
  36. }
  37. /// <summary>
  38. /// 私钥
  39. /// </summary>
  40. public string PrivateKey
  41. {
  42. get;
  43. set;
  44. }
  45. }
  46. public RSAKEY GetKey()
  47. {
  48. //RSA密钥对的构造器
  49. RsaKeyPairGenerator keyGenerator = new RsaKeyPairGenerator();
  50. //RSA密钥构造器的参数
  51. RsaKeyGenerationParameters param = new RsaKeyGenerationParameters(
  52. Org.BouncyCastle.Math.BigInteger.ValueOf(3),
  53. new Org.BouncyCastle.Security.SecureRandom(),
  54. 1024, //密钥长度
  55. 25);
  56. //用参数初始化密钥构造器
  57. keyGenerator.Init(param);
  58. //产生密钥对
  59. AsymmetricCipherKeyPair keyPair = keyGenerator.GenerateKeyPair();
  60. //获取公钥和密钥
  61. AsymmetricKeyParameter publicKey = keyPair.Public;
  62. AsymmetricKeyParameter privateKey = keyPair.Private;
  63. SubjectPublicKeyInfo subjectPublicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(publicKey);
  64. PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
  65. Asn1Object asn1ObjectPublic = subjectPublicKeyInfo.ToAsn1Object();
  66. byte[] publicInfoByte = asn1ObjectPublic.GetEncoded("UTF-8");
  67. Asn1Object asn1ObjectPrivate = privateKeyInfo.ToAsn1Object();
  68. byte[] privateInfoByte = asn1ObjectPrivate.GetEncoded("UTF-8");
  69. RSAKEY item = new RSAKEY()
  70. {
  71. PublicKey = Convert.ToBase64String(publicInfoByte),
  72. PrivateKey = Convert.ToBase64String(privateInfoByte)
  73. };
  74. return item;
  75. }
  76. private AsymmetricKeyParameter GetPublicKeyParameter(string s)
  77. {
  78. s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
  79. byte[] publicInfoByte = Convert.FromBase64String(s);
  80. Asn1Object pubKeyObj = Asn1Object.FromByteArray(publicInfoByte);//这里也可以从流中读取,从本地导入
  81. AsymmetricKeyParameter pubKey = PublicKeyFactory.CreateKey(publicInfoByte);
  82. return pubKey;
  83. }
  84. private AsymmetricKeyParameter GetPrivateKeyParameter(string s)
  85. {
  86. s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
  87. byte[] privateInfoByte = Convert.FromBase64String(s);
  88. // Asn1Object priKeyObj = Asn1Object.FromByteArray(privateInfoByte);//这里也可以从流中读取,从本地导入
  89. // PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
  90. AsymmetricKeyParameter priKey = PrivateKeyFactory.CreateKey(privateInfoByte);
  91. return priKey;
  92. }
  93. private AsymmetricKeyParameter GetPrivateKeyParameter2(string s)
  94. {
  95. s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
  96. byte[] privateInfoByte = System.Text.Encoding.UTF8.GetBytes(s);
  97. // Asn1Object priKeyObj = Asn1Object.FromByteArray(privateInfoByte);//这里也可以从流中读取,从本地导入
  98. // PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
  99. AsymmetricKeyParameter priKey = PrivateKeyFactory.CreateKey(privateInfoByte);
  100. return priKey;
  101. }
  102. public string EncryptByPrivateKey(string s, string key)
  103. {
  104. //非对称加密算法,加解密用
  105. IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
  106. //加密
  107. try
  108. {
  109. engine.Init(true, GetPrivateKeyParameter(key));
  110. byte[] byteData = System.Text.Encoding.UTF8.GetBytes(s);
  111. var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
  112. return Convert.ToBase64String(ResultData);
  113. //Console.WriteLine("密文(base64编码):" + Convert.ToBase64String(testData) + Environment.NewLine);
  114. }
  115. catch (Exception ex)
  116. {
  117. return ex.Message;
  118. }
  119. }
  120. public string DecryptByPublicKey(string s, string key)
  121. {
  122. s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
  123. //非对称加密算法,加解密用
  124. IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
  125. //解密
  126. try
  127. {
  128. engine.Init(false, GetPublicKeyParameter(key));
  129. byte[] byteData = Convert.FromBase64String(s);
  130. string result = "";
  131. List<byte> cache = new List<byte>();
  132. for (int i = 0; i < byteData.Length; i++)
  133. {
  134. cache.Add(byteData[i]);
  135. if ((i + 1) % 256 == 0 || i + 1 == byteData.Length)
  136. {
  137. var ResultData = engine.ProcessBlock(cache.ToArray(), 0, cache.ToArray().Length);
  138. result += System.Text.Encoding.UTF8.GetString(ResultData);
  139. cache.Clear();
  140. }
  141. }
  142. return result;
  143. }
  144. catch (Exception ex)
  145. {
  146. return ex.Message;
  147. }
  148. }
  149. public string DecryptByPrivateKey(string s, string key)
  150. {
  151. s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
  152. //非对称加密算法,加解密用
  153. IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
  154. //解密
  155. try
  156. {
  157. engine.Init(false, GetPrivateKeyParameter2(key));
  158. byte[] byteData = Convert.FromBase64String(s);
  159. string result = "";
  160. List<byte> cache = new List<byte>();
  161. for (int i = 0; i < byteData.Length; i++)
  162. {
  163. cache.Add(byteData[i]);
  164. if ((i + 1) % 256 == 0 || i + 1 == byteData.Length)
  165. {
  166. var ResultData = engine.ProcessBlock(cache.ToArray(), 0, cache.ToArray().Length);
  167. result += System.Text.Encoding.UTF8.GetString(ResultData);
  168. cache.Clear();
  169. }
  170. }
  171. return result;
  172. }
  173. catch (Exception ex)
  174. {
  175. return ex.Message;
  176. }
  177. }
  178. }
  179. }