@@ -7,6 +7,6 @@ namespace GBNewEnergy.Protocol | |||||
public interface INEEncrypt | public interface INEEncrypt | ||||
{ | { | ||||
byte[] Encrypt(byte[] buffer); | byte[] Encrypt(byte[] buffer); | ||||
byte[] Eecrypt(byte[] buffer); | |||||
byte[] Decrypt(byte[] buffer); | |||||
} | } | ||||
} | } |
@@ -20,10 +20,10 @@ namespace GBNewEnergy.Protocol | |||||
#if NETCOREAPP2_1 | #if NETCOREAPP2_1 | ||||
return new NE_AES128EncryptImpl_NetCore2(nEConfigs); | return new NE_AES128EncryptImpl_NetCore2(nEConfigs); | ||||
#else | #else | ||||
return new NE_AES128EncryptImpl(nEConfigs); | |||||
return new NEAES128EncryptImpl(nEConfigs); | |||||
#endif | #endif | ||||
case NEEncryptMethod.RSA: | case NEEncryptMethod.RSA: | ||||
return new NE_RSAEncryptImpl(nEConfigs); | |||||
return new NERSAEncryptImpl(nEConfigs); | |||||
default: | default: | ||||
return null; | return null; | ||||
} | } | ||||
@@ -6,7 +6,7 @@ using System.Text; | |||||
namespace GBNewEnergy.Protocol.NEEncrypts | namespace GBNewEnergy.Protocol.NEEncrypts | ||||
{ | { | ||||
public class NE_AES128EncryptImpl : INEEncrypt | |||||
public class NEAES128EncryptImpl : INEEncrypt | |||||
{ | { | ||||
private readonly NEGlobalConfigs _nEConfigs; | private readonly NEGlobalConfigs _nEConfigs; | ||||
@@ -15,14 +15,13 @@ namespace GBNewEnergy.Protocol.NEEncrypts | |||||
/// </summary> | /// </summary> | ||||
private readonly static byte[] saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 }; | private readonly static byte[] saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 }; | ||||
public NE_AES128EncryptImpl(NEGlobalConfigs nEConfigs) | |||||
public NEAES128EncryptImpl(NEGlobalConfigs nEConfigs) | |||||
{ | { | ||||
_nEConfigs = nEConfigs; | _nEConfigs = nEConfigs; | ||||
} | } | ||||
public byte[] Encrypt(byte[] buffer) | public byte[] Encrypt(byte[] buffer) | ||||
{ | |||||
byte[] decryptedBytes = null; | |||||
{ | |||||
using (var ms = new MemoryStream()) | using (var ms = new MemoryStream()) | ||||
{ | { | ||||
using (var AES = new RijndaelManaged()) | using (var AES = new RijndaelManaged()) | ||||
@@ -33,24 +32,21 @@ namespace GBNewEnergy.Protocol.NEEncrypts | |||||
AES.Key = key.GetBytes(32); | AES.Key = key.GetBytes(32); | ||||
AES.IV = key.GetBytes(16); | AES.IV = key.GetBytes(16); | ||||
AES.Mode = CipherMode.CBC; | AES.Mode = CipherMode.CBC; | ||||
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write)) | |||||
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write)) | |||||
{ | { | ||||
cs.Write(buffer, 0, buffer.Length); | cs.Write(buffer, 0, buffer.Length); | ||||
cs.Close(); | cs.Close(); | ||||
} | } | ||||
decryptedBytes = ms.ToArray(); | |||||
return ms.ToArray(); | |||||
} | } | ||||
} | } | ||||
return decryptedBytes; | |||||
} | } | ||||
public byte[] Eecrypt(byte[] buffer) | |||||
public byte[] Decrypt(byte[] buffer) | |||||
{ | { | ||||
byte[] decryptedBytes = null; | |||||
using (var ms = new MemoryStream()) | using (var ms = new MemoryStream()) | ||||
using (var AES = new RijndaelManaged()) | |||||
{ | { | ||||
using (var AES = new RijndaelManaged()) | |||||
{ | |||||
AES.KeySize = 256; | AES.KeySize = 256; | ||||
AES.BlockSize = 128; | AES.BlockSize = 128; | ||||
var key = new Rfc2898DeriveBytes(_nEConfigs.NEEncryptKeyBytes, saltBytes, 1000); | var key = new Rfc2898DeriveBytes(_nEConfigs.NEEncryptKeyBytes, saltBytes, 1000); | ||||
@@ -62,10 +58,8 @@ namespace GBNewEnergy.Protocol.NEEncrypts | |||||
cs.Write(buffer, 0, buffer.Length); | cs.Write(buffer, 0, buffer.Length); | ||||
cs.Close(); | cs.Close(); | ||||
} | } | ||||
decryptedBytes = ms.ToArray(); | |||||
} | |||||
} | |||||
return decryptedBytes; | |||||
return ms.ToArray(); | |||||
} | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -6,7 +6,7 @@ using System.Text; | |||||
namespace GBNewEnergy.Protocol.NEEncrypts | namespace GBNewEnergy.Protocol.NEEncrypts | ||||
{ | { | ||||
public class NE_AES128EncryptImpl_NetCore2 : INEEncrypt | |||||
public class NEAES128EncryptImpl_NetCore2 : INEEncrypt | |||||
{ | { | ||||
private readonly NEGlobalConfigs _nEConfigs; | private readonly NEGlobalConfigs _nEConfigs; | ||||
@@ -15,12 +15,12 @@ namespace GBNewEnergy.Protocol.NEEncrypts | |||||
/// </summary> | /// </summary> | ||||
private readonly static byte[] saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 }; | private readonly static byte[] saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 }; | ||||
public NE_AES128EncryptImpl_NetCore2(NEGlobalConfigs nEConfigs) | |||||
public NEAES128EncryptImpl_NetCore2(NEGlobalConfigs nEConfigs) | |||||
{ | { | ||||
_nEConfigs = nEConfigs; | _nEConfigs = nEConfigs; | ||||
} | } | ||||
public byte[] Eecrypt(byte[] buffer) | |||||
public byte[] Decrypt(byte[] buffer) | |||||
{ | { | ||||
var iv = new byte[16]; | var iv = new byte[16]; | ||||
var cipher = new byte[16]; | var cipher = new byte[16]; |
@@ -5,16 +5,16 @@ using System.Text; | |||||
namespace GBNewEnergy.Protocol.NEEncrypts | namespace GBNewEnergy.Protocol.NEEncrypts | ||||
{ | { | ||||
#warning 待加解密 | #warning 待加解密 | ||||
public class NE_RSAEncryptImpl : INEEncrypt | |||||
public class NERSAEncryptImpl : INEEncrypt | |||||
{ | { | ||||
private readonly NEGlobalConfigs _nEConfigs; | private readonly NEGlobalConfigs _nEConfigs; | ||||
public NE_RSAEncryptImpl(NEGlobalConfigs nEConfigs) | |||||
public NERSAEncryptImpl(NEGlobalConfigs nEConfigs) | |||||
{ | { | ||||
_nEConfigs = nEConfigs; | _nEConfigs = nEConfigs; | ||||
} | } | ||||
public byte[] Eecrypt(byte[] buffer) | |||||
public byte[] Decrypt(byte[] buffer) | |||||
{ | { | ||||
throw new NotImplementedException(); | throw new NotImplementedException(); | ||||
} | } |
@@ -129,7 +129,7 @@ namespace GBNewEnergy.Protocol | |||||
Array.Copy(Buffer, HeaderFixedByteLength, bodiesBytes, 0, bodiesBytes.Length); | Array.Copy(Buffer, HeaderFixedByteLength, bodiesBytes, 0, bodiesBytes.Length); | ||||
if (NEConfigs.Encrypt != null) | if (NEConfigs.Encrypt != null) | ||||
{ | { | ||||
Bodies = NEBodiesFactory.GetNEBodiesByMsgId(MsgId, NEConfigs.Encrypt.Eecrypt(bodiesBytes),NEConfigs); | |||||
Bodies = NEBodiesFactory.GetNEBodiesByMsgId(MsgId, NEConfigs.Encrypt.Decrypt(bodiesBytes),NEConfigs); | |||||
} | } | ||||
else | else | ||||
{ | { | ||||
@@ -43,5 +43,11 @@ | |||||
<Compile Include="Program.cs" /> | <Compile Include="Program.cs" /> | ||||
<Compile Include="Properties\AssemblyInfo.cs" /> | <Compile Include="Properties\AssemblyInfo.cs" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | |||||
<ProjectReference Include="..\..\GBNewEnergy.Protocol\GBNewEnergy.Protocol.csproj"> | |||||
<Project>{1934f3a6-1396-46c2-bfd6-1e2dc1a26e3a}</Project> | |||||
<Name>GBNewEnergy.Protocol</Name> | |||||
</ProjectReference> | |||||
</ItemGroup> | |||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||||
</Project> | </Project> |
@@ -1,9 +1,11 @@ | |||||
using System; | |||||
using GBNewEnergy.Protocol.NEEncrypts; | |||||
using System; | |||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.IO; | using System.IO; | ||||
using System.Linq; | using System.Linq; | ||||
using System.Security.Cryptography; | using System.Security.Cryptography; | ||||
using System.Text; | using System.Text; | ||||
using GBNewEnergy.Protocol.Extensions; | |||||
namespace NEEncryptsNET4.Test | namespace NEEncryptsNET4.Test | ||||
{ | { | ||||
@@ -11,39 +13,18 @@ namespace NEEncryptsNET4.Test | |||||
{ | { | ||||
static void Main(string[] args) | static void Main(string[] args) | ||||
{ | { | ||||
} | |||||
public static byte[] AESEncryptBytes(byte[] bytesToBeEncrypted, byte[] passwordBytes) | |||||
{ | |||||
byte[] encryptedBytes = null; | |||||
var saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 }; | |||||
using (var ms = new MemoryStream()) | |||||
{ | |||||
using (var AES = new RijndaelManaged()) | |||||
NEAES128EncryptImpl nE_AES128EncryptImpl = new NEAES128EncryptImpl( | |||||
new GBNewEnergy.Protocol.NEGlobalConfigs() | |||||
{ | { | ||||
AES.KeySize = 256; | |||||
AES.BlockSize = 128; | |||||
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000); | |||||
AES.Key = key.GetBytes(32); | |||||
AES.IV = key.GetBytes(16); | |||||
AES.Mode = CipherMode.CBC; | |||||
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), | |||||
CryptoStreamMode.Write)) | |||||
{ | |||||
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length); | |||||
cs.Close(); | |||||
} | |||||
encryptedBytes = ms.ToArray(); | |||||
} | |||||
} | |||||
return encryptedBytes; | |||||
NEEncryptKey="smallchi" | |||||
}); | |||||
string str = "aaasssddd123"; | |||||
var bytes = Encoding.UTF8.GetBytes(str); | |||||
var encrypt=nE_AES128EncryptImpl.Encrypt(bytes); | |||||
Console.WriteLine("原数据:"+ str); | |||||
Console.WriteLine("加密后:"+ encrypt.ToHexString()); | |||||
Console.WriteLine("解密后:" + Encoding.UTF8.GetString(nE_AES128EncryptImpl.Decrypt(encrypt))); | |||||
Console.ReadKey(); | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -47,5 +47,11 @@ | |||||
<ItemGroup> | <ItemGroup> | ||||
<None Include="App.config" /> | <None Include="App.config" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | |||||
<ProjectReference Include="..\..\GBNewEnergy.Protocol\GBNewEnergy.Protocol.csproj"> | |||||
<Project>{1934f3a6-1396-46c2-bfd6-1e2dc1a26e3a}</Project> | |||||
<Name>GBNewEnergy.Protocol</Name> | |||||
</ProjectReference> | |||||
</ItemGroup> | |||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | ||||
</Project> | </Project> |
@@ -1,4 +1,6 @@ | |||||
using System; | |||||
using GBNewEnergy.Protocol.Extensions; | |||||
using GBNewEnergy.Protocol.NEEncrypts; | |||||
using System; | |||||
using System.Collections.Generic; | using System.Collections.Generic; | ||||
using System.Linq; | using System.Linq; | ||||
using System.Text; | using System.Text; | ||||
@@ -10,6 +12,18 @@ namespace NEEncryptsNET45.Test | |||||
{ | { | ||||
static void Main(string[] args) | static void Main(string[] args) | ||||
{ | { | ||||
NEAES128EncryptImpl nE_AES128EncryptImpl = new NEAES128EncryptImpl( | |||||
new GBNewEnergy.Protocol.NEGlobalConfigs() | |||||
{ | |||||
NEEncryptKey = "smallchi" | |||||
}); | |||||
string str = "aaasssddd123"; | |||||
var bytes = Encoding.UTF8.GetBytes(str); | |||||
var encrypt = nE_AES128EncryptImpl.Encrypt(bytes); | |||||
Console.WriteLine("原数据:" + str); | |||||
Console.WriteLine("加密后:" + encrypt.ToHexString()); | |||||
Console.WriteLine("解密后:" + Encoding.UTF8.GetString(nE_AES128EncryptImpl.Decrypt(encrypt))); | |||||
Console.ReadKey(); | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -5,4 +5,8 @@ | |||||
<TargetFramework>netcoreapp2.1</TargetFramework> | <TargetFramework>netcoreapp2.1</TargetFramework> | ||||
</PropertyGroup> | </PropertyGroup> | ||||
<ItemGroup> | |||||
<ProjectReference Include="..\..\GBNewEnergy.Protocol\GBNewEnergy.Protocol.csproj" /> | |||||
</ItemGroup> | |||||
</Project> | </Project> |
@@ -1,4 +1,7 @@ | |||||
using System; | |||||
using GBNewEnergy.Protocol.NEEncrypts; | |||||
using GBNewEnergy.Protocol.Extensions; | |||||
using System; | |||||
using System.Text; | |||||
namespace NEEncryptsNETCore.Test | namespace NEEncryptsNETCore.Test | ||||
{ | { | ||||
@@ -6,7 +9,18 @@ namespace NEEncryptsNETCore.Test | |||||
{ | { | ||||
static void Main(string[] args) | static void Main(string[] args) | ||||
{ | { | ||||
Console.WriteLine("Hello World!"); | |||||
NEAES128EncryptImpl nE_AES128EncryptImpl = new NEAES128EncryptImpl( | |||||
new GBNewEnergy.Protocol.NEGlobalConfigs() | |||||
{ | |||||
NEEncryptKey = "smallchi" | |||||
}); | |||||
string str =Guid.NewGuid().ToString("N"); | |||||
var bytes = Encoding.UTF8.GetBytes(str); | |||||
var encrypt = nE_AES128EncryptImpl.Encrypt(bytes); | |||||
Console.WriteLine("原数据:" + str); | |||||
Console.WriteLine("加密后:" + encrypt.ToHexString()); | |||||
Console.WriteLine("解密后:" + Encoding.UTF8.GetString(nE_AES128EncryptImpl.Decrypt(encrypt))); | |||||
Console.ReadKey(); | |||||
} | } | ||||
} | } | ||||
} | } |