CryptogramUtil.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using Org.BouncyCastle.Utilities.Encoders;
  2. namespace Admin.NET.Core;
  3. public class CryptogramUtil
  4. {
  5. public static readonly string CryptoType = App.GetConfig<string>("Cryptogram:CryptoType"); // 加密类型
  6. public static readonly string PublicKey = App.GetConfig<string>("Cryptogram:PublicKey"); // 公钥
  7. public static readonly string PrivateKey = App.GetConfig<string>("Cryptogram:PrivateKey"); // 私钥
  8. /// <summary>
  9. /// 加密
  10. /// </summary>
  11. /// <param name="plainText"></param>
  12. /// <returns></returns>
  13. public static string Encrypt(string plainText)
  14. {
  15. if (CryptoType == CryptogramEnum.MD5.ToString())
  16. {
  17. return MD5Encryption.Encrypt(plainText);
  18. }
  19. else if (CryptoType == CryptogramEnum.SM2.ToString())
  20. {
  21. return SM2Encrypt(plainText);
  22. }
  23. else if (CryptoType == CryptogramEnum.SM4.ToString())
  24. {
  25. return SM4EncryptECB(plainText);
  26. }
  27. return plainText;
  28. }
  29. /// <summary>
  30. /// 解密
  31. /// </summary>
  32. /// <param name="cipherText"></param>
  33. /// <returns></returns>
  34. public static string Decrypt(string cipherText)
  35. {
  36. if (CryptoType == CryptogramEnum.SM2.ToString())
  37. {
  38. return SM2Decrypt(cipherText);
  39. }
  40. else if (CryptoType == CryptogramEnum.SM4.ToString())
  41. {
  42. return SM4DecryptECB(cipherText);
  43. }
  44. return cipherText;
  45. }
  46. /// <summary>
  47. /// SM2加密
  48. /// </summary>
  49. /// <param name="plainText"></param>
  50. /// <returns></returns>
  51. public static string SM2Encrypt(string plainText)
  52. {
  53. byte[] sourceData = Encoding.Default.GetBytes(plainText);
  54. return SM2Util.Encrypt(Hex.Decode(PublicKey), sourceData);
  55. }
  56. /// <summary>
  57. /// SM2解密
  58. /// </summary>
  59. /// <param name="cipherText"></param>
  60. /// <returns></returns>
  61. public static string SM2Decrypt(string cipherText)
  62. {
  63. return Encoding.Default.GetString(SM2Util.Decrypt(Hex.Decode(PrivateKey), Hex.Decode(cipherText)));
  64. }
  65. /// <summary>
  66. /// SM4加密(ECB)
  67. /// </summary>
  68. /// <param name="plainText"></param>
  69. /// <returns></returns>
  70. public static string SM4EncryptECB(string plainText)
  71. {
  72. var sm4 = new SM4Util();
  73. return sm4.Encrypt_ECB(plainText);
  74. }
  75. /// <summary>
  76. /// SM4解密(ECB)
  77. /// </summary>
  78. /// <param name="cipherText"></param>
  79. /// <returns></returns>
  80. public static string SM4DecryptECB(string cipherText)
  81. {
  82. var sm4 = new SM4Util();
  83. return sm4.Decrypt_ECB(cipherText);
  84. }
  85. /// <summary>
  86. /// SM4加密(CBC)
  87. /// </summary>
  88. /// <param name="plainText"></param>
  89. /// <returns></returns>
  90. public static string SM4EncryptCBC(string plainText)
  91. {
  92. var sm4 = new SM4Util();
  93. return sm4.Encrypt_CBC(plainText);
  94. }
  95. /// <summary>
  96. /// SM4解密(CBC)
  97. /// </summary>
  98. /// <param name="cipherText"></param>
  99. /// <returns></returns>
  100. public static string SM4DecryptCBC(string cipherText)
  101. {
  102. var sm4 = new SM4Util();
  103. return sm4.Decrypt_CBC(cipherText);
  104. }
  105. }