平安校园
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

SM2Util.cs 1.1 KiB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using System.Configuration;
  2. namespace SafeAuthActivate.Sm;
  3. /// <summary>
  4. /// SM2加密解密
  5. /// </summary>
  6. public class SM2Util
  7. {
  8. /// <summary>
  9. /// 公钥
  10. /// </summary>
  11. public static string PublicKey = ConfigurationManager.AppSettings["SM2PublicKey"];
  12. /// <summary>
  13. /// 私钥
  14. /// </summary>
  15. public static string PrivateKey = ConfigurationManager.AppSettings["SM2PrivateKey"];
  16. /// <summary>
  17. /// 公钥加密明文
  18. /// </summary>
  19. /// <param name="plainText">明文</param>
  20. /// <returns>密文</returns>
  21. public static string Encrypt(string plainText)
  22. {
  23. return SafeAuthActivate.Sm.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 SafeAuthActivate.Sm.SM2CryptoUtil.Decrypt(PrivateKey, cipherText);
  34. }
  35. }