CryptogramUtil.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // 大名科技(天津)有限公司 版权所有
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动
  6. //
  7. // 任何基于本项目二次开发而产生的一切法律纠纷和责任,均与作者无关
  8. namespace Admin.NET.Core;
  9. public class CryptogramUtil
  10. {
  11. public static readonly bool StrongPassword = App.GetConfig<bool>("Cryptogram:StrongPassword"); // 是否开启密码强度验证
  12. public static readonly string PasswordStrengthValidation = App.GetConfig<string>("Cryptogram:PasswordStrengthValidation"); // 密码强度验证正则表达式
  13. public static readonly string PasswordStrengthValidationMsg = App.GetConfig<string>("Cryptogram:PasswordStrengthValidationMsg"); // 密码强度验证提示
  14. public static readonly string CryptoType = App.GetConfig<string>("Cryptogram:CryptoType"); // 加密类型
  15. public static readonly string PublicKey = App.GetConfig<string>("Cryptogram:PublicKey"); // 公钥
  16. public static readonly string PrivateKey = App.GetConfig<string>("Cryptogram:PrivateKey"); // 私钥
  17. public static readonly string SM4_key = "0123456789abcdeffedcba9876543210";
  18. public static readonly string SM4_iv = "595298c7c6fd271f0402f804c33d3f66";
  19. /// <summary>
  20. /// 加密
  21. /// </summary>
  22. /// <param name="plainText"></param>
  23. /// <returns></returns>
  24. public static string Encrypt(string plainText)
  25. {
  26. if (CryptoType == CryptogramEnum.MD5.ToString())
  27. {
  28. return MD5Encryption.Encrypt(plainText);
  29. }
  30. else if (CryptoType == CryptogramEnum.SM2.ToString())
  31. {
  32. return SM2Encrypt(plainText);
  33. }
  34. else if (CryptoType == CryptogramEnum.SM4.ToString())
  35. {
  36. return SM4EncryptECB(plainText);
  37. }
  38. return plainText;
  39. }
  40. /// <summary>
  41. /// 解密
  42. /// </summary>
  43. /// <param name="cipherText"></param>
  44. /// <returns></returns>
  45. public static string Decrypt(string cipherText)
  46. {
  47. if (CryptoType == CryptogramEnum.SM2.ToString())
  48. {
  49. return SM2Decrypt(cipherText);
  50. }
  51. else if (CryptoType == CryptogramEnum.SM4.ToString())
  52. {
  53. return SM4DecryptECB(cipherText);
  54. }
  55. return cipherText;
  56. }
  57. /// <summary>
  58. /// SM2加密
  59. /// </summary>
  60. /// <param name="plainText"></param>
  61. /// <returns></returns>
  62. public static string SM2Encrypt(string plainText)
  63. {
  64. return GMUtil.SM2Encrypt(PublicKey, plainText);
  65. }
  66. /// <summary>
  67. /// SM2解密
  68. /// </summary>
  69. /// <param name="cipherText"></param>
  70. /// <returns></returns>
  71. public static string SM2Decrypt(string cipherText)
  72. {
  73. return GMUtil.SM2Decrypt(PrivateKey, cipherText);
  74. }
  75. /// <summary>
  76. /// SM4加密(ECB)
  77. /// </summary>
  78. /// <param name="plainText"></param>
  79. /// <returns></returns>
  80. public static string SM4EncryptECB(string plainText)
  81. {
  82. return GMUtil.SM4EncryptECB(SM4_key, plainText);
  83. }
  84. /// <summary>
  85. /// SM4解密(ECB)
  86. /// </summary>
  87. /// <param name="cipherText"></param>
  88. /// <returns></returns>
  89. public static string SM4DecryptECB(string cipherText)
  90. {
  91. return GMUtil.SM4DecryptECB(SM4_key, cipherText);
  92. }
  93. /// <summary>
  94. /// SM4加密(CBC)
  95. /// </summary>
  96. /// <param name="plainText"></param>
  97. /// <returns></returns>
  98. public static string SM4EncryptCBC(string plainText)
  99. {
  100. return GMUtil.SM4EncryptCBC(SM4_key, SM4_iv, plainText);
  101. }
  102. /// <summary>
  103. /// SM4解密(CBC)
  104. /// </summary>
  105. /// <param name="cipherText"></param>
  106. /// <returns></returns>
  107. public static string SM4DecryptCBC(string cipherText)
  108. {
  109. return GMUtil.SM4DecryptCBC(SM4_key, SM4_iv, cipherText);
  110. }
  111. }