GMUtil.cs 5.6 KB

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