SM4Util.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using Org.BouncyCastle.Utilities.Encoders;
  2. namespace Admin.NET.Core;
  3. /// <summary>
  4. /// SM4工具类
  5. /// </summary>
  6. public class SM4Util
  7. {
  8. public string secretKey = "1814546261730461"; // 长度必须为16字节
  9. public string iv = "0000000000000000";
  10. public bool hexString = false;
  11. public bool forJavascript = false;
  12. public string Encrypt_ECB(string plainText)
  13. {
  14. var ctx = new SM4_Context
  15. {
  16. isPadding = true,
  17. mode = SM4.SM4_ENCRYPT
  18. };
  19. byte[] keyBytes;
  20. if (hexString)
  21. {
  22. keyBytes = Hex.Decode(secretKey);
  23. }
  24. else
  25. {
  26. keyBytes = Encoding.ASCII.GetBytes(secretKey);
  27. }
  28. var sm4 = new SM4();
  29. sm4.FOR_JAVASCRIPT = forJavascript;
  30. sm4.Sm4_setkey_enc(ctx, keyBytes);
  31. byte[] encrypted = sm4.Sm4_crypt_ecb(ctx, Encoding.ASCII.GetBytes(plainText));
  32. string cipherText = Encoding.ASCII.GetString(Hex.Encode(encrypted));
  33. return cipherText;
  34. }
  35. public byte[] Encrypt_ECB(byte[] plainBytes, byte[] keyBytes)
  36. {
  37. var ctx = new SM4_Context
  38. {
  39. isPadding = false,
  40. mode = SM4.SM4_ENCRYPT
  41. };
  42. var sm4 = new SM4();
  43. sm4.FOR_JAVASCRIPT = forJavascript;
  44. sm4.Sm4_setkey_enc(ctx, keyBytes);
  45. byte[] encrypted = sm4.Sm4_crypt_ecb(ctx, plainBytes);
  46. return encrypted;
  47. //return Hex.Encode(encrypted);
  48. }
  49. public string Decrypt_ECB(string cipherText)
  50. {
  51. var ctx = new SM4_Context
  52. {
  53. isPadding = true,
  54. mode = SM4.SM4_DECRYPT
  55. };
  56. byte[] keyBytes;
  57. if (hexString)
  58. {
  59. keyBytes = Hex.Decode(secretKey);
  60. }
  61. else
  62. {
  63. keyBytes = Encoding.ASCII.GetBytes(secretKey);
  64. }
  65. var sm4 = new SM4();
  66. sm4.FOR_JAVASCRIPT = forJavascript;
  67. sm4.Sm4_setkey_dec(ctx, keyBytes);
  68. byte[] decrypted = sm4.Sm4_crypt_ecb(ctx, Hex.Decode(cipherText));
  69. return Encoding.ASCII.GetString(decrypted);
  70. }
  71. public string Encrypt_CBC(string plainText)
  72. {
  73. var ctx = new SM4_Context
  74. {
  75. isPadding = true,
  76. mode = SM4.SM4_ENCRYPT
  77. };
  78. byte[] keyBytes;
  79. byte[] ivBytes;
  80. if (hexString)
  81. {
  82. keyBytes = Hex.Decode(secretKey);
  83. ivBytes = Hex.Decode(iv);
  84. }
  85. else
  86. {
  87. keyBytes = Encoding.ASCII.GetBytes(secretKey);
  88. ivBytes = Encoding.ASCII.GetBytes(iv);
  89. }
  90. var sm4 = new SM4();
  91. sm4.FOR_JAVASCRIPT = forJavascript;
  92. sm4.Sm4_setkey_enc(ctx, keyBytes);
  93. byte[] encrypted = sm4.Sm4_crypt_cbc(ctx, ivBytes, Encoding.ASCII.GetBytes(plainText));
  94. string cipherText = Encoding.ASCII.GetString(Hex.Encode(encrypted));
  95. return cipherText;
  96. }
  97. public string Decrypt_CBC(string cipherText)
  98. {
  99. var ctx = new SM4_Context
  100. {
  101. isPadding = true,
  102. mode = SM4.SM4_DECRYPT
  103. };
  104. byte[] keyBytes;
  105. byte[] ivBytes;
  106. if (hexString)
  107. {
  108. keyBytes = Hex.Decode(secretKey);
  109. ivBytes = Hex.Decode(iv);
  110. }
  111. else
  112. {
  113. keyBytes = Encoding.ASCII.GetBytes(secretKey);
  114. ivBytes = Encoding.ASCII.GetBytes(iv);
  115. }
  116. var sm4 = new SM4();
  117. sm4.FOR_JAVASCRIPT = forJavascript;
  118. sm4.Sm4_setkey_dec(ctx, keyBytes);
  119. byte[] decrypted = sm4.Sm4_crypt_cbc(ctx, ivBytes, Hex.Decode(cipherText));
  120. return Encoding.ASCII.GetString(decrypted);
  121. }
  122. }