GMUtil.cs 5.6 KB

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