From 746685a9010410783b0fcd93e66d6dbebb4f95d6 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Tue, 6 Sep 2022 11:13:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0AES=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E5=8F=8A=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Encryption/EncryptionTest.cs | 43 ++++---- Falcon.SugarApi/Encryption/AESConfig.cs | 15 +++ Falcon.SugarApi/Encryption/AESProvider.cs | 100 ++++++++++++++++++ .../Encryption/EncryptionProviderRegirst.cs | 27 +++++ Falcon.SugarApi/Encryption/IAESEncryption.cs | 7 ++ 5 files changed, 172 insertions(+), 20 deletions(-) create mode 100644 Falcon.SugarApi/Encryption/AESConfig.cs create mode 100644 Falcon.SugarApi/Encryption/AESProvider.cs create mode 100644 Falcon.SugarApi/Encryption/IAESEncryption.cs diff --git a/Falcon.SugarApi.Test/Encryption/EncryptionTest.cs b/Falcon.SugarApi.Test/Encryption/EncryptionTest.cs index 1508456..4842d5b 100644 --- a/Falcon.SugarApi.Test/Encryption/EncryptionTest.cs +++ b/Falcon.SugarApi.Test/Encryption/EncryptionTest.cs @@ -11,26 +11,6 @@ namespace Falcon.SugarApi.Test.Encryption [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, "解密后明文不同"); - } /// /// 测试公钥、秘钥生成,加密解密 @@ -59,6 +39,29 @@ namespace Falcon.SugarApi.Test.Encryption } } + /// + /// 测试公钥、秘钥生成,加密解密 + /// + [TestMethod("AES测试")] + public void AESTest() { + var config = new AESConfig() { }; + IAESEncryption r = new AESProvider(config); + var k = r.GenerateKey(); + + for (int i = 0; i < 1000; i++) { + var mingw = GenerateStr(); + Console.WriteLine(mingw); + var p = new AESProvider(config); + var miwen = p.Encrypt(k, mingw); + + var p1 = new AESProvider(config); + var mingw1 = p1.Decrypt(k, miwen); + + Assert.AreEqual(mingw, mingw1, "解密后明文不同"); + + } + } + /// /// 生成随机长度,由字符表内字符组成的字符串 /// diff --git a/Falcon.SugarApi/Encryption/AESConfig.cs b/Falcon.SugarApi/Encryption/AESConfig.cs new file mode 100644 index 0000000..30ecb6e --- /dev/null +++ b/Falcon.SugarApi/Encryption/AESConfig.cs @@ -0,0 +1,15 @@ +namespace Falcon.SugarApi.Encryption +{ + /// + /// AES加密算法配置 + /// + public class AESConfig + { + private int keyLength = 32; + + /// + /// 秘钥长度。最大32 + /// + public int KeyLength { get => keyLength; set => keyLength = value > 32 ? 32 : value; } + } +} diff --git a/Falcon.SugarApi/Encryption/AESProvider.cs b/Falcon.SugarApi/Encryption/AESProvider.cs new file mode 100644 index 0000000..47dc7e9 --- /dev/null +++ b/Falcon.SugarApi/Encryption/AESProvider.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection.Metadata; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; + +namespace Falcon.SugarApi.Encryption +{ + /// + /// AES对称加密算法 + /// + public class AESProvider : IEncryption, IAESEncryption + { + /// + /// 通过提供配置构造AES加密实例 + /// + /// 配置文件 + public AESProvider(AESConfig config) { + Config = config; + } + + /// + /// 加密配置 + /// + public AESConfig Config { get; } + + /// + public string Decrypt(string key, string str) { + var fullCipher = Convert.FromBase64String(str); + var iv = new byte[16]; + var cipher = new byte[fullCipher.Length - iv.Length]; + Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length); + Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, fullCipher.Length - iv.Length); + var decryptKey = GetDecryptKey(key); + using (var aesAlg = Aes.Create()) { + using (var decryptor = aesAlg.CreateDecryptor(decryptKey, iv)) { + string result; + using (var msDecrypt = new MemoryStream(cipher)) { + using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) { + using (var srDecrypt = new StreamReader(csDecrypt)) { + result = srDecrypt.ReadToEnd(); + } + } + } + return result; + } + } + } + + /// + public string Encrypt(string key, string str) { + var encryptKey = GetDecryptKey(key); + using (var aesAlg = Aes.Create()) { + var iv = aesAlg.IV; + using (var encryptor = aesAlg.CreateEncryptor(encryptKey, iv)) { + using (var msEncrypt = new MemoryStream()) { + using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) + using (var swEncrypt = new StreamWriter(csEncrypt)) { + swEncrypt.Write(str); + } + var decryptedContent = msEncrypt.ToArray(); + var result = new byte[iv.Length + decryptedContent.Length]; + Buffer.BlockCopy(iv, 0, result, 0, iv.Length); + Buffer.BlockCopy(decryptedContent, 0, result, iv.Length, decryptedContent.Length); + return Convert.ToBase64String(result); + } + } + } + } + + /// + public string GenerateKey() { + var chars = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789,.!/*\"; + var r = new Random(); + var len = this.Config.KeyLength; + var sb = new StringBuilder(len); + for (int i = 0; i < len; i++) { + sb.Append(chars[r.Next(0, chars.Length)]); + } + return sb.ToString(); + } + + /// + /// 格式化加密key长度 + /// + /// 加密用的key + /// 加密key字节数组 + protected byte[] GetDecryptKey(string key) { + var buf = new byte[this.Config.KeyLength]; + var ek = Encoding.UTF8.GetBytes(key); + var len = this.Config.KeyLength > ek.Length ? ek.Length : buf.Length; + Buffer.BlockCopy(ek, 0, buf, 0, len); + return buf; + } + + } +} diff --git a/Falcon.SugarApi/Encryption/EncryptionProviderRegirst.cs b/Falcon.SugarApi/Encryption/EncryptionProviderRegirst.cs index 3b50270..7ec5f1f 100644 --- a/Falcon.SugarApi/Encryption/EncryptionProviderRegirst.cs +++ b/Falcon.SugarApi/Encryption/EncryptionProviderRegirst.cs @@ -34,5 +34,32 @@ namespace Falcon.SugarApi.Encryption services.AddSingleton(new DESProvider(defConfig)); return services; } + + /// + /// 注册AES加密提供程序,通过IEncryption或IAESEncryption获取注册程序。 + /// + /// 服务集合 + /// 配置方法 + /// 服务集合 + public static IServiceCollection AddAesProvider(this IServiceCollection services, Action config) { + var defConfig = new AESConfig(); + config(defConfig); + services.AddSingleton(new AESProvider(defConfig)); + services.AddSingleton(new AESProvider(defConfig)); + return services; + } + + /// + /// 使用默认配置注册AES加密提供程序,通过IEncryption或IAESEncryption获取注册程序。 + /// + /// 服务集合 + /// 服务集合 + public static IServiceCollection AddAesProvider(this IServiceCollection services) { + var defConfig = new AESConfig(); + services.AddSingleton(new AESProvider(defConfig)); + services.AddSingleton(new AESProvider(defConfig)); + return services; + } + } } diff --git a/Falcon.SugarApi/Encryption/IAESEncryption.cs b/Falcon.SugarApi/Encryption/IAESEncryption.cs new file mode 100644 index 0000000..e0e502a --- /dev/null +++ b/Falcon.SugarApi/Encryption/IAESEncryption.cs @@ -0,0 +1,7 @@ +namespace Falcon.SugarApi.Encryption +{ + /// + /// AES加密算法接口 + /// + public interface IAESEncryption : IEncryption { } +} \ No newline at end of file