@@ -9,6 +9,8 @@ | |||||
<Compile Remove="IBuffer.cs" /> | <Compile Remove="IBuffer.cs" /> | ||||
<Compile Remove="IBuffered.cs" /> | <Compile Remove="IBuffered.cs" /> | ||||
<Compile Remove="NEDownStreamBase.cs" /> | <Compile Remove="NEDownStreamBase.cs" /> | ||||
<Compile Remove="NEEncryptFactory.cs" /> | |||||
<Compile Remove="NEEncrypts\NEAES128EncryptImpl_NetCore2.cs" /> | |||||
<Compile Remove="NEHeader.cs" /> | <Compile Remove="NEHeader.cs" /> | ||||
<Compile Remove="NEUpStreamBase.cs" /> | <Compile Remove="NEUpStreamBase.cs" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
@@ -17,11 +17,7 @@ namespace GBNewEnergy.Protocol | |||||
case NEEncryptMethod.Exception: | case NEEncryptMethod.Exception: | ||||
return null; | return null; | ||||
case NEEncryptMethod.AES128: | case NEEncryptMethod.AES128: | ||||
#if NETCOREAPP2_1 | |||||
return new NE_AES128EncryptImpl_NetCore2(nEConfigs); | |||||
#else | |||||
return new NEAES128EncryptImpl(nEConfigs); | |||||
#endif | |||||
return new NEAES128EncryptImpl(nEConfigs); | |||||
case NEEncryptMethod.RSA: | case NEEncryptMethod.RSA: | ||||
return new NERSAEncryptImpl(nEConfigs); | return new NERSAEncryptImpl(nEConfigs); | ||||
default: | default: | ||||
@@ -21,22 +21,17 @@ namespace GBNewEnergy.Protocol.NEEncrypts | |||||
} | } | ||||
public byte[] Encrypt(byte[] buffer) | public byte[] Encrypt(byte[] buffer) | ||||
{ | |||||
using (var ms = new MemoryStream()) | |||||
{ | |||||
using (Aes encryptor = Aes.Create()) | |||||
{ | { | ||||
using (var AES = new RijndaelManaged()) | |||||
{ | |||||
AES.KeySize = 256; | |||||
AES.BlockSize = 128; | |||||
var key = new Rfc2898DeriveBytes(_nEConfigs.NEEncryptKeyBytes, 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(buffer, 0, buffer.Length); | |||||
cs.Close(); | |||||
} | |||||
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_nEConfigs.NEEncryptKey, saltBytes); | |||||
encryptor.Key = pdb.GetBytes(32); | |||||
encryptor.IV = pdb.GetBytes(16); | |||||
using (MemoryStream ms = new MemoryStream()) | |||||
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(),CryptoStreamMode.Write)) | |||||
{ | |||||
cs.Write(buffer, 0, buffer.Length); | |||||
cs.Close(); | |||||
return ms.ToArray(); | return ms.ToArray(); | ||||
} | } | ||||
} | } | ||||
@@ -44,22 +39,19 @@ namespace GBNewEnergy.Protocol.NEEncrypts | |||||
public byte[] Decrypt(byte[] buffer) | public byte[] Decrypt(byte[] buffer) | ||||
{ | { | ||||
using (var ms = new MemoryStream()) | |||||
using (var AES = new RijndaelManaged()) | |||||
using (Aes encryptor = Aes.Create()) | |||||
{ | { | ||||
AES.KeySize = 256; | |||||
AES.BlockSize = 128; | |||||
var key = new Rfc2898DeriveBytes(_nEConfigs.NEEncryptKeyBytes, saltBytes, 1000); | |||||
AES.Key = key.GetBytes(32); | |||||
AES.IV = key.GetBytes(16); | |||||
AES.Mode = CipherMode.CBC; | |||||
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write)) | |||||
{ | |||||
cs.Write(buffer, 0, buffer.Length); | |||||
cs.Close(); | |||||
} | |||||
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(_nEConfigs.NEEncryptKey, saltBytes); | |||||
encryptor.Key = pdb.GetBytes(32); | |||||
encryptor.IV = pdb.GetBytes(16); | |||||
using (MemoryStream ms = new MemoryStream()) | |||||
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(),CryptoStreamMode.Write)) | |||||
{ | |||||
cs.Write(buffer, 0, buffer.Length); | |||||
cs.Close(); | |||||
return ms.ToArray(); | return ms.ToArray(); | ||||
} | |||||
} | |||||
} | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -12,10 +12,6 @@ namespace GBNewEnergy.Protocol | |||||
/// </summary> | /// </summary> | ||||
public string NEEncryptKey { get; set; } | public string NEEncryptKey { get; set; } | ||||
/// <summary> | /// <summary> | ||||
/// 密钥 | |||||
/// </summary> | |||||
public byte[] NEEncryptKeyBytes => Encoding.UTF8.GetBytes(NEEncryptKey); | |||||
/// <summary> | |||||
/// 数据单元加密方式 | /// 数据单元加密方式 | ||||
/// 0x01:数据不加密;0x02:数据经过 RSA 算法加密;0x03:数据经过 AES128 位算法加密;“0xFE”表示异常,“0xFF”表示无效 | /// 0x01:数据不加密;0x02:数据经过 RSA 算法加密;0x03:数据经过 AES128 位算法加密;“0xFE”表示异常,“0xFF”表示无效 | ||||
/// </summary> | /// </summary> | ||||
@@ -14,12 +14,12 @@ namespace NEEncryptsNETCore.Test | |||||
{ | { | ||||
NEEncryptKey = "smallchi" | NEEncryptKey = "smallchi" | ||||
}); | }); | ||||
string str =Guid.NewGuid().ToString("N"); | |||||
string str ="aaaaaa111111"; | |||||
var bytes = Encoding.UTF8.GetBytes(str); | var bytes = Encoding.UTF8.GetBytes(str); | ||||
var encrypt = nE_AES128EncryptImpl.Encrypt(bytes); | var encrypt = nE_AES128EncryptImpl.Encrypt(bytes); | ||||
Console.WriteLine("原数据:" + str); | Console.WriteLine("原数据:" + str); | ||||
Console.WriteLine("加密后:" + encrypt.ToHexString()); | Console.WriteLine("加密后:" + encrypt.ToHexString()); | ||||
Console.WriteLine("解密后:" + Encoding.UTF8.GetString(nE_AES128EncryptImpl.Decrypt(encrypt))); | |||||
Console.WriteLine("解密后:" + Encoding.ASCII.GetString(nE_AES128EncryptImpl.Decrypt(encrypt))); | |||||
Console.ReadKey(); | Console.ReadKey(); | ||||
} | } | ||||
} | } | ||||