CryptogramUtil.cs 4.1 KB

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