Skip to content

Commit

Permalink
添加生成随机Key和Iv得方法
Browse files Browse the repository at this point in the history
  • Loading branch information
j4587698 committed Apr 16, 2024
1 parent 4aa9651 commit b30c7e1
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 3 deletions.
44 changes: 43 additions & 1 deletion Jx.Toolbox/Cryptography/AesEncryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,41 @@ namespace Jx.Toolbox.Cryptography
/// <summary>
/// Aes加解密
/// </summary>
public class AesEncryption
public static class AesEncryption
{
/// <summary>
/// 生成密钥
/// </summary>
/// <returns></returns>
public static string CreateKey()
{
using (Aes aes = Aes.Create())
{
return aes.Key.ToHexString();
}
}

/// <summary>
/// 生成IV
/// </summary>
/// <returns></returns>
public static string CreateIv()
{
using (Aes aes = Aes.Create())
{
return aes.IV.ToHexString();
}
}

/// <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 string Encrypt(string plainText, string key, string iv, CipherMode cipherMode = CipherMode.CBC,
PaddingMode paddingMode = PaddingMode.PKCS7)
{
Expand Down Expand Up @@ -51,6 +84,15 @@ public static byte[] Encrypt(string plainText, byte[] key, byte[] iv, CipherMode
}
}

/// <summary>
/// Aes解密
/// </summary>
/// <param name="cipherText"></param>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <param name="cipherMode"></param>
/// <param name="paddingMode"></param>
/// <returns></returns>
public static string Decrypt(string cipherText, string key, string iv, CipherMode cipherMode = CipherMode.CBC,
PaddingMode paddingMode = PaddingMode.PKCS7)
{
Expand Down
65 changes: 64 additions & 1 deletion Jx.Toolbox/Cryptography/DesEncryption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,59 @@

namespace Jx.Toolbox.Cryptography
{
public class DesEncryption
/// <summary>
/// Des加解密
/// </summary>
public static class DesEncryption
{
/// <summary>
/// 生成密钥
/// </summary>
/// <returns></returns>
public static string CreateKey()
{
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
return des.Key.ToHexString();
}
}

/// <summary>
/// 生成IV
/// </summary>
/// <returns></returns>
public static string CreateIv()
{
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
{
return des.IV.ToHexString();
}
}

/// <summary>
/// Des加密
/// </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 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>
/// Des加密
/// </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)
{
Expand All @@ -38,12 +83,30 @@ public static byte[] Encrypt(string plainText, byte[] key, byte[] iv, CipherMode
}
}

/// <summary>
/// Des解密
/// </summary>
/// <param name="cipherText"></param>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <param name="cipherMode"></param>
/// <param name="paddingMode"></param>
/// <returns></returns>
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);
}

/// <summary>
/// Des解密
/// </summary>
/// <param name="cipherText"></param>
/// <param name="key"></param>
/// <param name="iv"></param>
/// <param name="cipherMode"></param>
/// <param name="paddingMode"></param>
/// <returns></returns>
public static string Decrypt(byte[] cipherText, byte[] key, byte[] iv, CipherMode cipherMode = CipherMode.CBC,
PaddingMode paddingMode = PaddingMode.PKCS7)
{
Expand Down
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.9</PackageVersion>
<PackageVersion>0.3.10</PackageVersion>
</PropertyGroup>

</Project>
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,18 @@ ShaHash.ComputeSha256Hash("要计算得字符串"); // 获取64位小写的Sha25
```

## 加密库
AesEncryption
```
AesEncryption.CreateKey(); // 创建一个随机的Aes密钥
AesEncryption.CreateIV(); // 创建一个随机的Aes向量
AesEncryption.Encrypt("要加密的字符串", "密钥", "向量"); // 使用Aes加密字符串
AesEncryption.Decrypt("要解密的字符串", "密钥", "向量"); // 使用Aes解密字符串
```

DesEncryption
```
DesEncryption.CreateKey(); // 创建一个随机的Des密钥
DesEncryption.CreateIV(); // 创建一个随机的Des向量
DesEncryption.Encrypt("要加密的字符串", "密钥", "向量"); // 使用Des加密字符串
DesEncryption.Decrypt("要解密的字符串", "密钥", "向量"); // 使用Des解密字符串
```
Expand Down

0 comments on commit b30c7e1

Please sign in to comment.