From 72130081ee22332be73ce9b6b60453e82e8e7535 Mon Sep 17 00:00:00 2001 From: SmallChi <564952747@qq.com> Date: Mon, 11 Jun 2018 13:34:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0AES128=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEAES128EncryptImpl_NetCore2.cs | 60 ------------------- 1 file changed, 60 deletions(-) delete mode 100644 src/GBNewEnergy.Protocol/NEEncrypts/NEAES128EncryptImpl_NetCore2.cs diff --git a/src/GBNewEnergy.Protocol/NEEncrypts/NEAES128EncryptImpl_NetCore2.cs b/src/GBNewEnergy.Protocol/NEEncrypts/NEAES128EncryptImpl_NetCore2.cs deleted file mode 100644 index 125275a..0000000 --- a/src/GBNewEnergy.Protocol/NEEncrypts/NEAES128EncryptImpl_NetCore2.cs +++ /dev/null @@ -1,60 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Security.Cryptography; -using System.Text; - -namespace GBNewEnergy.Protocol.NEEncrypts -{ - public class NEAES128EncryptImpl_NetCore2 : INEEncrypt - { - private readonly NEGlobalConfigs _nEConfigs; - - /// - /// 盐字节必须为至少8个字节 - /// - private readonly static byte[] saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 }; - - public NEAES128EncryptImpl_NetCore2(NEGlobalConfigs nEConfigs) - { - _nEConfigs = nEConfigs; - } - - public byte[] Decrypt(byte[] buffer) - { - var iv = new byte[16]; - var cipher = new byte[16]; - Buffer.BlockCopy(buffer, 0, iv, 0, iv.Length); - Buffer.BlockCopy(buffer, iv.Length, cipher, 0, iv.Length); - using (var aesAlg = Aes.Create()) - using (var decryptor = aesAlg.CreateDecryptor(_nEConfigs.NEEncryptKeyBytes, iv)) - using (var msDecrypt = new MemoryStream(cipher)) - using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) - { - byte[] bytes = new byte[csDecrypt.Length]; - csDecrypt.Read(bytes, 0, bytes.Length); - // 设置当前流的位置为流的开始 - csDecrypt.Seek(0, SeekOrigin.Begin); - return bytes; - } - } - - public byte[] Encrypt(byte[] buffer) - { - using (var aesAlg = Aes.Create()) - using (var encryptor = aesAlg.CreateEncryptor(_nEConfigs.NEEncryptKeyBytes, aesAlg.IV)) - using (var msEncrypt = new MemoryStream()) - using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) - using (var swEncrypt = new StreamWriter(csEncrypt)) - { - swEncrypt.Write(buffer); - var iv = aesAlg.IV; - 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 result; - } - } - } -}