RSACrypt.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.Collections.Generic;
  2. using System.Security.Cryptography;
  3. using System.Text;
  4. using XC.Framework.Security.RSAUtil;
  5. namespace TEAMModelOS.SDK.Helper.Security.RSACrypt
  6. {
  7. /// <summary>
  8. /// RSA加密解密
  9. /// https://github.com/stulzq/RSAUtil
  10. /// </summary>
  11. public class RSACrypt
  12. {
  13. private readonly RsaPkcs1Util _RsaUtil;
  14. private readonly Encoding _encoding;
  15. /// <summary>
  16. /// 获得私钥和公钥
  17. /// [0]=privateKey 私钥
  18. /// [1]=publicKey 公钥
  19. /// </summary>
  20. /// <returns></returns>
  21. public static List<string> GetKey()
  22. {
  23. return RsaKeyGenerator.Pkcs1Key(2048, true);
  24. }
  25. /// <summary>
  26. /// 实例化
  27. /// </summary>
  28. /// <param name="encoding">编码类型</param>
  29. /// <param name="privateKey">私钥</param>
  30. /// <param name="publicKey">公钥</param>
  31. public RSACrypt(string privateKey, string publicKey)
  32. {
  33. _encoding = Encoding.UTF8;
  34. _RsaUtil = new RsaPkcs1Util(_encoding, publicKey, privateKey, 1024);
  35. }
  36. /// <summary>
  37. /// 加密
  38. /// </summary>
  39. /// <param name="code">加密代码</param>
  40. /// <returns></returns>
  41. public string Encrypt(string code)
  42. {
  43. return _RsaUtil.Encrypt(code, RSAEncryptionPadding.Pkcs1);
  44. }
  45. /// <summary>
  46. /// 解密
  47. /// </summary>
  48. /// <param name="code">解密代码</param>
  49. /// <returns></returns>
  50. public string Decrypt(string code)
  51. {
  52. return _RsaUtil.Decrypt(code, RSAEncryptionPadding.Pkcs1);
  53. }
  54. }
  55. }