SM4Util.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // 麻省理工学院许可证
  2. //
  3. // 版权所有 (c) 2021-2023 zuohuaijun,大名科技(天津)有限公司 联系电话/微信:18020030720 QQ:515096995
  4. //
  5. // 特此免费授予获得本软件的任何人以处理本软件的权利,但须遵守以下条件:在所有副本或重要部分的软件中必须包括上述版权声明和本许可声明。
  6. //
  7. // 软件按“原样”提供,不提供任何形式的明示或暗示的保证,包括但不限于对适销性、适用性和非侵权的保证。
  8. // 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是因合同、侵权或其他方式引起的,与软件或其使用或其他交易有关。
  9. using Org.BouncyCastle.Utilities.Encoders;
  10. namespace Admin.NET.Core;
  11. /// <summary>
  12. /// SM4工具类
  13. /// </summary>
  14. public class SM4Util
  15. {
  16. public string secretKey = "1814546261730461"; // 长度必须为16字节
  17. public string iv = "0000000000000000";
  18. public bool hexString = false;
  19. public bool forJavascript = false;
  20. public string Encrypt_ECB(string plainText)
  21. {
  22. var ctx = new SM4_Context
  23. {
  24. isPadding = true,
  25. mode = SM4.SM4_ENCRYPT
  26. };
  27. byte[] keyBytes;
  28. if (hexString)
  29. {
  30. keyBytes = Hex.Decode(secretKey);
  31. }
  32. else
  33. {
  34. keyBytes = Encoding.ASCII.GetBytes(secretKey);
  35. }
  36. var sm4 = new SM4();
  37. sm4.FOR_JAVASCRIPT = forJavascript;
  38. sm4.Sm4_setkey_enc(ctx, keyBytes);
  39. byte[] encrypted = sm4.Sm4_crypt_ecb(ctx, Encoding.ASCII.GetBytes(plainText));
  40. string cipherText = Encoding.ASCII.GetString(Hex.Encode(encrypted));
  41. return cipherText;
  42. }
  43. public byte[] Encrypt_ECB(byte[] plainBytes, byte[] keyBytes)
  44. {
  45. var ctx = new SM4_Context
  46. {
  47. isPadding = false,
  48. mode = SM4.SM4_ENCRYPT
  49. };
  50. var sm4 = new SM4();
  51. sm4.FOR_JAVASCRIPT = forJavascript;
  52. sm4.Sm4_setkey_enc(ctx, keyBytes);
  53. byte[] encrypted = sm4.Sm4_crypt_ecb(ctx, plainBytes);
  54. return encrypted;
  55. //return Hex.Encode(encrypted);
  56. }
  57. public string Decrypt_ECB(string cipherText)
  58. {
  59. var ctx = new SM4_Context
  60. {
  61. isPadding = true,
  62. mode = SM4.SM4_DECRYPT
  63. };
  64. byte[] keyBytes;
  65. if (hexString)
  66. {
  67. keyBytes = Hex.Decode(secretKey);
  68. }
  69. else
  70. {
  71. keyBytes = Encoding.ASCII.GetBytes(secretKey);
  72. }
  73. var sm4 = new SM4();
  74. sm4.FOR_JAVASCRIPT = forJavascript;
  75. sm4.Sm4_setkey_dec(ctx, keyBytes);
  76. byte[] decrypted = sm4.Sm4_crypt_ecb(ctx, Hex.Decode(cipherText));
  77. return Encoding.ASCII.GetString(decrypted);
  78. }
  79. public string Encrypt_CBC(string plainText)
  80. {
  81. var ctx = new SM4_Context
  82. {
  83. isPadding = true,
  84. mode = SM4.SM4_ENCRYPT
  85. };
  86. byte[] keyBytes;
  87. byte[] ivBytes;
  88. if (hexString)
  89. {
  90. keyBytes = Hex.Decode(secretKey);
  91. ivBytes = Hex.Decode(iv);
  92. }
  93. else
  94. {
  95. keyBytes = Encoding.ASCII.GetBytes(secretKey);
  96. ivBytes = Encoding.ASCII.GetBytes(iv);
  97. }
  98. var sm4 = new SM4();
  99. sm4.FOR_JAVASCRIPT = forJavascript;
  100. sm4.Sm4_setkey_enc(ctx, keyBytes);
  101. byte[] encrypted = sm4.Sm4_crypt_cbc(ctx, ivBytes, Encoding.ASCII.GetBytes(plainText));
  102. string cipherText = Encoding.ASCII.GetString(Hex.Encode(encrypted));
  103. return cipherText;
  104. }
  105. public string Decrypt_CBC(string cipherText)
  106. {
  107. var ctx = new SM4_Context
  108. {
  109. isPadding = true,
  110. mode = SM4.SM4_DECRYPT
  111. };
  112. byte[] keyBytes;
  113. byte[] ivBytes;
  114. if (hexString)
  115. {
  116. keyBytes = Hex.Decode(secretKey);
  117. ivBytes = Hex.Decode(iv);
  118. }
  119. else
  120. {
  121. keyBytes = Encoding.ASCII.GetBytes(secretKey);
  122. ivBytes = Encoding.ASCII.GetBytes(iv);
  123. }
  124. var sm4 = new SM4();
  125. sm4.FOR_JAVASCRIPT = forJavascript;
  126. sm4.Sm4_setkey_dec(ctx, keyBytes);
  127. byte[] decrypted = sm4.Sm4_crypt_cbc(ctx, ivBytes, Hex.Decode(cipherText));
  128. return Encoding.ASCII.GetString(decrypted);
  129. }
  130. }