Skip to content

Commit

Permalink
新增Aes Des加解密
Browse files Browse the repository at this point in the history
将MD5移动位置
新增SHA1 SHA256哈希算法
  • Loading branch information
j4587698 committed Apr 15, 2024
1 parent 98a60d2 commit 4aa9651
Show file tree
Hide file tree
Showing 12 changed files with 313 additions and 8 deletions.
17 changes: 17 additions & 0 deletions Jx.Toolbox.Test/AesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Jx.Toolbox.Cryptography;

namespace Jx.Toolbox.Test;

public class AesTest
{
[Fact]
public void EncryptTest()
{
var key = "1234567890ABCDEF1234567890ABCDEF";
var iv = "1234567890ABCDEF1234567890ABCDEF";
var data = "test";
var encrypted = AesEncryption.Encrypt(data, key, iv);
var decrypted = AesEncryption.Decrypt(encrypted, key, iv);
Assert.Equal(data, decrypted);
}
}
17 changes: 17 additions & 0 deletions Jx.Toolbox.Test/DesTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Jx.Toolbox.Cryptography;

namespace Jx.Toolbox.Test;

public class DesTest
{
[Fact]
public void EncryptTest()
{
var key = "2B7E151628AED2A6";
var iv = "AABB09182736CCDD";
var data = "test";
var encrypted = DesEncryption.Encrypt(data, key, iv);
var decrypted = DesEncryption.Decrypt(encrypted, key, iv);
Assert.Equal(data, decrypted);
}
}
1 change: 1 addition & 0 deletions Jx.Toolbox.Test/MD5Test.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Jx.Toolbox.Cryptography;
using Jx.Toolbox.Hash;

namespace Jx.Toolbox.Test;

Expand Down
84 changes: 84 additions & 0 deletions Jx.Toolbox/Cryptography/AesEncryption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Jx.Toolbox.Extensions;
using Jx.Toolbox.Utils;

namespace Jx.Toolbox.Cryptography
{
/// <summary>
/// Aes加解密
/// </summary>
public class AesEncryption
{
public static string Encrypt(string plainText, string key, string iv, CipherMode cipherMode = CipherMode.CBC,
PaddingMode paddingMode = PaddingMode.PKCS7)
{
return Base64.Encode(Encrypt(plainText, key.HexStringToBytes(), iv.HexStringToBytes(), cipherMode, paddingMode));
}

/// <summary>
/// Aes加密
/// </summary>
/// <param name="plainText"></param>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <param name="cipherMode"></param>
/// <param name="paddingMode"></param>
/// <returns></returns>
public static byte[] Encrypt(string plainText, byte[] key, byte[] iv, CipherMode cipherMode = CipherMode.CBC,
PaddingMode paddingMode = PaddingMode.PKCS7)
{
using (var aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
aesAlg.Padding = paddingMode;
aesAlg.Mode = cipherMode;

ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
}
return msEncrypt.ToArray();
}
}
}

public static string Decrypt(string cipherText, string key, string iv, CipherMode cipherMode = CipherMode.CBC,
PaddingMode paddingMode = PaddingMode.PKCS7)
{
return Decrypt(Base64.Decode(cipherText), key.HexStringToBytes(), iv.HexStringToBytes(), cipherMode, paddingMode);
}

public static string Decrypt(byte[] cipherText, byte[] key, byte[] iv, CipherMode cipherMode = CipherMode.CBC,
PaddingMode paddingMode = PaddingMode.PKCS7)
{
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = key;
aesAlg.IV = iv;
aesAlg.Padding = paddingMode;
aesAlg.Mode = cipherMode;

ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
return srDecrypt.ReadToEnd();
}
}
}
}
}
}
}
71 changes: 71 additions & 0 deletions Jx.Toolbox/Cryptography/DesEncryption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System.IO;
using System.Security.Cryptography;
using Jx.Toolbox.Extensions;
using Jx.Toolbox.Utils;

namespace Jx.Toolbox.Cryptography
{
public class DesEncryption
{
public static string Encrypt(string plainText, string key, string iv, CipherMode cipherMode = CipherMode.CBC,
PaddingMode paddingMode = PaddingMode.PKCS7)
{
return Base64.Encode(Encrypt(plainText, key.HexStringToBytes(), iv.HexStringToBytes(), cipherMode, paddingMode));
}

public static byte[] Encrypt(string plainText, byte[] key, byte[] iv, CipherMode cipherMode = CipherMode.CBC,
PaddingMode paddingMode = PaddingMode.PKCS7)
{
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
des.Key = key;
des.IV = iv;
des.Mode = cipherMode;
des.Padding = paddingMode;

ICryptoTransform encryptor = des.CreateEncryptor(des.Key, des.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
}
return msEncrypt.ToArray();
}
}
}

public static string Decrypt(string cipherText, string key, string iv, CipherMode cipherMode = CipherMode.CBC,
PaddingMode paddingMode = PaddingMode.PKCS7)
{
return Decrypt(Base64.Decode(cipherText), key.HexStringToBytes(), iv.HexStringToBytes(), cipherMode, paddingMode);
}

public static string Decrypt(byte[] cipherText, byte[] key, byte[] iv, CipherMode cipherMode = CipherMode.CBC,
PaddingMode paddingMode = PaddingMode.PKCS7)
{
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
des.Key = key;
des.IV = iv;
des.Mode = cipherMode;
des.Padding = paddingMode;

ICryptoTransform decryptor = des.CreateDecryptor(des.Key, des.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
return srDecrypt.ReadToEnd();
}
}
}
}
}
}
}
20 changes: 20 additions & 0 deletions Jx.Toolbox/Extensions/ByteArrayExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace Jx.Toolbox.Extensions
{
/// <summary>
/// byte数组扩展
/// </summary>
public static class ByteArrayExtension
{
/// <summary>
/// byte数组转16进制字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public static string ToHexString(this byte[] bytes)
{
return BitConverter.ToString(bytes).Replace("-", "");
}
}
}
30 changes: 29 additions & 1 deletion Jx.Toolbox/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -100,5 +101,32 @@ public static string TrimEx(this string str)
{
return str == null ? string.Empty : str.Trim();
}

/// <summary>
/// Hex String转换位byte数组
/// </summary>
/// <param name="hex"></param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static byte[] HexStringToBytes(this string hex)
{
if (string.IsNullOrEmpty(hex))
{
return Array.Empty<byte>();
}

if (hex.Length % 2 != 0)
{
throw new ArgumentException("Invalid hex string length");
}

byte[] bytes = new byte[hex.Length / 2];
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}

return bytes;
}
}
}
2 changes: 1 addition & 1 deletion Jx.Toolbox/Cryptography/MD5.cs → Jx.Toolbox/Hash/MD5.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text;

namespace Jx.Toolbox.Cryptography
namespace Jx.Toolbox.Hash
{
public static class MD5
{
Expand Down
49 changes: 49 additions & 0 deletions Jx.Toolbox/Hash/ShaHash.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Security.Cryptography;
using System.Text;

namespace Jx.Toolbox.Hash
{
/// <summary>
/// SHA类
/// </summary>
public class ShaHash
{
/// <summary>
/// SHA1
/// </summary>
/// <param name="rawData"></param>
/// <returns></returns>
public static string ComputeSha1Hash(string rawData)
{
using (SHA1 sha1 = SHA1.Create())
{
byte[] bytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(rawData));
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
}
}

/// <summary>
/// SHA256
/// </summary>
/// <param name="rawData"></param>
/// <returns></returns>
public static string ComputeSha256Hash(string rawData)
{
using (SHA256 sha256 = SHA256.Create())
{
byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(rawData));
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}
return builder.ToString();
}
}
}
}
2 changes: 1 addition & 1 deletion Jx.Toolbox/Jx.Toolbox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageProjectUrl>https://github.com/j4587698/Jx.Toolbox</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>tool</PackageTags>
<PackageVersion>0.3.8</PackageVersion>
<PackageVersion>0.3.9</PackageVersion>
</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions Jx.Toolbox/Utils/Avatar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Jx.Toolbox.Cryptography;
using Jx.Toolbox.Extensions;
using Jx.Toolbox.Hash;

namespace Jx.Toolbox.Utils
{
Expand Down
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ StringExtension
"字符串".FirstLetterToLower(); // 字符串首字母小写
"字符串".FirstLetterToUpper(); // 字符串首字母大写
"字符串".TrimEx(); // 安全的Trim,如果字符串为null,则返回string.Empty
"字符串".HexStringToBytes(); // 将16进制字符串转换为byte数组
```

ObjectExtension
Expand Down Expand Up @@ -110,15 +111,31 @@ DictionaryExtension
Dictionary.TryRemove(键, out 值); // 尝试删除指定键的值,同时返回值
```

## 加密库
ByteArrayExtension
```
byte[].ToHexString(); // 将byte数组转换为16进制字符串
```
## Hash库
```
MD5.MD5String("要计算得字符串"); // 获取32位小写的MD5串
MD5.MD5StringWithSalt("要计算得字符串", "盐"); // 获取加盐后的32位小写MD5串
MD5.MD5String2("要计算得字符串"); // 获取2次MD5加密的32位小写MD5串
MD5.MD5String2WithSalt("要计算得字符串", "盐"); // 获取加盐后的2次MD5加密的32位小写MD5串
ShaHash.ComputeSha1Hash("要计算得字符串"); // 获取40位小写的Sha1串
ShaHash.ComputeSha256Hash("要计算得字符串"); // 获取64位小写的Sha256串
```
MD5.MD5String("要加密的字符串"); // 获取32位小写的MD5串
MD5.MD5StringWithSalt("要加密的字符串", "盐"); // 获取加盐后的32位小写MD5串
MD5.MD5String2("要加密的字符串"); // 获取2次MD5加密的32位小写MD5串
MD5.MD5String2WithSalt("要加密的字符串", "盐"); // 获取加盐后的2次MD5加密的32位小写MD5串

## 加密库
```
AesEncryption.Encrypt("要加密的字符串", "密钥", "向量"); // 使用Aes加密字符串
AesEncryption.Decrypt("要解密的字符串", "密钥", "向量"); // 使用Aes解密字符串
DesEncryption.Encrypt("要加密的字符串", "密钥", "向量"); // 使用Des加密字符串
DesEncryption.Decrypt("要解密的字符串", "密钥", "向量"); // 使用Des解密字符串
```


# Jx.Toolbox.HtmlTools
Html相关

Expand Down

0 comments on commit 4aa9651

Please sign in to comment.