Skip to content

Commit

Permalink
Merge pull request #193 from EasilyNET/dev
Browse files Browse the repository at this point in the history
♻️ 重构代码
  • Loading branch information
joesdu authored Aug 3, 2023
2 parents 449177d + 55a5bea commit d7b0975
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 108 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

EasilyNET Packages

- AES,DES,SHA,RC4,TripleDES,RSA,SM2,SM3,SM4
- AES,DES,RC4,TripleDES,RSA,SM2,SM3,SM4
- mongodb based distributed locks
- property injection
- simple qrcode
Expand Down Expand Up @@ -34,7 +34,7 @@ EasilyNET Packages
| [EasilyNET.AutoDependencyInjection](https://www.nuget.org/packages/EasilyNET.AutoDependencyInjection) | ![Nuget](https://img.shields.io/nuget/v/EasilyNET.AutoDependencyInjection) | ![Nuget](https://img.shields.io/nuget/dt/EasilyNET.AutoDependencyInjection) | 模块化自动注入服务,属性注入,特性和接口注入等多种方式 |
| [EasilyNET.Images](https://www.nuget.org/packages/EasilyNET.Images) | ![Nuget](https://img.shields.io/nuget/v/EasilyNET.Images) | ![Nuget](https://img.shields.io/nuget/dt/EasilyNET.Images) | 一些涉及到图形的工具包,目前仅有 QrCode |
| [EasilyNET.RabbitBus.AspNetCore](https://www.nuget.org/packages/EasilyNET.RabbitBus.AspNetCore) | ![Nuget](https://img.shields.io/nuget/v/EasilyNET.RabbitBus.AspNetCore) | ![Nuget](https://img.shields.io/nuget/dt/EasilyNET.RabbitBus.AspNetCore) | 基于 RabbitMQ 的消息总线处理方案 |
| [EasilyNET.Security](https://www.nuget.org/packages/EasilyNET.Security) | ![Nuget](https://img.shields.io/nuget/v/EasilyNET.Security) | ![Nuget](https://img.shields.io/nuget/dt/EasilyNET.Security) | AES,DES,SHA,RC4,TripleDES,RSA,SM2,SM3,SM4 |
| [EasilyNET.Security](https://www.nuget.org/packages/EasilyNET.Security) | ![Nuget](https://img.shields.io/nuget/v/EasilyNET.Security) | ![Nuget](https://img.shields.io/nuget/dt/EasilyNET.Security) | AES,DES,RC4,TripleDES,RSA,SM2,SM3,SM4 |

#### Mongo

Expand Down
9 changes: 8 additions & 1 deletion src/EasilyNET.Security/AES/AESModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
/// <summary>
/// AES加密的密钥算法模式
/// </summary>
internal enum AESModel
public enum AESModel
{
/// <summary>
/// AES128
/// </summary>
AES128,

/// <summary>
/// AES256
/// </summary>
AES256
}
60 changes: 5 additions & 55 deletions src/EasilyNET.Security/AES/AesCrypt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace EasilyNET.Security;

/// <summary>
/// AES加密解密
/// AES加密解密(使用本库加密仅能用本库解密)
/// </summary>
public static class AesCrypt
{
Expand Down Expand Up @@ -51,14 +51,13 @@ private static Tuple<byte[], byte[]> GetAesKey(string pwd, AESModel model = AESM
/// <summary>
/// AES加密
/// </summary>
/// <param name="txt">需要加密的内容</param>
/// <param name="content">需要加密的内容</param>
/// <param name="pwd">密钥</param>
/// <param name="model">加密模式</param>
/// <returns></returns>
private static byte[] Encrypt(string txt, string pwd, AESModel model)
public static byte[] Encrypt(byte[] content, string pwd, AESModel model)
{
var (Key, IV) = GetAesKey(pwd, model);
var content = Encoding.UTF8.GetBytes(txt);
using var aes = Aes.Create();
aes.Key = Key;
aes.IV = IV;
Expand All @@ -75,64 +74,15 @@ private static byte[] Encrypt(string txt, string pwd, AESModel model)
/// <param name="pwd"></param>
/// <param name="model"></param>
/// <returns></returns>
private static byte[] Decrypt(string secret, string pwd, AESModel model)
public static byte[] Decrypt(byte[] secret, string pwd, AESModel model)
{
var (Key, IV) = GetAesKey(pwd, model);
var toEncryptArray = Convert.FromBase64String(secret);
using var aes = Aes.Create();
aes.Key = Key;
aes.IV = IV;
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
var cTransform = aes.CreateDecryptor();
return cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
}

/// <summary>
/// 使用AES加密字符串
/// </summary>
/// <param name="content">加密内容</param>
/// <param name="pwd">秘钥</param>
/// <returns>Base64字符串结果</returns>
public static string Aes256CbcEncrypt(string content, string pwd)
{
var resultArray = Encrypt(content, pwd, AESModel.AES256);
return resultArray.ToBase64();
}

/// <summary>
/// 使用AES解密字符串
/// </summary>
/// <param name="secret">内容</param>
/// <param name="pwd">秘钥</param>
/// <returns>UTF8解密结果</returns>
public static string Aes256CbcDecrypt(string secret, string pwd)
{
var resultArray = Decrypt(secret, pwd, AESModel.AES256);
return Encoding.UTF8.GetString(resultArray);
}

/// <summary>
/// 使用AES加密字符串
/// </summary>
/// <param name="content">加密内容</param>
/// <param name="pwd">秘钥</param>
/// <returns>Base64字符串结果</returns>
public static string Aes128CbcEncrypt(string content, string pwd)
{
var resultArray = Encrypt(content, pwd, AESModel.AES128);
return resultArray.ToBase64();
}

/// <summary>
/// 使用AES解密字符串
/// </summary>
/// <param name="secret">内容</param>
/// <param name="pwd">秘钥</param>
/// <returns>UTF8解密结果</returns>
public static string Aes128CbcDecrypt(string secret, string pwd)
{
var resultArray = Decrypt(secret, pwd, AESModel.AES128);
return Encoding.UTF8.GetString(resultArray);
return cTransform.TransformFinalBlock(secret, 0, secret.Length);
}
}
30 changes: 14 additions & 16 deletions src/EasilyNET.Security/DES/DesCrypt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace EasilyNET.Security;

/// <summary>
/// DES加密解密
/// DES加密解密(使用本库加密仅能用本库解密)
/// </summary>
// ReSharper disable once UnusedType.Global
public static class DesCrypt
Expand All @@ -33,25 +33,24 @@ private static Tuple<byte[], byte[]> GetEesKey(string pwd)
}

/// <summary>
/// DES加密字符串
/// DES加密
/// </summary>
/// <param name="content">待加密的字符串</param>
/// <param name="content">待加密的数据</param>
/// <param name="pwd">加密密钥</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt(string content, string pwd)
/// <returns>加密后的数据</returns>
public static byte[] Encrypt(byte[] content, string pwd)
{
var (Key, IV) = GetEesKey(pwd);
var inputByteArray = Encoding.UTF8.GetBytes(content);
var des = DES.Create();
des.Key = Key;
des.IV = IV;
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.PKCS7;
var ms = new MemoryStream();
var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
using var ms = new MemoryStream();
using var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(content, 0, content.Length);
cs.FlushFinalBlock();
return ms.ToArray().ToBase64();
return ms.ToArray();
}

/// <summary>
Expand All @@ -60,19 +59,18 @@ public static string Encrypt(string content, string pwd)
/// <param name="secret">待解密的字符串</param>
/// <param name="pwd">解密密钥</param>
/// <returns>解密后的字符串</returns>
public static string Decrypt(string secret, string pwd)
public static byte[] Decrypt(byte[] secret, string pwd)
{
var (Key, IV) = GetEesKey(pwd);
var inputByteArray = secret.FromBase64();
var des = DES.Create();
des.Key = Key;
des.IV = IV;
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.PKCS7;
var ms = new MemoryStream();
var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
using var ms = new MemoryStream();
using var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(secret, 0, secret.Length);
cs.FlushFinalBlock();
return Encoding.UTF8.GetString(ms.ToArray());
return ms.ToArray();
}
}
32 changes: 15 additions & 17 deletions src/EasilyNET.Security/DES/TripleDES.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace EasilyNET.Security;

/// <summary>
/// TripleDES加密解密
/// TripleDES加密解密(使用本库加密仅能用本库解密)
/// </summary>
public static class TripleDES
{
Expand All @@ -35,44 +35,42 @@ private static Tuple<byte[], byte[]> GetEesKey(string pwd)
/// <summary>
/// 使用给定密钥加密
/// </summary>
/// <param name="content">待加密的字符串</param>
/// <param name="content">待加密的数据</param>
/// <param name="pwd">加密密钥</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt(string content, string pwd)
/// <returns>加密后的数据</returns>
public static byte[] Encrypt(byte[] content, string pwd)
{
var (Key, IV) = GetEesKey(pwd);
var inputByteArray = Encoding.UTF8.GetBytes(content);
var des = System.Security.Cryptography.TripleDES.Create();
des.Key = Key;
des.IV = IV;
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.PKCS7;
var ms = new MemoryStream();
var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
using var ms = new MemoryStream();
using var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(content, 0, content.Length);
cs.FlushFinalBlock();
return ms.ToArray().ToBase64();
return ms.ToArray();
}

/// <summary>
/// 使用给定密钥解密数据
/// </summary>
/// <param name="secret">待解密的字符串</param>
/// <param name="secret">待解密的数据</param>
/// <param name="pwd">解密密钥</param>
/// <returns>解密后的字符串</returns>
public static string Decrypt(string secret, string pwd)
/// <returns>解密后的数据</returns>
public static byte[] Decrypt(byte[] secret, string pwd)
{
var (Key, IV) = GetEesKey(pwd);
var inputByteArray = secret.FromBase64();
var des = System.Security.Cryptography.TripleDES.Create();
des.Key = Key;
des.IV = IV;
des.Mode = CipherMode.CBC;
des.Padding = PaddingMode.PKCS7;
var ms = new MemoryStream();
var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
using var ms = new MemoryStream();
using var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(secret, 0, secret.Length);
cs.FlushFinalBlock();
return Encoding.UTF8.GetString(ms.ToArray());
return ms.ToArray();
}
}
25 changes: 8 additions & 17 deletions src/EasilyNET.Security/RC4/Rc4Crypt.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using EasilyNET.Core.Misc;
using System.Text;
using System.Text;

// ReSharper disable SuggestBaseTypeForParameter
// ReSharper disable ParameterTypeCanBeEnumerable.Local
Expand All @@ -17,26 +16,18 @@ public static class Rc4Crypt
/// <summary>
/// RC4加密
/// </summary>
/// <param name="key"></param>
/// <param name="data"></param>
/// <param name="data">待加密数据</param>
/// <param name="key">密钥</param>
/// <returns></returns>
public static string Encrypt(string data, string key)
{
var encrypted = Encrypt(Encoding.UTF8.GetBytes(data), Encoding.UTF8.GetBytes(key));
return encrypted.ToBase64();
}
public static byte[] Encrypt(byte[] data, string key) => Encrypt(data, Encoding.UTF8.GetBytes(key));

/// <summary>
/// RC4解密
/// </summary>
/// <param name="key"></param>
/// <param name="data"></param>
/// <param name="data">待解密数据</param>
/// <param name="key">密钥</param>
/// <returns></returns>
public static string Decrypt(string data, string key)
{
var decrypted = Encrypt(data.FromBase64(), Encoding.UTF8.GetBytes(key));
return Encoding.UTF8.GetString(decrypted);
}
public static byte[] Decrypt(byte[] data, string key) => Encrypt(data, Encoding.UTF8.GetBytes(key));

private static byte[] EncryptInit(byte[] key)
{
Expand All @@ -63,5 +54,5 @@ private static byte[] Encrypt(byte[] data, byte[] key)
}).ToArray();
}

private static void Swap(IList<byte> s, int i, int j) => (s[i], s[j]) = (s[j], s[i]);
private static void Swap(byte[] s, int i, int j) => (s[i], s[j]) = (s[j], s[i]);
}

0 comments on commit d7b0975

Please sign in to comment.