平安校园
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.
 
 
 
 
 
 

49 lines
1.0 KiB

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