RSAForJava.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. public string EncryptByPrivateKey(string s, string key)
  94. {
  95. //非对称加密算法,加解密用
  96. IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
  97. //加密
  98. try
  99. {
  100. engine.Init(true, GetPrivateKeyParameter(key));
  101. byte[] byteData = System.Text.Encoding.UTF8.GetBytes(s);
  102. var ResultData = engine.ProcessBlock(byteData, 0, byteData.Length);
  103. return Convert.ToBase64String(ResultData);
  104. //Console.WriteLine("密文(base64编码):" + Convert.ToBase64String(testData) + Environment.NewLine);
  105. }
  106. catch (Exception ex)
  107. {
  108. return ex.Message;
  109. }
  110. }
  111. public string DecryptByPublicKey(string s, string key)
  112. {
  113. s = s.Replace("\r", "").Replace("\n", "").Replace(" ", "");
  114. //非对称加密算法,加解密用
  115. IAsymmetricBlockCipher engine = new Pkcs1Encoding(new RsaEngine());
  116. //解密
  117. try
  118. {
  119. engine.Init(false, GetPublicKeyParameter(key));
  120. byte[] byteData = Convert.FromBase64String(s);
  121. string result = "";
  122. List<byte> cache = new List<byte>();
  123. for (int i = 0; i < byteData.Length; i++)
  124. {
  125. cache.Add(byteData[i]);
  126. if ((i + 1) % 256 == 0 || i + 1 == byteData.Length)
  127. {
  128. var ResultData = engine.ProcessBlock(cache.ToArray(), 0, cache.ToArray().Length);
  129. result += System.Text.Encoding.UTF8.GetString(ResultData);
  130. cache.Clear();
  131. }
  132. }
  133. return result;
  134. }
  135. catch (Exception ex)
  136. {
  137. return ex.Message;
  138. }
  139. }
  140. }
  141. }