平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SM2Util.cs 1.0 KiB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 
  2. //
  3. namespace SafeCampus.Core.Utils;
  4. /// <summary>
  5. /// SM2加密解密
  6. /// </summary>
  7. public class SM2Util
  8. {
  9. /// <summary>
  10. /// 公钥
  11. /// </summary>
  12. public static string PublicKey = App.GetConfig<string>("Cryptogram:SM2:PublicKey");
  13. /// <summary>
  14. /// 私钥
  15. /// </summary>
  16. public static string PrivateKey = App.GetConfig<string>("Cryptogram:SM2:PrivateKey");
  17. /// <summary>
  18. /// 公钥加密明文
  19. /// </summary>
  20. /// <param name="plainText">明文</param>
  21. /// <returns>密文</returns>
  22. public static string Encrypt(string plainText)
  23. {
  24. return SM2CryptoUtil.Encrypt(PublicKey, plainText);
  25. }
  26. /// <summary>
  27. /// 私钥解密密文
  28. /// </summary>
  29. /// <param name="cipherText">密文</param>
  30. /// <returns>明文</returns>
  31. public static string Decrypt(string cipherText)
  32. {
  33. if (!cipherText.StartsWith("04")) cipherText = "04" + cipherText;//如果不是04开头加上04
  34. return SM2CryptoUtil.Decrypt(PrivateKey, cipherText);
  35. }
  36. }