RSAForJava.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 GetPublicKeyParameter2(string s)
  85. {
  86. s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
  87. byte[] publicInfoByte = System.Text.Encoding.UTF8.GetBytes(s);
  88. Asn1Object pubKeyObj = Asn1Object.FromByteArray(publicInfoByte);//这里也可以从流中读取,从本地导入
  89. AsymmetricKeyParameter pubKey = PublicKeyFactory.CreateKey(publicInfoByte);
  90. return pubKey;
  91. }
  92. private AsymmetricKeyParameter GetPrivateKeyParameter(string s)
  93. {
  94. s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
  95. byte[] privateInfoByte = Convert.FromBase64String(s);
  96. // Asn1Object priKeyObj = Asn1Object.FromByteArray(privateInfoByte);//这里也可以从流中读取,从本地导入
  97. // PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
  98. AsymmetricKeyParameter priKey = PrivateKeyFactory.CreateKey(privateInfoByte);
  99. return priKey;
  100. }
  101. private AsymmetricKeyParameter GetPrivateKeyParameter2(string s)
  102. {
  103. s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
  104. byte[] privateInfoByte = System.Text.Encoding.UTF8.GetBytes(s);
  105. // Asn1Object priKeyObj = Asn1Object.FromByteArray(privateInfoByte);//这里也可以从流中读取,从本地导入
  106. // PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKey);
  107. AsymmetricKeyParameter priKey = PrivateKeyFactory.CreateKey(privateInfoByte);
  108. return priKey;
  109. }
  110. public string EncryptByPrivateKey(string s, string key)
  111. {
  112. //非对称加密算法,加解密用
  113. IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
  114. //加密
  115. try
  116. {
  117. engine.Init(true, GetPrivateKeyParameter(key));
  118. byte[] byteData = System.Text.Encoding.UTF8.GetBytes(s);
  119. var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
  120. return Convert.ToBase64String(ResultData);
  121. //Console.WriteLine("密文(base64编码):" + Convert.ToBase64String(testData) + Environment.NewLine);
  122. }
  123. catch (Exception ex)
  124. {
  125. return ex.Message;
  126. }
  127. }
  128. public string DecryptByPublicKey(string s, string key)
  129. {
  130. s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
  131. //非对称加密算法,加解密用
  132. IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
  133. //解密
  134. try
  135. {
  136. engine.Init(false, GetPublicKeyParameter(key));
  137. byte[] byteData = Convert.FromBase64String(s);
  138. string result = "";
  139. List<byte> cache = new List<byte>();
  140. for (int i = 0; i < byteData.Length; i++)
  141. {
  142. cache.Add(byteData[i]);
  143. if ((i + 1) % 128 == 0 || i + 1 == byteData.Length)
  144. {
  145. var ResultData = engine.ProcessBlock(cache.ToArray(), 0, cache.ToArray().Length);
  146. result += System.Text.Encoding.UTF8.GetString(ResultData);
  147. cache.Clear();
  148. }
  149. }
  150. return result;
  151. }
  152. catch (Exception ex)
  153. {
  154. return ex.Message;
  155. }
  156. }
  157. public string DecryptByPrivateKey(string s, string key)
  158. {
  159. s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
  160. //非对称加密算法,加解密用
  161. IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
  162. //解密
  163. try
  164. {
  165. engine.Init(false, GetPrivateKeyParameter2(key));
  166. byte[] byteData = Convert.FromBase64String(s);
  167. string result = "";
  168. List<byte> cache = new List<byte>();
  169. for (int i = 0; i < byteData.Length; i++)
  170. {
  171. cache.Add(byteData[i]);
  172. if ((i + 1) % 256 == 0 || i + 1 == byteData.Length)
  173. {
  174. var ResultData = engine.ProcessBlock(cache.ToArray(), 0, cache.ToArray().Length);
  175. result += System.Text.Encoding.UTF8.GetString(ResultData);
  176. cache.Clear();
  177. }
  178. }
  179. return result;
  180. }
  181. catch (Exception ex)
  182. {
  183. return ex.Message;
  184. }
  185. }
  186. }
  187. }