Skip to content

Commit

Permalink
Merge pull request #46 from Sheldon-NULL/DesUpdate
Browse files Browse the repository at this point in the history
Add  添加DES加密对独立IV的支持
  • Loading branch information
786744873 authored Oct 10, 2023
2 parents 1b8b5b9 + 6746430 commit c865396
Showing 1 changed file with 67 additions and 2 deletions.
69 changes: 67 additions & 2 deletions EasyTool.Core/CodeCategory/DesUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

namespace EasyTool.CodeCategory
{
/// <summary>
/// DES工具类
/// </summary>
public static class DesUtil
{
/// <summary>
Expand All @@ -20,7 +23,7 @@ public static class DesUtil
/// <returns></returns>
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();
Expand All @@ -47,7 +50,7 @@ public static string Encrypt(string str, string sk, CipherMode cipher = CipherMo
/// <returns></returns>
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();
Expand All @@ -62,12 +65,74 @@ public static string Decrypt(string str, string sk, CipherMode cipher = CipherMo
return encoding.GetString(resultArray);
}



/// <summary>
/// des 加密
/// </summary>
/// <param name="str">待加密字符串</param>
/// <param name="sk">秘钥</param>
/// <param name="iv">向量Iv</param>
/// <param name="cipher">默认ECB</param>
/// <param name="padding">默认PKCS7</param>
/// <param name="encoding">默认UTF8</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
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);
}

/// <summary>
/// Des 解密
/// </summary>
/// <param name="str">待解密字符串</param>
/// <param name="sk">秘钥</param>
/// <param name="iv">向量Iv</param>
/// <param name="cipher">默认ECB</param>
/// <param name="padding">默认PKCS7</param>
/// <param name="encoding">默认UTF8</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
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)
return true;

return false;
}

}
}

0 comments on commit c865396

Please sign in to comment.