CryptogramUtil.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. return plainText;
  24. }
  25. /// <summary>
  26. /// 解密
  27. /// </summary>
  28. /// <param name="cipherText"></param>
  29. /// <returns></returns>
  30. public static string Decrypt(string cipherText)
  31. {
  32. if (CryptoType == CryptogramEnum.SM2.ToString())
  33. {
  34. return SM2Decrypt(cipherText);
  35. }
  36. return cipherText;
  37. }
  38. /// <summary>
  39. /// SM2加密
  40. /// </summary>
  41. /// <param name="plainText"></param>
  42. /// <returns></returns>
  43. public static string SM2Encrypt(string plainText)
  44. {
  45. byte[] sourceData = Encoding.Default.GetBytes(plainText);
  46. return SM2Util.Encrypt(Hex.Decode(PublicKey), sourceData);
  47. }
  48. /// <summary>
  49. /// SM2解密
  50. /// </summary>
  51. /// <param name="cipherText"></param>
  52. /// <returns></returns>
  53. public static string SM2Decrypt(string cipherText)
  54. {
  55. return Encoding.Default.GetString(SM2Util.Decrypt(Hex.Decode(PrivateKey), Hex.Decode(cipherText)));
  56. }
  57. /// <summary>
  58. /// SM4加密(ECB)
  59. /// </summary>
  60. /// <param name="plainText"></param>
  61. /// <returns></returns>
  62. public static string SM4EncryptECB(string plainText)
  63. {
  64. var sm4 = new SM4Util();
  65. return sm4.Encrypt_ECB(plainText);
  66. }
  67. /// <summary>
  68. /// SM4解密(ECB)
  69. /// </summary>
  70. /// <param name="cipherText"></param>
  71. /// <returns></returns>
  72. public static string SM4DecryptECB(string cipherText)
  73. {
  74. var sm4 = new SM4Util();
  75. return sm4.Decrypt_ECB(cipherText);
  76. }
  77. /// <summary>
  78. /// SM4加密(CBC)
  79. /// </summary>
  80. /// <param name="plainText"></param>
  81. /// <returns></returns>
  82. public static string SM4EncryptCBC(string plainText)
  83. {
  84. var sm4 = new SM4Util();
  85. return sm4.Encrypt_CBC(plainText);
  86. }
  87. /// <summary>
  88. /// SM4解密(CBC)
  89. /// </summary>
  90. /// <param name="cipherText"></param>
  91. /// <returns></returns>
  92. public static string SM4DecryptCBC(string cipherText)
  93. {
  94. var sm4 = new SM4Util();
  95. return sm4.Decrypt_CBC(cipherText);
  96. }
  97. }