RsaBCHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //using Org.BouncyCastle.Crypto;
  2. //using Org.BouncyCastle.Crypto.Parameters;
  3. //using Org.BouncyCastle.Security;
  4. //using System;
  5. //using System.Collections.Generic;
  6. //using System.Linq;
  7. //using System.Security.Cryptography;
  8. //using System.Text;
  9. //using System.Threading.Tasks;
  10. ///// <summary>
  11. ///// Rsa非对称加密工具类
  12. ///// </summary>
  13. //public class RsaBCHelper
  14. //{
  15. // /// <summary>
  16. // /// 签名
  17. // /// </summary>
  18. // /// <param name="content">待签名字符串</param>
  19. // /// <param name="privateKey">私钥</param>
  20. // /// <returns>签名后字符串</returns>
  21. // public static string Sign(string content, string privateKey)
  22. // {
  23. // AsymmetricKeyParameter priKey = RsaKeyHelper.loadPrivateKeyPk1(privateKey);
  24. // ISigner sig = SignerUtilities.GetSigner("SHA256withRSA"); //其他算法 如SHA1withRSA
  25. // sig.Init(true, priKey);
  26. // var bytes = Encoding.Default.GetBytes(content);
  27. // sig.BlockUpdate(bytes, 0, bytes.Length);
  28. // byte[] signature = sig.GenerateSignature();
  29. // var signedString = Convert.ToBase64String(signature);
  30. // return signedString;
  31. // }
  32. // /// <summary>
  33. // /// 验签
  34. // /// </summary>
  35. // /// <param name="content">待验签字符串</param>
  36. // /// <param name="signedString">签名</param>
  37. // /// <param name="publicKey">公钥</param>
  38. // /// <returns>true(通过),false(不通过)</returns>
  39. // public static bool Verify(string content, string signedString, string publicKey)
  40. // {
  41. // AsymmetricKeyParameter pubKey = RsaKeyHelper.loadPublicKey(publicKey);
  42. // ISigner signer = SignerUtilities.GetSigner("SHA256withRSA"); //其他算法 如SHA1withRSA
  43. // signer.Init(false, pubKey);
  44. // var expectedSig = Convert.FromBase64String(signedString);
  45. // var msgBytes = Encoding.Default.GetBytes(content);
  46. // signer.BlockUpdate(msgBytes, 0, msgBytes.Length);
  47. // return signer.VerifySignature(expectedSig);
  48. // }
  49. // /// <summary>
  50. // /// 公钥加密
  51. // /// </summary>
  52. // /// <param name="resData">需要加密的字符串</param>
  53. // /// <param name="publicKey">公钥</param>
  54. // /// <returns>明文</returns>
  55. // public static string Encrypt(string resData, string publicKey)
  56. // {
  57. // try
  58. // {
  59. // AsymmetricKeyParameter pubKey = RsaKeyHelper.loadPublicKey(publicKey);
  60. // IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
  61. // cipher.Init(true, pubKey);//true表示加密
  62. // var data = Encoding.Default.GetBytes(resData.Trim());
  63. // byte[] encryptData = cipher.DoFinal(data);
  64. // return Convert.ToBase64String(encryptData);
  65. // }
  66. // catch (Exception)
  67. // {
  68. // throw new Exception("加密失败");
  69. // }
  70. // }
  71. // /// <summary>
  72. // /// 私钥解密
  73. // /// </summary>
  74. // /// <param name="resData">加密字符串</param>
  75. // /// <param name="privateKey">私钥</param>
  76. // /// <param name="input_charset">编码格式</param>
  77. // /// <returns>明文</returns>
  78. // public static string Decrypt(string resData, string privateKey)
  79. // {
  80. // try
  81. // {
  82. // AsymmetricKeyParameter priKey = RsaKeyHelper.loadPrivateKeyPk1(privateKey);
  83. // IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
  84. // cipher.Init(false, priKey);//false表示解密
  85. // var encryptData = Convert.FromBase64String(resData);
  86. // var decryptData = cipher.DoFinal(encryptData);
  87. // return Encoding.Default.GetString(decryptData);
  88. // }
  89. // catch (Exception)
  90. // {
  91. // throw new Exception("解密失败");
  92. // }
  93. // }
  94. // /// <summary>
  95. // /// 私钥加密
  96. // /// </summary>
  97. // /// <param name="resData">需要加密的字符串</param>
  98. // /// <param name="privateKey">私钥</param>
  99. // /// <returns>明文</returns>
  100. // public static string EncryptByPrivateKey(string resData, string privateKey)
  101. // {
  102. // try
  103. // {
  104. // AsymmetricKeyParameter priKey = RsaKeyHelper.loadPrivateKeyPk1(privateKey);
  105. // IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
  106. // cipher.Init(true, priKey);//true表示加密
  107. // var data = Encoding.Default.GetBytes(resData.Trim());
  108. // byte[] encryptData = cipher.DoFinal(data);
  109. // return Convert.ToBase64String(encryptData);
  110. // }
  111. // catch (Exception)
  112. // {
  113. // throw new Exception("加密失败");
  114. // }
  115. // }
  116. // /// <summary>
  117. // /// 公钥解密
  118. // /// </summary>
  119. // /// <param name="resData">加密字符串</param>
  120. // /// <param name="publicKey">公钥</param>
  121. // /// <returns>明文</returns>
  122. // public static string DecryptByPublicKey(string resData, string publicKey)
  123. // {
  124. // try
  125. // {
  126. // AsymmetricKeyParameter pubKey = RsaKeyHelper.loadPublicKey(publicKey);
  127. // IBufferedCipher cipher = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
  128. // cipher.Init(false, pubKey);//false表示解密
  129. // var encryptData = Convert.FromBase64String(resData);
  130. // var decryptData = cipher.DoFinal(encryptData);
  131. // return Encoding.Default.GetString(decryptData);
  132. // }
  133. // catch (Exception)
  134. // {
  135. // throw new Exception("解密失败");
  136. // }
  137. // }
  138. //}