From 2733612bcecacff8d825b4a08b453397dd3c9b16 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Mon, 5 Sep 2022 12:36:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0DES=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E5=B9=B6=E5=AE=8C=E6=88=90=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Encryption/EncryptionTest.cs | 77 +++++++++++++++++ Falcon.SugarApi/Encryption/DESConfig.cs | 32 +++++++ Falcon.SugarApi/Encryption/DESProvider.cs | 83 +++++++++++++++++++ .../Encryption/EncryptionProviderRegirst.cs | 38 +++++++++ Falcon.SugarApi/Encryption/IDESEncryption.cs | 7 ++ Falcon.SugarApi/Encryption/IEncryption.cs | 28 +++++++ 6 files changed, 265 insertions(+) create mode 100644 Falcon.SugarApi.Test/Encryption/EncryptionTest.cs create mode 100644 Falcon.SugarApi/Encryption/DESConfig.cs create mode 100644 Falcon.SugarApi/Encryption/DESProvider.cs create mode 100644 Falcon.SugarApi/Encryption/EncryptionProviderRegirst.cs create mode 100644 Falcon.SugarApi/Encryption/IDESEncryption.cs create mode 100644 Falcon.SugarApi/Encryption/IEncryption.cs diff --git a/Falcon.SugarApi.Test/Encryption/EncryptionTest.cs b/Falcon.SugarApi.Test/Encryption/EncryptionTest.cs new file mode 100644 index 0000000..1508456 --- /dev/null +++ b/Falcon.SugarApi.Test/Encryption/EncryptionTest.cs @@ -0,0 +1,77 @@ +using Falcon.SugarApi.Encryption; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Text; + +namespace Falcon.SugarApi.Test.Encryption +{ + /// + /// RSA测试类 + /// + [TestClass] + public class EncryptionTest + { + /// + /// 测试公钥、秘钥生成,加密解密 + /// + [TestMethod] + public void RSATest() { + var config = new RSAConfig() { + BlockSize = 1024 + }; + IRSAEncryption r = new RSAProvider(config); + var k = r.GenerateKey(); + + var p = new RSAProvider(config); + var mingw = " /// \r\n /// 测试公钥、秘钥生成,加密解密\r\n /// \r\n [TestMethod]\r\n public void EncryptionRSA() {\r\n var config = new RSAConfig();\r\n var r = new RSAProvider(config);"; + var miwen = p.Encrypt(mingw, k.PublicKey); + + var p1 = new RSAProvider(config); + var mingw1 = p1.Decrypt(miwen, k.PrivateKey); + + Assert.AreEqual(mingw, mingw1, "解密后明文不同"); + } + + /// + /// 测试公钥、秘钥生成,加密解密 + /// + [TestMethod("DES测试")] + public void DESTest() { + var config = new DESConfig() { }; + IDESEncryption r = new DESProvider(config); + var k = r.GenerateKey(); + + //var bs = Encoding.UTF8.GetBytes(mingw); + //var a1 = Convert.ToBase64String(bs); + //var a2 = Convert.FromBase64String(a1); + //Assert.AreEqual(bs.Length, a2.Length); + + for (int i = 0; i < 1000; i++) { + var mingw = GenerateStr(); + Console.WriteLine(mingw); + var p = new DESProvider(config); + var miwen = p.Encrypt(k, mingw); + + var p1 = new DESProvider(config); + var mingw1 = p1.Decrypt(k, miwen); + + Assert.AreEqual(mingw, mingw1, "解密后明文不同"); + + } + } + /// + /// 生成随机长度,由字符表内字符组成的字符串 + /// + /// + private string GenerateStr() { + var chars = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789,.!/*\"; + var r = new Random(); + var len = r.Next(100); + var sb = new StringBuilder(len); + for (int i = 0; i < len; i++) { + sb.Append(chars[r.Next(0, chars.Length)]); + } + return sb.ToString(); + } + } +} diff --git a/Falcon.SugarApi/Encryption/DESConfig.cs b/Falcon.SugarApi/Encryption/DESConfig.cs new file mode 100644 index 0000000..7924c10 --- /dev/null +++ b/Falcon.SugarApi/Encryption/DESConfig.cs @@ -0,0 +1,32 @@ +using Microsoft.Extensions.Options; +using System.Security.Cryptography; +using System.Text; + +namespace Falcon.SugarApi.Encryption +{ + /// + /// DES加密配置 + /// + public class DESConfig:IOptions + { + /// + /// 加解密编码方式 + /// + public Encoding Encoding { get; set; } = Encoding.UTF8; + /// + /// 填充方式 + /// + public PaddingMode Padding { get; set; } = PaddingMode.PKCS7; + /// + /// 加密模式 + /// + public CipherMode Mode { get; set; } = CipherMode.CBC; + /// + /// 加密用的IV + /// + public byte[] IV { get; set; } = { 123, 221, 221, 111, 4, 6, 7, 22 }; + + /// + public DESConfig Value => this; + } +} diff --git a/Falcon.SugarApi/Encryption/DESProvider.cs b/Falcon.SugarApi/Encryption/DESProvider.cs new file mode 100644 index 0000000..bc1a12e --- /dev/null +++ b/Falcon.SugarApi/Encryption/DESProvider.cs @@ -0,0 +1,83 @@ +using System; +using System.IO; +using System.Security.Cryptography; +using System.Text; + +namespace Falcon.SugarApi.Encryption +{ + /// + /// DES对称加密 + /// + public class DESProvider : IEncryption, IDESEncryption + { + /// + /// 配置文件 + /// + public DESConfig Config { get; set; } + + /// + /// 通过提供配置创建DES提供程序 + /// + /// + public DESProvider(DESConfig config) { + Config = config; + } + + /// + /// 创建加解密提供程序 + /// + /// 加密用的key + /// 加解密提供器 + private DESCryptoServiceProvider createProvider(string key) { + if (key.Length < 8) { + throw new Exception("加密用的key长度为8位"); + } + byte[] bKey = this.Config.Encoding.GetBytes(key.Substring(0, 8)); + return new DESCryptoServiceProvider() { + Padding = Config.Padding, + Mode = Config.Mode, + IV=Config.IV, + Key = bKey, + }; + } + + /// + public string GenerateKey() { + var p = new DESCryptoServiceProvider() { + Padding = Config.Padding, + Mode = Config.Mode, + IV = Config.IV, + }; + p.GenerateKey(); + return Convert.ToBase64String(p.Key); + } + + /// + public string Decrypt(string key, string str) { + var bStr = Convert.FromBase64String(str); + var desc = createProvider(key); + using (var mStream = new MemoryStream()) { + using (var cStream = new CryptoStream(mStream, desc.CreateDecryptor(), CryptoStreamMode.Write)) { + cStream.Write(bStr, 0, bStr.Length); + cStream.FlushFinalBlock(); + var bs = mStream.ToArray(); + return this.Config.Encoding.GetString(bs); + } + } + } + + /// + public string Encrypt(string key, string str) { + byte[] bStr = this.Config.Encoding.GetBytes(str); + var desc = createProvider(key); + using (var mStream = new MemoryStream()) { + using (var cStream = new CryptoStream(mStream, desc.CreateEncryptor(), CryptoStreamMode.Write)) { + cStream.Write(bStr, 0, bStr.Length); + cStream.FlushFinalBlock(); + var bs = mStream.ToArray(); + return Convert.ToBase64String(bs); + } + } + } + } +} diff --git a/Falcon.SugarApi/Encryption/EncryptionProviderRegirst.cs b/Falcon.SugarApi/Encryption/EncryptionProviderRegirst.cs new file mode 100644 index 0000000..3b50270 --- /dev/null +++ b/Falcon.SugarApi/Encryption/EncryptionProviderRegirst.cs @@ -0,0 +1,38 @@ +using Microsoft.Extensions.DependencyInjection; +using System; + + +namespace Falcon.SugarApi.Encryption +{ + /// + /// 安全组件注册 + /// + public static class EncryptionProviderRegirst + { + /// + /// 注册DES加密提供程序,通过IEncryption或IDESEncryption获取注册程序。 + /// + /// 服务集合 + /// 配置方法 + /// 服务集合 + public static IServiceCollection AddDesProvider(this IServiceCollection services, Action config) { + var defConfig = new DESConfig(); + config(defConfig); + services.AddSingleton(new DESProvider(defConfig)); + services.AddSingleton(new DESProvider(defConfig)); + return services; + } + + /// + /// 使用默认配置注册DES加密提供程序,通过IEncryption或IDESEncryption获取注册程序。 + /// + /// 服务集合 + /// 服务集合 + public static IServiceCollection AddDesProvider(this IServiceCollection services) { + var defConfig = new DESConfig(); + services.AddSingleton(new DESProvider(defConfig)); + services.AddSingleton(new DESProvider(defConfig)); + return services; + } + } +} diff --git a/Falcon.SugarApi/Encryption/IDESEncryption.cs b/Falcon.SugarApi/Encryption/IDESEncryption.cs new file mode 100644 index 0000000..3dee492 --- /dev/null +++ b/Falcon.SugarApi/Encryption/IDESEncryption.cs @@ -0,0 +1,7 @@ +namespace Falcon.SugarApi.Encryption +{ + /// + /// DES加密算法接口 + /// + public interface IDESEncryption : IEncryption { } +} \ No newline at end of file diff --git a/Falcon.SugarApi/Encryption/IEncryption.cs b/Falcon.SugarApi/Encryption/IEncryption.cs new file mode 100644 index 0000000..ab8ad21 --- /dev/null +++ b/Falcon.SugarApi/Encryption/IEncryption.cs @@ -0,0 +1,28 @@ +namespace Falcon.SugarApi.Encryption +{ + /// + /// 基础加密解密接口 + /// + public interface IEncryption + { + /// + /// 通过key解密字符串 + /// + /// key + /// 密文 + /// 明文 + string Decrypt(string key, string str); + /// + /// 通过key加密字符串 + /// + /// key + /// 明文 + /// 密文 + string Encrypt(string key, string str); + /// + /// 生成加密key + /// + /// key + string GenerateKey(); + } +} \ No newline at end of file