GMUtil.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
  2. //
  3. // 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
  4. using Org.BouncyCastle.Crypto;
  5. using Org.BouncyCastle.Math;
  6. using Org.BouncyCastle.Utilities.Encoders;
  7. namespace Admin.NET.Core;
  8. /// <summary>
  9. /// GM工具类
  10. /// </summary>
  11. public class GMUtil
  12. {
  13. /// <summary>
  14. /// SM2加密
  15. /// </summary>
  16. /// <param name="publicKeyHex"></param>
  17. /// <param name="data_string"></param>
  18. /// <returns></returns>
  19. public static string SM2Encrypt(string publicKeyHex, string data_string)
  20. {
  21. // 如果是130位公钥,.NET使用的话,把开头的04截取掉
  22. if (publicKeyHex.Length == 130)
  23. {
  24. publicKeyHex = publicKeyHex.Substring(2, 128);
  25. }
  26. // 公钥X,前64位
  27. string x = publicKeyHex.Substring(0, 64);
  28. // 公钥Y,后64位
  29. string y = publicKeyHex.Substring(64);
  30. // 获取公钥对象
  31. AsymmetricKeyParameter publicKey1 = GM.GetPublickeyFromXY(new BigInteger(x, 16), new BigInteger(y, 16));
  32. // Sm2Encrypt: C1C3C2
  33. // Sm2EncryptOld: C1C2C3
  34. byte[] digestByte = GM.Sm2Encrypt(Encoding.UTF8.GetBytes(data_string), publicKey1);
  35. string strSM2 = Hex.ToHexString(digestByte);
  36. return strSM2;
  37. }
  38. /// <summary>
  39. /// SM2解密
  40. /// </summary>
  41. /// <param name="privateKey_string"></param>
  42. /// <param name="encryptedData_string"></param>
  43. /// <returns></returns>
  44. public static string SM2Decrypt(string privateKey_string, string encryptedData_string)
  45. {
  46. if (!encryptedData_string.StartsWith("04"))
  47. encryptedData_string = "04" + encryptedData_string;
  48. BigInteger d = new(privateKey_string, 16);
  49. // 先拿到私钥对象,用ECPrivateKeyParameters 或 AsymmetricKeyParameter 都可以
  50. // ECPrivateKeyParameters bcecPrivateKey = GmUtil.GetPrivatekeyFromD(d);
  51. AsymmetricKeyParameter bcecPrivateKey = GM.GetPrivatekeyFromD(d);
  52. byte[] byToDecrypt = Hex.Decode(encryptedData_string);
  53. byte[] byDecrypted = GM.Sm2Decrypt(byToDecrypt, bcecPrivateKey);
  54. string strDecrypted = Encoding.UTF8.GetString(byDecrypted);
  55. return strDecrypted;
  56. }
  57. /// <summary>
  58. /// SM4加密(ECB)
  59. /// </summary>
  60. /// <param name="key_string"></param>
  61. /// <param name="plainText"></param>
  62. /// <returns></returns>
  63. public static string SM4EncryptECB(string key_string, string plainText)
  64. {
  65. byte[] key = Hex.Decode(key_string);
  66. byte[] bs = GM.Sm4EncryptECB(key, Encoding.UTF8.GetBytes(plainText), GM.SM4_CBC_PKCS7PADDING);//NoPadding 的情况下需要校验数据长度是16的倍数. 使用 HandleSm4Padding 处理
  67. return Hex.ToHexString(bs);
  68. }
  69. /// <summary>
  70. /// SM4解密(ECB)
  71. /// </summary>
  72. /// <param name="key_string"></param>
  73. /// <param name="cipherText"></param>
  74. /// <returns></returns>
  75. public static string SM4DecryptECB(string key_string, string cipherText)
  76. {
  77. byte[] key = Hex.Decode(key_string);
  78. byte[] bs = GM.Sm4DecryptECB(key, Hex.Decode(cipherText), GM.SM4_CBC_PKCS7PADDING);
  79. return Encoding.UTF8.GetString(bs);
  80. }
  81. /// <summary>
  82. /// SM4加密(CBC)
  83. /// </summary>
  84. /// <param name="key_string"></param>
  85. /// <param name="iv_string"></param>
  86. /// <param name="plainText"></param>
  87. /// <returns></returns>
  88. public static string SM4EncryptCBC(string key_string, string iv_string, string plainText)
  89. {
  90. byte[] key = Hex.Decode(key_string);
  91. byte[] iv = Hex.Decode(iv_string);
  92. byte[] bs = GM.Sm4EncryptCBC(key, Encoding.UTF8.GetBytes(plainText), iv, GM.SM4_CBC_PKCS7PADDING);
  93. return Hex.ToHexString(bs);
  94. }
  95. /// <summary>
  96. /// SM4解密(CBC)
  97. /// </summary>
  98. /// <param name="key_string"></param>
  99. /// <param name="iv_string"></param>
  100. /// <param name="cipherText"></param>
  101. /// <returns></returns>
  102. public static string SM4DecryptCBC(string key_string, string iv_string, string cipherText)
  103. {
  104. byte[] key = Hex.Decode(key_string);
  105. byte[] iv = Hex.Decode(iv_string);
  106. byte[] bs = GM.Sm4DecryptCBC(key, Hex.Decode(cipherText), iv, GM.SM4_CBC_PKCS7PADDING);
  107. return Encoding.UTF8.GetString(bs);
  108. }
  109. /// <summary>
  110. /// 补足 16 进制字符串的 0 字符,返回不带 0x 的16进制字符串
  111. /// </summary>
  112. /// <param name="input"></param>
  113. /// <param name="mode">1表示加密,0表示解密</param>
  114. /// <returns></returns>
  115. private static byte[] HandleSm4Padding(byte[] input, int mode)
  116. {
  117. if (input == null)
  118. {
  119. return null;
  120. }
  121. byte[] ret = (byte[])null;
  122. if (mode == 1)
  123. {
  124. int p = 16 - input.Length % 16;
  125. ret = new byte[input.Length + p];
  126. Array.Copy(input, 0, ret, 0, input.Length);
  127. for (int i = 0; i < p; i++)
  128. {
  129. ret[input.Length + i] = (byte)p;
  130. }
  131. }
  132. else
  133. {
  134. int p = input[input.Length - 1];
  135. ret = new byte[input.Length - p];
  136. Array.Copy(input, 0, ret, 0, input.Length - p);
  137. }
  138. return ret;
  139. }
  140. }