平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SM4Util.cs 4.5 KiB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. 
  2. //
  3. namespace SafeCampus.Core.Utils;
  4. //加密和解密结构相同,只不过,解密密钥是加密密钥的逆序
  5. /// <summary>
  6. /// Sm4算法
  7. /// 对标国际DES算法
  8. /// </summary>
  9. public class SM4Util
  10. {
  11. public SM4Util()
  12. {
  13. Key = "1814546261730461";//密钥长度必须为16字节。
  14. Iv = "0000000000000000";
  15. HexString = false;
  16. CryptoMode = Sm4CryptoEnum.ECB;
  17. }
  18. /// <summary>
  19. /// 数据
  20. /// </summary>
  21. public string Data { get; set; }
  22. /// <summary>
  23. /// 秘钥
  24. /// </summary>
  25. public string Key { get; }//不同的key,加密出来的数据不一样,所以此处设定好key以后,禁止修改
  26. /// <summary>
  27. /// 向量
  28. /// </summary>
  29. public string Iv { get; set; }
  30. /// <summary>
  31. /// 明文是否是十六进制
  32. /// </summary>
  33. public bool HexString { get; }//set;
  34. /// <summary>
  35. /// 加密模式(默认ECB)
  36. /// 统一改为ECB模式
  37. /// </summary>
  38. public Sm4CryptoEnum CryptoMode { get; }
  39. #region 加密
  40. public static string Encrypt(SM4Util entity)
  41. {
  42. return entity.CryptoMode == Sm4CryptoEnum.CBC ? EncryptCBC(entity) : EncryptECB(entity);
  43. }
  44. /// <summary>
  45. /// ECB加密
  46. /// </summary>
  47. /// <param name="entity"></param>
  48. /// <returns></returns>
  49. public static string EncryptECB(SM4Util entity)
  50. {
  51. var ctx = new Sm4Context
  52. {
  53. IsPadding = true
  54. };
  55. var keyBytes = entity.HexString ? Hex.Decode(entity.Key) : Encoding.Default.GetBytes(entity.Key);
  56. var sm4 = new SM4CryptoUtil();
  57. sm4.SetKeyEnc(ctx, keyBytes);
  58. var encrypted = sm4.Sm4CryptEcb(ctx, Encoding.Default.GetBytes(entity.Data));
  59. return Encoding.Default.GetString(Hex.Encode(encrypted));
  60. }
  61. /// <summary>
  62. /// CBC加密
  63. /// </summary>
  64. /// <param name="entity"></param>
  65. /// <returns></returns>
  66. public static string EncryptCBC(SM4Util entity)
  67. {
  68. var ctx = new Sm4Context
  69. {
  70. IsPadding = true
  71. };
  72. var keyBytes = entity.HexString ? Hex.Decode(entity.Key) : Encoding.Default.GetBytes(entity.Key);
  73. var ivBytes = entity.HexString ? Hex.Decode(entity.Iv) : Encoding.Default.GetBytes(entity.Iv);
  74. var sm4 = new SM4CryptoUtil();
  75. sm4.SetKeyEnc(ctx, keyBytes);
  76. var encrypted = sm4.Sm4CryptCbc(ctx, ivBytes, Encoding.Default.GetBytes(entity.Data));
  77. return Convert.ToBase64String(encrypted);
  78. }
  79. #endregion 加密
  80. #region 解密
  81. /// <summary>
  82. /// 解密
  83. /// </summary>
  84. /// <param name="entity"></param>
  85. /// <returns></returns>
  86. public static string Decrypt(SM4Util entity)
  87. {
  88. return entity.CryptoMode == Sm4CryptoEnum.CBC ? DecryptCBC(entity) : DecryptECB(entity);
  89. }
  90. /// <summary>
  91. /// ECB解密
  92. /// </summary>
  93. /// <param name="entity"></param>
  94. /// <returns></returns>
  95. public static string DecryptECB(SM4Util entity)
  96. {
  97. var ctx = new Sm4Context
  98. {
  99. IsPadding = true,
  100. Mode = 0
  101. };
  102. var keyBytes = entity.HexString ? Hex.Decode(entity.Key) : Encoding.Default.GetBytes(entity.Key);
  103. var sm4 = new SM4CryptoUtil();
  104. sm4.Sm4SetKeyDec(ctx, keyBytes);
  105. var decrypted = sm4.Sm4CryptEcb(ctx, Hex.Decode(entity.Data));
  106. return Encoding.Default.GetString(decrypted);
  107. }
  108. /// <summary>
  109. /// CBC解密
  110. /// </summary>
  111. /// <param name="entity"></param>
  112. /// <returns></returns>
  113. public static string DecryptCBC(SM4Util entity)
  114. {
  115. var ctx = new Sm4Context
  116. {
  117. IsPadding = true,
  118. Mode = 0
  119. };
  120. var keyBytes = entity.HexString ? Hex.Decode(entity.Key) : Encoding.Default.GetBytes(entity.Key);
  121. var ivBytes = entity.HexString ? Hex.Decode(entity.Iv) : Encoding.Default.GetBytes(entity.Iv);
  122. var sm4 = new SM4CryptoUtil();
  123. sm4.Sm4SetKeyDec(ctx, keyBytes);
  124. var decrypted = sm4.Sm4CryptCbc(ctx, ivBytes, Convert.FromBase64String(entity.Data));
  125. return Encoding.Default.GetString(decrypted);
  126. }
  127. #endregion 解密
  128. /// <summary>
  129. /// 加密类型
  130. /// </summary>
  131. public enum Sm4CryptoEnum
  132. {
  133. /// <summary>
  134. /// ECB(电码本模式)
  135. /// </summary>
  136. [Description("ECB模式")]
  137. ECB = 0,
  138. /// <summary>
  139. /// CBC(密码分组链接模式)
  140. /// </summary>
  141. [Description("CBC模式")]
  142. CBC = 1
  143. }
  144. }