平安校园
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.
 
 
 
 
 
 

171 lines
4.5 KiB

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