SM4Util.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 = "";
  9. public string iv = "";
  10. public bool hexString = false;
  11. public string Encrypt_ECB(string plainText)
  12. {
  13. var ctx = new SM4_Context
  14. {
  15. isPadding = true,
  16. mode = SM4.SM4_ENCRYPT
  17. };
  18. byte[] keyBytes;
  19. if (hexString)
  20. {
  21. keyBytes = Hex.Decode(secretKey);
  22. }
  23. else
  24. {
  25. keyBytes = Encoding.ASCII.GetBytes(secretKey);
  26. }
  27. var sm4 = new SM4();
  28. sm4.Sm4_setkey_enc(ctx, keyBytes);
  29. byte[] encrypted = sm4.Sm4_crypt_ecb(ctx, Encoding.ASCII.GetBytes(plainText));
  30. string cipherText = Encoding.ASCII.GetString(Hex.Encode(encrypted));
  31. return cipherText;
  32. }
  33. public byte[] Encrypt_ECB(byte[] plainBytes, byte[] keyBytes)
  34. {
  35. var ctx = new SM4_Context
  36. {
  37. isPadding = false,
  38. mode = SM4.SM4_ENCRYPT
  39. };
  40. var sm4 = new SM4();
  41. sm4.Sm4_setkey_enc(ctx, keyBytes);
  42. byte[] encrypted = sm4.Sm4_crypt_ecb(ctx, plainBytes);
  43. return encrypted;
  44. //return Hex.Encode(encrypted);
  45. }
  46. public string Decrypt_ECB(string cipherText)
  47. {
  48. var ctx = new SM4_Context
  49. {
  50. isPadding = true,
  51. mode = SM4.SM4_DECRYPT
  52. };
  53. byte[] keyBytes;
  54. if (hexString)
  55. {
  56. keyBytes = Hex.Decode(secretKey);
  57. }
  58. else
  59. {
  60. keyBytes = Encoding.ASCII.GetBytes(secretKey);
  61. }
  62. var sm4 = new SM4();
  63. sm4.Sm4_setkey_dec(ctx, keyBytes);
  64. byte[] decrypted = sm4.Sm4_crypt_ecb(ctx, Hex.Decode(cipherText));
  65. return Encoding.ASCII.GetString(decrypted);
  66. }
  67. public string Encrypt_CBC(string plainText)
  68. {
  69. var ctx = new SM4_Context
  70. {
  71. isPadding = true,
  72. mode = SM4.SM4_ENCRYPT
  73. };
  74. byte[] keyBytes;
  75. byte[] ivBytes;
  76. if (hexString)
  77. {
  78. keyBytes = Hex.Decode(secretKey);
  79. ivBytes = Hex.Decode(iv);
  80. }
  81. else
  82. {
  83. keyBytes = Encoding.ASCII.GetBytes(secretKey);
  84. ivBytes = Encoding.ASCII.GetBytes(iv);
  85. }
  86. var sm4 = new SM4();
  87. sm4.Sm4_setkey_enc(ctx, keyBytes);
  88. byte[] encrypted = sm4.Sm4_crypt_cbc(ctx, ivBytes, Encoding.ASCII.GetBytes(plainText));
  89. string cipherText = Encoding.ASCII.GetString(Hex.Encode(encrypted));
  90. return cipherText;
  91. }
  92. public string Decrypt_CBC(string cipherText)
  93. {
  94. var ctx = new SM4_Context
  95. {
  96. isPadding = true,
  97. mode = SM4.SM4_DECRYPT
  98. };
  99. byte[] keyBytes;
  100. byte[] ivBytes;
  101. if (hexString)
  102. {
  103. keyBytes = Hex.Decode(secretKey);
  104. ivBytes = Hex.Decode(iv);
  105. }
  106. else
  107. {
  108. keyBytes = Encoding.ASCII.GetBytes(secretKey);
  109. ivBytes = Encoding.ASCII.GetBytes(iv);
  110. }
  111. var sm4 = new SM4();
  112. sm4.Sm4_setkey_dec(ctx, keyBytes);
  113. byte[] decrypted = sm4.Sm4_crypt_cbc(ctx, ivBytes, Hex.Decode(cipherText));
  114. return Encoding.ASCII.GetString(decrypted);
  115. }
  116. }