Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using static System.Net.Mime.MediaTypeNames;

namespace EasyTool.CodeCategory
namespace EasyTool
{
public static class AesUtil
{
Expand Down Expand Up @@ -96,13 +92,13 @@ private static bool IsLegalSize(string sk)
/// <param name="encoding">默认UTF8</param>
/// <returns>加密后的结果</returns>
/// <exception cref="ArgumentException"></exception>
public static string Encrypt(string text, string key, string iv ,CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null)
public static string Encrypt(string text, string key, string iv, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7, Encoding? encoding = null)
{
if (string.IsNullOrWhiteSpace(text))
return string.Empty; ;
if (!KeyIsLegalSize(key))
if (!KeyIsLegalSize(key))
throw new ArgumentException("不合规的秘钥,请确认秘钥为16 、24、 32位的字符");
if (!IvIsLegalSize(iv))
if (!IvIsLegalSize(iv))
throw new ArgumentException("不合规的iv,请确认iv为16位的字符");
encoding ??= Encoding.UTF8;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace EasyTool.CodeCategory
namespace EasyTool
{
/// <summary>
/// DES工具类
Expand Down Expand Up @@ -78,7 +76,7 @@ public static string Decrypt(string str, string sk, CipherMode cipher = CipherMo
/// <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)
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位的字符");
Expand Down
56 changes: 56 additions & 0 deletions EasyTool.Core/SecureCategory/MD5Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using EasyTool.ConvertCategory;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace EasyTool
{
/// <summary>
/// MD5加密工具类
/// </summary>
public class MD5Utils
{
/// <summary>
/// MD5加密
/// </summary>
/// <param name="source">加密字符串</param>
/// <param name="lowerCase">是否小写</param>
/// <returns></returns>
public static string Encrypt(string source, bool lowerCase = false)
{
if (string.IsNullOrWhiteSpace(source))
return string.Empty;

return Encrypt(Encoding.UTF8.GetBytes(source), lowerCase);
}

/// <summary>
/// MD5加密
/// </summary>
/// <param name="source">加密字节流</param>
/// <param name="lowerCase">是否小写</param>
/// <returns></returns>
public static string Encrypt(byte[] source, bool lowerCase = false)
{
if (source == null)
return string.Empty;

using var md5Hash = MD5.Create();
return md5Hash.ComputeHash(source).ToHex(lowerCase);
}

/// <summary>
/// MD5加密
/// </summary>
/// <param name="inputStream">流</param>
/// <param name="lowerCase">是否小写</param>
/// <returns></returns>
public static string Encrypt(Stream inputStream, bool lowerCase = false)
{
if (inputStream == null) return string.Empty;

using var md5Hash = MD5.Create();
return md5Hash.ComputeHash(inputStream).ToHex(lowerCase);
}
}
}