diff --git a/EasyTool.Core/CodeCategory/DesUtil.cs b/EasyTool.Core/CodeCategory/DesUtil.cs index 2f9332c..94e350f 100644 --- a/EasyTool.Core/CodeCategory/DesUtil.cs +++ b/EasyTool.Core/CodeCategory/DesUtil.cs @@ -7,6 +7,9 @@ namespace EasyTool.CodeCategory { + /// + /// DES工具类 + /// public static class DesUtil { /// @@ -20,7 +23,7 @@ public static class DesUtil /// public static string Encrypt(string str, string sk, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null) { - if (string.IsNullOrEmpty(str)) return string.Empty; + if (string.IsNullOrWhiteSpace(str)) return string.Empty; if (!IsLegalSize(sk)) throw new ArgumentException("不合规的秘钥,请确认秘钥为8位的字符"); encoding ??= Encoding.UTF8; byte[] keyBytes = encoding.GetBytes(sk).ToArray(); @@ -47,7 +50,7 @@ public static string Encrypt(string str, string sk, CipherMode cipher = CipherMo /// public static string Decrypt(string str, string sk, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null) { - if (string.IsNullOrEmpty(str)) return string.Empty; + if (string.IsNullOrWhiteSpace(str)) return string.Empty; if (!IsLegalSize(sk)) throw new ArgumentException("不合规的秘钥,请确认秘钥为8位的字符"); encoding ??= Encoding.UTF8; byte[] keyBytes = encoding.GetBytes(sk).ToArray(); @@ -62,6 +65,67 @@ public static string Decrypt(string str, string sk, CipherMode cipher = CipherMo return encoding.GetString(resultArray); } + + + /// + /// des 加密 + /// + /// 待加密字符串 + /// 秘钥 + /// 向量Iv + /// 默认ECB + /// 默认PKCS7 + /// 默认UTF8 + /// + /// + public static string Encrypt(string str, string sk,string iv, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null) + { + if (string.IsNullOrWhiteSpace(str)) return string.Empty; + if (!IsLegalSize(sk)) throw new ArgumentException("不合规的秘钥,请确认秘钥为8位的字符"); + if (!IsLegalSize(iv)) throw new ArgumentException("不合规的IV,请确认IV为8位的字符"); + encoding ??= Encoding.UTF8; + byte[] keyBytes = encoding.GetBytes(sk).ToArray(); + byte[] toEncrypt = encoding.GetBytes(str); + var des = DES.Create(); + des.Mode = cipher; + des.Padding = padding; + des.Key = keyBytes; + des.IV = keyBytes; + + ICryptoTransform cTransform = des.CreateEncryptor(); + var resultArray = cTransform.TransformFinalBlock(toEncrypt, 0, toEncrypt.Length); + return Convert.ToBase64String(resultArray); + } + + /// + /// Des 解密 + /// + /// 待解密字符串 + /// 秘钥 + /// 向量Iv + /// 默认ECB + /// 默认PKCS7 + /// 默认UTF8 + /// + /// + public static string Decrypt(string str, string sk, string iv, CipherMode cipher = CipherMode.ECB, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null) + { + if (string.IsNullOrWhiteSpace(str)) return string.Empty; + if (!IsLegalSize(sk)) throw new ArgumentException("不合规的秘钥,请确认秘钥为8位的字符"); + if (!IsLegalSize(iv)) throw new ArgumentException("不合规的IV,请确认IV为8位的字符"); + encoding ??= Encoding.UTF8; + byte[] keyBytes = encoding.GetBytes(sk).ToArray(); + byte[] toDecrypt = Convert.FromBase64String(str); + var des = DES.Create(); + des.Mode = cipher; + des.Padding = padding; + des.Key = keyBytes; + des.IV = keyBytes; + ICryptoTransform cTransform = des.CreateDecryptor(); + var resultArray = cTransform.TransformFinalBlock(toDecrypt, 0, toDecrypt.Length); + return encoding.GetString(resultArray); + } + private static bool IsLegalSize(string sk) { if (!string.IsNullOrEmpty(sk) && sk.Length == 8) @@ -69,5 +133,6 @@ private static bool IsLegalSize(string sk) return false; } + } }