CryptogramUtil.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. namespace Admin.NET.Core;
  5. public class CryptogramUtil
  6. {
  7. public static readonly bool StrongPassword = App.GetConfig<bool>("Cryptogram:StrongPassword"); // 是否开启密码强度验证
  8. public static readonly string PasswordStrengthValidation = App.GetConfig<string>("Cryptogram:PasswordStrengthValidation"); // 密码强度验证正则表达式
  9. public static readonly string PasswordStrengthValidationMsg = App.GetConfig<string>("Cryptogram:PasswordStrengthValidationMsg"); // 密码强度验证提示
  10. public static readonly string CryptoType = App.GetConfig<string>("Cryptogram:CryptoType"); // 加密类型
  11. public static readonly string PublicKey = App.GetConfig<string>("Cryptogram:PublicKey"); // 公钥
  12. public static readonly string PrivateKey = App.GetConfig<string>("Cryptogram:PrivateKey"); // 私钥
  13. public static readonly string SM4_key = "0123456789abcdeffedcba9876543210";
  14. public static readonly string SM4_iv = "595298c7c6fd271f0402f804c33d3f66";
  15. /// <summary>
  16. /// 加密
  17. /// </summary>
  18. /// <param name="plainText"></param>
  19. /// <returns></returns>
  20. public static string Encrypt(string plainText)
  21. {
  22. if (CryptoType == CryptogramEnum.MD5.ToString())
  23. {
  24. return MD5Encryption.Encrypt(plainText);
  25. }
  26. else if (CryptoType == CryptogramEnum.SM2.ToString())
  27. {
  28. return SM2Encrypt(plainText);
  29. }
  30. else if (CryptoType == CryptogramEnum.SM4.ToString())
  31. {
  32. return SM4EncryptECB(plainText);
  33. }
  34. return plainText;
  35. }
  36. /// <summary>
  37. /// 解密
  38. /// </summary>
  39. /// <param name="cipherText"></param>
  40. /// <returns></returns>
  41. public static string Decrypt(string cipherText)
  42. {
  43. if (CryptoType == CryptogramEnum.SM2.ToString())
  44. {
  45. return SM2Decrypt(cipherText);
  46. }
  47. else if (CryptoType == CryptogramEnum.SM4.ToString())
  48. {
  49. return SM4DecryptECB(cipherText);
  50. }
  51. return cipherText;
  52. }
  53. /// <summary>
  54. /// SM2加密
  55. /// </summary>
  56. /// <param name="plainText"></param>
  57. /// <returns></returns>
  58. public static string SM2Encrypt(string plainText)
  59. {
  60. return GMUtil.SM2Encrypt(PublicKey, plainText);
  61. }
  62. /// <summary>
  63. /// SM2解密
  64. /// </summary>
  65. /// <param name="cipherText"></param>
  66. /// <returns></returns>
  67. public static string SM2Decrypt(string cipherText)
  68. {
  69. return GMUtil.SM2Decrypt(PrivateKey, cipherText);
  70. }
  71. /// <summary>
  72. /// SM4加密(ECB)
  73. /// </summary>
  74. /// <param name="plainText"></param>
  75. /// <returns></returns>
  76. public static string SM4EncryptECB(string plainText)
  77. {
  78. return GMUtil.SM4EncryptECB(SM4_key, plainText);
  79. }
  80. /// <summary>
  81. /// SM4解密(ECB)
  82. /// </summary>
  83. /// <param name="cipherText"></param>
  84. /// <returns></returns>
  85. public static string SM4DecryptECB(string cipherText)
  86. {
  87. return GMUtil.SM4DecryptECB(SM4_key, cipherText);
  88. }
  89. /// <summary>
  90. /// SM4加密(CBC)
  91. /// </summary>
  92. /// <param name="plainText"></param>
  93. /// <returns></returns>
  94. public static string SM4EncryptCBC(string plainText)
  95. {
  96. return GMUtil.SM4EncryptCBC(SM4_key, SM4_iv, plainText);
  97. }
  98. /// <summary>
  99. /// SM4解密(CBC)
  100. /// </summary>
  101. /// <param name="cipherText"></param>
  102. /// <returns></returns>
  103. public static string SM4DecryptCBC(string cipherText)
  104. {
  105. return GMUtil.SM4DecryptCBC(SM4_key, SM4_iv, cipherText);
  106. }
  107. }