From 22f04ed222f0ec7db2a3cdfc348de5ff2bc458b8 Mon Sep 17 00:00:00 2001 From: SmallChi <564952747@qq.com> Date: Sat, 9 Jun 2018 16:59:51 +0800 Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0=E5=85=A8=E5=B1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=202.=E5=A2=9E=E5=8A=A0=E5=8A=A0=E5=AF=86=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=88=E5=AD=98=E5=9C=A8=E6=93=8D=E4=BD=9C=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E5=85=BC=E5=AE=B9=E9=97=AE=E9=A2=98=EF=BC=8C=E5=BE=85?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=20=E3=80=90mono=E3=80=81dotnetcore=E3=80=81d?= =?UTF-8?q?otnet=E3=80=91=20=EF=BC=89=203.=E5=A2=9E=E5=8A=A0=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E5=A4=9A=E7=8E=AF=E5=A2=83=E6=B5=8B=E8=AF=95=EF=BC=88?= =?UTF-8?q?=E5=BE=85=E6=B5=8B=E8=AF=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEPackageTest.cs | 30 +++++----- .../Extensions/NEExtensions.cs | 20 ------- .../GBNewEnergy.Protocol.csproj | 12 +++- src/GBNewEnergy.Protocol/NEBodies.cs | 14 +++-- src/GBNewEnergy.Protocol/NEBodiesFactory.cs | 6 +- .../NEBufferedEntityBase.cs | 11 +++- src/GBNewEnergy.Protocol/NEEncryptFactory.cs | 10 +++- .../NEEncrypts/NE_AES128EncryptImpl.cs | 59 ++++++++++++++++-- .../NE_AES128EncryptImpl_NetCore2.cs | 60 +++++++++++++++++++ .../NEEncrypts/NE_RSAEncryptImpl.cs | 7 +++ src/GBNewEnergy.Protocol/NEGlobalConfigs.cs | 29 +++++++++ src/GBNewEnergy.Protocol/NEPackage.cs | 27 ++++----- .../NEProperties/NEPackageProperty.cs | 2 - .../UpStream/NELoginUpStream.cs | 13 ++-- .../UpStream/NELogoutUpStream.cs | 11 ++-- src/GBNewEnergy.sln | 25 ++++++++ .../NEEncryptsNET4.Test.csproj | 47 +++++++++++++++ .../NEEncryptsNET4.Test/Program.cs | 49 +++++++++++++++ .../Properties/AssemblyInfo.cs | 36 +++++++++++ .../NEEncryptsNET45.Test/App.config | 6 ++ .../NEEncryptsNET45.Test.csproj | 51 ++++++++++++++++ .../NEEncryptsNET45.Test/Program.cs | 15 +++++ .../Properties/AssemblyInfo.cs | 36 +++++++++++ .../NEEncryptsNETCore.Test.csproj | 8 +++ .../NEEncryptsNETCore.Test/Program.cs | 12 ++++ 25 files changed, 512 insertions(+), 84 deletions(-) create mode 100644 src/GBNewEnergy.Protocol/NEEncrypts/NE_AES128EncryptImpl_NetCore2.cs create mode 100644 src/GBNewEnergy.Protocol/NEGlobalConfigs.cs create mode 100644 src/NEEncrypts.Tests/NEEncryptsNET4.Test/NEEncryptsNET4.Test.csproj create mode 100644 src/NEEncrypts.Tests/NEEncryptsNET4.Test/Program.cs create mode 100644 src/NEEncrypts.Tests/NEEncryptsNET4.Test/Properties/AssemblyInfo.cs create mode 100644 src/NEEncrypts.Tests/NEEncryptsNET45.Test/App.config create mode 100644 src/NEEncrypts.Tests/NEEncryptsNET45.Test/NEEncryptsNET45.Test.csproj create mode 100644 src/NEEncrypts.Tests/NEEncryptsNET45.Test/Program.cs create mode 100644 src/NEEncrypts.Tests/NEEncryptsNET45.Test/Properties/AssemblyInfo.cs create mode 100644 src/NEEncrypts.Tests/NEEncryptsNETCore.Test/NEEncryptsNETCore.Test.csproj create mode 100644 src/NEEncrypts.Tests/NEEncryptsNETCore.Test/Program.cs diff --git a/src/GBNewEnergy.Protocol.Test/NEPackageTest.cs b/src/GBNewEnergy.Protocol.Test/NEPackageTest.cs index a99f8e3..df279c4 100644 --- a/src/GBNewEnergy.Protocol.Test/NEPackageTest.cs +++ b/src/GBNewEnergy.Protocol.Test/NEPackageTest.cs @@ -12,6 +12,10 @@ namespace GBNewEnergy.Protocol.Test { public class NEPackageTest { + private readonly NEGlobalConfigs NEGlobalConfigs = new NEGlobalConfigs() + { + NEEncryptKey="smallchi" + }; #region 车辆登入 [Fact] public void NELoginUpStreamConstructor4_1() @@ -27,16 +31,15 @@ namespace GBNewEnergy.Protocol.Test BatteryNos=new List() { "1" }, SIM= "64743066405" }; - NELoginUpStream nELoginUpStream = new NELoginUpStream(nELoginProperty); + NELoginUpStream nELoginUpStream = new NELoginUpStream(nELoginProperty, NEGlobalConfigs); INEProperties nEPackageProperty = new NEPackageProperty { Bodies= nELoginUpStream, MsgId = Enums.NEMsgId.login, AskId= Enums.NEAskId.cmd, - EncryptMethod= NEEncryptMethod.None, VIN= "LGHC4V1D3HE202652" }; - NEPackage nEPackage = new NEPackage(nEPackageProperty); + NEPackage nEPackage = new NEPackage(nEPackageProperty, NEGlobalConfigs); string headerHex = nEPackage.Header.ToHexString(); string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); string packageHex = nEPackage.Buffer.ToHexString(); @@ -49,7 +52,7 @@ namespace GBNewEnergy.Protocol.Test // "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F 12 06 07 11 04 1B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D7" byte[] header = "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F".ToHexBytes(); byte[] body = "12 06 07 11 04 1B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D7".ToHexBytes(); - NEPackage nEPackage = new NEPackage(header, body); + NEPackage nEPackage = new NEPackage(header, body,NEGlobalConfigs); string headerHex = nEPackage.Header.ToHexString(); string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); string packageHex = nEPackage.Buffer.ToHexString(); @@ -60,7 +63,7 @@ namespace GBNewEnergy.Protocol.Test // "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F 12 06 07 11 04 1B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D7" byte[] packageBytes = "23 23 01 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 1F 12 06 07 11 04 1B 00 01 36 34 37 34 33 30 36 36 34 30 35 00 00 00 00 00 00 00 00 00 01 01 31 D7".ToHexBytes(); - NEPackage nEPackage = new NEPackage(packageBytes); + NEPackage nEPackage = new NEPackage(packageBytes, NEGlobalConfigs); string headerHex = nEPackage.Header.ToHexString(); string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); string packageHex = nEPackage.Buffer.ToHexString(); @@ -78,16 +81,15 @@ namespace GBNewEnergy.Protocol.Test { VIN = "LGHC4V1D3HE202652" }; - NELogoutUpStream nELogoutUpStream1 = new NELogoutUpStream(nELogoutProperty); + NELogoutUpStream nELogoutUpStream1 = new NELogoutUpStream(nELogoutProperty, NEGlobalConfigs); INEProperties nEPackageProperty1 = new NEPackageProperty { Bodies = nELogoutUpStream1, MsgId = Enums.NEMsgId.login, AskId = Enums.NEAskId.cmd, - EncryptMethod = NEEncryptMethod.None, VIN = "LGHC4V1D3HE202652" }; - NEPackage nEPackage = new NEPackage(nEPackageProperty1); + NEPackage nEPackage = new NEPackage(nEPackageProperty1, NEGlobalConfigs); string headerHex = nEPackage.Header.ToHexString(); string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); string packageHex = nEPackage.Buffer.ToHexString(); @@ -109,30 +111,28 @@ namespace GBNewEnergy.Protocol.Test BatteryNos = new List() { "1" }, SIM = "64743066405" }; - NELoginUpStream nELoginUpStream = new NELoginUpStream(nELoginProperty); + NELoginUpStream nELoginUpStream = new NELoginUpStream(nELoginProperty, NEGlobalConfigs); INEProperties nEPackageProperty = new NEPackageProperty { Bodies = nELoginUpStream, MsgId = Enums.NEMsgId.login, AskId = Enums.NEAskId.cmd, - EncryptMethod = NEEncryptMethod.None, VIN = "LGHC4V1D3HE202652" }; - NEPackage nEPackageLogin = new NEPackage(nEPackageProperty); + NEPackage nEPackageLogin = new NEPackage(nEPackageProperty, NEGlobalConfigs); INEProperties nELogoutProperty = new NELogoutProperty() { VIN = "LGHC4V1D3HE202652" }; - NELogoutUpStream nELogoutUpStream1 = new NELogoutUpStream(nELogoutProperty); + NELogoutUpStream nELogoutUpStream1 = new NELogoutUpStream(nELogoutProperty, NEGlobalConfigs); INEProperties nEPackageProperty1 = new NEPackageProperty { Bodies = nELogoutUpStream1, MsgId = Enums.NEMsgId.loginout, AskId = Enums.NEAskId.cmd, - EncryptMethod = NEEncryptMethod.None, VIN = "LGHC4V1D3HE202652" }; - NEPackage nEPackage = new NEPackage(nEPackageProperty1); + NEPackage nEPackage = new NEPackage(nEPackageProperty1, NEGlobalConfigs); // "23 23 05 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 08" // "12 06 08 12 06 3A 00 01" // "23 23 05 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 08 12 06 08 12 06 3A 00 01 E9" @@ -145,7 +145,7 @@ namespace GBNewEnergy.Protocol.Test public void NELogoutUpStream2_2() { byte[] packageBytes = "23 23 05 FE 4C 47 48 43 34 56 31 44 33 48 45 32 30 32 36 35 32 01 00 08 12 06 08 12 06 3A 00 01 E9".ToHexBytes(); - NEPackage nEPackage = new NEPackage(packageBytes); + NEPackage nEPackage = new NEPackage(packageBytes, NEGlobalConfigs); string headerHex = nEPackage.Header.ToHexString(); string bodiesHex = nEPackage.Bodies.Buffer.ToHexString(); string packageHex = nEPackage.Buffer.ToHexString(); diff --git a/src/GBNewEnergy.Protocol/Extensions/NEExtensions.cs b/src/GBNewEnergy.Protocol/Extensions/NEExtensions.cs index 318d327..aba558e 100644 --- a/src/GBNewEnergy.Protocol/Extensions/NEExtensions.cs +++ b/src/GBNewEnergy.Protocol/Extensions/NEExtensions.cs @@ -92,26 +92,6 @@ namespace GBNewEnergy.Protocol.Extensions write[offset++] = (byte)date.Second; } - public static void WriteLittle(this BinaryWriter write, string str,Encoding coding) - { - write.Write(coding.GetBytes(str)); - } - - public static void WriteLittle(this BinaryWriter write, string str) - { - write.Write(Encoding.ASCII.GetBytes(str)); - } - - public static void WriteLittle(this BinaryWriter write, int data, int len) - { - int n = 1; - for (int i = 0; i < len; i++) - { - write.Write((byte)(data >> 8 * (len - n))); - n++; - } - } - /// /// 异或 /// diff --git a/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj b/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj index a652fc6..58b2083 100644 --- a/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj +++ b/src/GBNewEnergy.Protocol/GBNewEnergy.Protocol.csproj @@ -1,7 +1,7 @@ - + - netstandard2.0 + netstandard2.0;net40;net45 @@ -13,6 +13,14 @@ + + + + + + + + diff --git a/src/GBNewEnergy.Protocol/NEBodies.cs b/src/GBNewEnergy.Protocol/NEBodies.cs index fad8858..7c48638 100644 --- a/src/GBNewEnergy.Protocol/NEBodies.cs +++ b/src/GBNewEnergy.Protocol/NEBodies.cs @@ -11,11 +11,11 @@ namespace GBNewEnergy.Protocol /// VIN - 登录流水号,过期时间(每天置1) /// 车载终端登入一次,登入流水号自动加1,从1开始循环累加,最大值为65531,循环周期为天 /// - protected static readonly ConcurrentDictionary LoginNumDict; + protected static readonly ConcurrentDictionary LoginNumDict; static NEBodies() { - LoginNumDict = new ConcurrentDictionary(); + LoginNumDict = new ConcurrentDictionary(); } /// @@ -30,8 +30,14 @@ namespace GBNewEnergy.Protocol /// public DateTime CurrentDateTime { get; protected set; } = DateTime.Now; - protected NEBodies(byte[] buffer):base(buffer){} + protected NEBodies(byte[] buffer, NEGlobalConfigs nEConfigs) : base(buffer, nEConfigs) { } - protected NEBodies(INEProperties nEProperties) : base(nEProperties){} + protected NEBodies(INEProperties nEProperties, NEGlobalConfigs nEConfigs) : base(nEProperties, nEConfigs) { } + + protected class LoginInfo + { + public ushort LoginNum { get; set; } + public DateTime ExpirationTime { get; set; } + } } } diff --git a/src/GBNewEnergy.Protocol/NEBodiesFactory.cs b/src/GBNewEnergy.Protocol/NEBodiesFactory.cs index c175c9c..b180e29 100644 --- a/src/GBNewEnergy.Protocol/NEBodiesFactory.cs +++ b/src/GBNewEnergy.Protocol/NEBodiesFactory.cs @@ -14,14 +14,14 @@ namespace GBNewEnergy.Protocol /// /// /// - public static NEBodies GetNEBodiesByMsgId(NEMsgId msgId,byte[] buf) + public static NEBodies GetNEBodiesByMsgId(NEMsgId msgId,byte[] buf, NEGlobalConfigs nEConfigs) { switch (msgId) { case NEMsgId.login: - return new NELoginUpStream(buf); + return new NELoginUpStream(buf, nEConfigs); case NEMsgId.loginout: - return new NELogoutUpStream(buf); + return new NELogoutUpStream(buf, nEConfigs); default: return null; } diff --git a/src/GBNewEnergy.Protocol/NEBufferedEntityBase.cs b/src/GBNewEnergy.Protocol/NEBufferedEntityBase.cs index 627f8ad..f47b11d 100644 --- a/src/GBNewEnergy.Protocol/NEBufferedEntityBase.cs +++ b/src/GBNewEnergy.Protocol/NEBufferedEntityBase.cs @@ -7,22 +7,27 @@ namespace GBNewEnergy.Protocol { public byte[] Buffer { get; protected set; } - protected NEBufferedEntityBase(byte[] buffer) + public NEGlobalConfigs NEConfigs { get;} + + protected NEBufferedEntityBase(byte[] buffer, NEGlobalConfigs nEConfigs) { + NEConfigs = nEConfigs; Buffer = buffer; InitializePropertiesFromBuffer(); } - protected NEBufferedEntityBase(byte[] headerBuffer, byte[] bodyBuffer) + protected NEBufferedEntityBase(byte[] headerBuffer, byte[] bodyBuffer, NEGlobalConfigs nEConfigs) { + NEConfigs = nEConfigs; Buffer = new byte[headerBuffer.Length + bodyBuffer.Length]; Array.Copy(headerBuffer, 0, Buffer, 0, headerBuffer.Length); Array.Copy(bodyBuffer, 0, Buffer, headerBuffer.Length, bodyBuffer.Length); InitializePropertiesFromBuffer(); } - protected NEBufferedEntityBase(INEProperties nEProperties) + protected NEBufferedEntityBase(INEProperties nEProperties,NEGlobalConfigs nEConfigs) { + NEConfigs = nEConfigs; InitializeProperties(nEProperties); ToBuffer(); } diff --git a/src/GBNewEnergy.Protocol/NEEncryptFactory.cs b/src/GBNewEnergy.Protocol/NEEncryptFactory.cs index 44403c4..6619778 100644 --- a/src/GBNewEnergy.Protocol/NEEncryptFactory.cs +++ b/src/GBNewEnergy.Protocol/NEEncryptFactory.cs @@ -8,7 +8,7 @@ namespace GBNewEnergy.Protocol { internal class NEEncryptFactory { - internal static INEEncrypt GetNEEncrypt(NEEncryptMethod nEEncryptMethod) + internal static INEEncrypt GetNEEncrypt(NEEncryptMethod nEEncryptMethod, NEGlobalConfigs nEConfigs) { switch (nEEncryptMethod) { @@ -17,9 +17,13 @@ namespace GBNewEnergy.Protocol case NEEncryptMethod.Exception: return null; case NEEncryptMethod.AES128: - return new NE_AES128EncryptImpl(); +#if NETCOREAPP2_1 + return new NE_AES128EncryptImpl_NetCore2(nEConfigs); +#else + return new NE_AES128EncryptImpl(nEConfigs); +#endif case NEEncryptMethod.RSA: - return new NE_RSAEncryptImpl(); + return new NE_RSAEncryptImpl(nEConfigs); default: return null; } diff --git a/src/GBNewEnergy.Protocol/NEEncrypts/NE_AES128EncryptImpl.cs b/src/GBNewEnergy.Protocol/NEEncrypts/NE_AES128EncryptImpl.cs index eee7be6..d50ad70 100644 --- a/src/GBNewEnergy.Protocol/NEEncrypts/NE_AES128EncryptImpl.cs +++ b/src/GBNewEnergy.Protocol/NEEncrypts/NE_AES128EncryptImpl.cs @@ -1,20 +1,71 @@ using System; using System.Collections.Generic; +using System.IO; +using System.Security.Cryptography; using System.Text; namespace GBNewEnergy.Protocol.NEEncrypts { -#warning 待加解密 public class NE_AES128EncryptImpl : INEEncrypt { - public byte[] Eecrypt(byte[] buffer) + private readonly NEGlobalConfigs _nEConfigs; + + /// + /// 盐字节必须为至少8个字节 + /// + private readonly static byte[] saltBytes = new byte[9] { 13, 34, 27, 67, 189, 255, 104, 219, 122 }; + + public NE_AES128EncryptImpl(NEGlobalConfigs nEConfigs) { - throw new NotImplementedException(); + _nEConfigs = nEConfigs; } public byte[] Encrypt(byte[] buffer) { - throw new NotImplementedException(); + byte[] decryptedBytes = null; + using (var ms = new MemoryStream()) + { + 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.CreateDecryptor(), CryptoStreamMode.Write)) + { + cs.Write(buffer, 0, buffer.Length); + cs.Close(); + } + decryptedBytes = ms.ToArray(); + } + } + return decryptedBytes; + } + + public byte[] Eecrypt(byte[] buffer) + { + byte[] decryptedBytes = null; + using (var ms = new MemoryStream()) + { + 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.CreateDecryptor(), CryptoStreamMode.Write)) + { + cs.Write(buffer, 0, buffer.Length); + cs.Close(); + } + decryptedBytes = ms.ToArray(); + } + } + return decryptedBytes; } } } diff --git a/src/GBNewEnergy.Protocol/NEEncrypts/NE_AES128EncryptImpl_NetCore2.cs b/src/GBNewEnergy.Protocol/NEEncrypts/NE_AES128EncryptImpl_NetCore2.cs new file mode 100644 index 0000000..67de7d0 --- /dev/null +++ b/src/GBNewEnergy.Protocol/NEEncrypts/NE_AES128EncryptImpl_NetCore2.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Security.Cryptography; +using System.Text; + +namespace GBNewEnergy.Protocol.NEEncrypts +{ + public class NE_AES128EncryptImpl_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 NE_AES128EncryptImpl_NetCore2(NEGlobalConfigs nEConfigs) + { + _nEConfigs = nEConfigs; + } + + public byte[] Eecrypt(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; + } + } + } +} diff --git a/src/GBNewEnergy.Protocol/NEEncrypts/NE_RSAEncryptImpl.cs b/src/GBNewEnergy.Protocol/NEEncrypts/NE_RSAEncryptImpl.cs index 207a2ae..8a8172d 100644 --- a/src/GBNewEnergy.Protocol/NEEncrypts/NE_RSAEncryptImpl.cs +++ b/src/GBNewEnergy.Protocol/NEEncrypts/NE_RSAEncryptImpl.cs @@ -7,6 +7,13 @@ namespace GBNewEnergy.Protocol.NEEncrypts #warning 待加解密 public class NE_RSAEncryptImpl : INEEncrypt { + private readonly NEGlobalConfigs _nEConfigs; + + public NE_RSAEncryptImpl(NEGlobalConfigs nEConfigs) + { + _nEConfigs = nEConfigs; + } + public byte[] Eecrypt(byte[] buffer) { throw new NotImplementedException(); diff --git a/src/GBNewEnergy.Protocol/NEGlobalConfigs.cs b/src/GBNewEnergy.Protocol/NEGlobalConfigs.cs new file mode 100644 index 0000000..fca9978 --- /dev/null +++ b/src/GBNewEnergy.Protocol/NEGlobalConfigs.cs @@ -0,0 +1,29 @@ +using GBNewEnergy.Protocol.Enums; +using System; +using System.Collections.Generic; +using System.Text; + +namespace GBNewEnergy.Protocol +{ + public class NEGlobalConfigs + { + /// + /// 密钥 + /// + public string NEEncryptKey { get; set; } + /// + /// 密钥 + /// + public byte[] NEEncryptKeyBytes => Encoding.UTF8.GetBytes(NEEncryptKey); + /// + /// 数据单元加密方式 + /// 0x01:数据不加密;0x02:数据经过 RSA 算法加密;0x03:数据经过 AES128 位算法加密;“0xFE”表示异常,“0xFF”表示无效 + /// + public NEEncryptMethod EncryptMethod { get; set; } = NEEncryptMethod.None; + /// + /// 数据单元加密 + /// 当数据单元存在加密时,应先加密后校验,先校验后解密 + /// + public INEEncrypt Encrypt { get; set; } + } +} diff --git a/src/GBNewEnergy.Protocol/NEPackage.cs b/src/GBNewEnergy.Protocol/NEPackage.cs index be19d32..7175fba 100644 --- a/src/GBNewEnergy.Protocol/NEPackage.cs +++ b/src/GBNewEnergy.Protocol/NEPackage.cs @@ -12,16 +12,16 @@ namespace GBNewEnergy.Protocol /// public class NEPackage : NEBufferedEntityBase { - public NEPackage(byte[] header, byte[] body) : base(header, body) + public NEPackage(byte[] header, byte[] body, NEGlobalConfigs nEConfigs) : base(header, body, nEConfigs) { } - public NEPackage(byte[] buf) : base(buf) + public NEPackage(byte[] buf, NEGlobalConfigs nEConfigs) : base(buf, nEConfigs) { } - public NEPackage(INEProperties nEProperties) - : base(nEProperties) + public NEPackage(INEProperties nEProperties, NEGlobalConfigs nEConfigs) + : base(nEProperties, nEConfigs) { } /// @@ -70,11 +70,6 @@ namespace GBNewEnergy.Protocol /// 数据体 /// public NEBodies Bodies { get; protected set; } - /// - /// 数据单元加密 - /// 当数据单元存在加密时,应先加密后校验,先校验后解密 - /// - private INEEncrypt Encrypt; protected override void ToBuffer() { @@ -88,9 +83,9 @@ namespace GBNewEnergy.Protocol Buffer.WriteLittle(VIN, 4); Buffer[21] = (byte)EncryptMethod; Buffer.WriteLittle(DataUnitLength, 22, 2); - if (Encrypt != null) + if (NEConfigs.Encrypt != null) { - Buffer.WriteLittle(Encrypt.Encrypt(Bodies.Buffer), 24, DataUnitLength); + Buffer.WriteLittle(NEConfigs.Encrypt.Encrypt(Bodies.Buffer), 24, DataUnitLength); } else { @@ -109,8 +104,7 @@ namespace GBNewEnergy.Protocol MsgId = nEPackageProperty.MsgId; AskId = nEPackageProperty.AskId; Bodies = nEPackageProperty.Bodies; - EncryptMethod = nEPackageProperty.EncryptMethod; - Encrypt = NEEncryptFactory.GetNEEncrypt(EncryptMethod); + EncryptMethod = NEConfigs.EncryptMethod; } protected override void InitializePropertiesFromBuffer() @@ -120,7 +114,6 @@ namespace GBNewEnergy.Protocol AskId = (NEAskId)Buffer[3]; VIN = Buffer.ReadStringLittle(4, 17); EncryptMethod = (NEEncryptMethod)Buffer[21]; - Encrypt = NEEncryptFactory.GetNEEncrypt(EncryptMethod); DataUnitLength = Buffer.ReadUShortH2LLittle(22, 2); // 进行BCC校验码 // 校验位 = 报文长度 - 最后一位(校验位) - 偏移量(2) @@ -134,13 +127,13 @@ namespace GBNewEnergy.Protocol BCCCode = bCCCode2; byte[] bodiesBytes = new byte[DataUnitLength + CheckBit]; Array.Copy(Buffer, HeaderFixedByteLength, bodiesBytes, 0, bodiesBytes.Length); - if (Encrypt != null) + if (NEConfigs.Encrypt != null) { - Bodies = NEBodiesFactory.GetNEBodiesByMsgId(MsgId, Encrypt.Eecrypt(bodiesBytes)); + Bodies = NEBodiesFactory.GetNEBodiesByMsgId(MsgId, NEConfigs.Encrypt.Eecrypt(bodiesBytes),NEConfigs); } else { - Bodies = NEBodiesFactory.GetNEBodiesByMsgId(MsgId, bodiesBytes); + Bodies = NEBodiesFactory.GetNEBodiesByMsgId(MsgId, bodiesBytes, NEConfigs); } Header = new byte[HeaderFixedByteLength]; Array.Copy(Buffer, 0, Header, 0, HeaderFixedByteLength); diff --git a/src/GBNewEnergy.Protocol/NEProperties/NEPackageProperty.cs b/src/GBNewEnergy.Protocol/NEProperties/NEPackageProperty.cs index d0fe270..3e74c9f 100644 --- a/src/GBNewEnergy.Protocol/NEProperties/NEPackageProperty.cs +++ b/src/GBNewEnergy.Protocol/NEProperties/NEPackageProperty.cs @@ -11,7 +11,5 @@ namespace GBNewEnergy.Protocol.NEProperties public NEMsgId MsgId { get; set; } public NEAskId AskId { get; set; } public NEBodies Bodies { get; set; } - public NEEncryptMethod EncryptMethod { get; set; } - public INEEncrypt Encrypt { get; set; } } } diff --git a/src/GBNewEnergy.Protocol/UpStream/NELoginUpStream.cs b/src/GBNewEnergy.Protocol/UpStream/NELoginUpStream.cs index f36349d..f72593d 100644 --- a/src/GBNewEnergy.Protocol/UpStream/NELoginUpStream.cs +++ b/src/GBNewEnergy.Protocol/UpStream/NELoginUpStream.cs @@ -12,12 +12,11 @@ namespace GBNewEnergy.Protocol.UpStream /// public class NELoginUpStream : NEBodies { - public NELoginUpStream(byte[] buffer) : base(buffer) + public NELoginUpStream(INEProperties nEProperties, NEGlobalConfigs nEConfigs) : base(nEProperties, nEConfigs) { - } - public NELoginUpStream(INEProperties nEProperties) : base(nEProperties) + public NELoginUpStream(byte[] buffer, NEGlobalConfigs nEConfigs) : base(buffer, nEConfigs) { } @@ -62,26 +61,26 @@ namespace GBNewEnergy.Protocol.UpStream NELoginProperty nELoginProperty = (NELoginProperty)nEProperties; if (LoginNumDict.ContainsKey(nELoginProperty.VIN)) { - (ushort LoginNum, DateTime ExpirationTime) temp; + LoginInfo temp; if (LoginNumDict.TryGetValue(nELoginProperty.VIN, out temp)) { // 不等于当天 if (temp.ExpirationTime != DateTime.Now.Date) { LoginNum = 1; - LoginNumDict.TryUpdate(nELoginProperty.VIN, (LoginNum, DateTime.Now.Date), temp); + LoginNumDict.TryUpdate(nELoginProperty.VIN, new LoginInfo { LoginNum = LoginNum, ExpirationTime=DateTime.Now.Date }, temp); } else {// 自增1 更新字典 LoginNum = temp.LoginNum++; - LoginNumDict.TryUpdate(nELoginProperty.VIN, (LoginNum, DateTime.Now.Date), temp); + LoginNumDict.TryUpdate(nELoginProperty.VIN, new LoginInfo { LoginNum = LoginNum, ExpirationTime = DateTime.Now.Date }, temp); } } } else { LoginNum = 1; - LoginNumDict.TryAdd(nELoginProperty.VIN, (LoginNum, DateTime.Now.Date)); + LoginNumDict.TryAdd(nELoginProperty.VIN, new LoginInfo { LoginNum = LoginNum, ExpirationTime = DateTime.Now.Date }); } SIM = nELoginProperty.SIM; BatteryCount = nELoginProperty.BatteryCount; diff --git a/src/GBNewEnergy.Protocol/UpStream/NELogoutUpStream.cs b/src/GBNewEnergy.Protocol/UpStream/NELogoutUpStream.cs index 25aa27a..1be54f8 100644 --- a/src/GBNewEnergy.Protocol/UpStream/NELogoutUpStream.cs +++ b/src/GBNewEnergy.Protocol/UpStream/NELogoutUpStream.cs @@ -12,15 +12,18 @@ namespace GBNewEnergy.Protocol.UpStream /// public class NELogoutUpStream : NEBodies { - public NELogoutUpStream(byte[] buffer) : base(buffer) - {} + public NELogoutUpStream(INEProperties nEProperties, NEGlobalConfigs nEConfigs) : base(nEProperties, nEConfigs) + { + } - public NELogoutUpStream(INEProperties nEProperties) : base(nEProperties){} + public NELogoutUpStream(byte[] buffer, NEGlobalConfigs nEConfigs) : base(buffer, nEConfigs) + { + } protected override void InitializeProperties(INEProperties nEProperties) { NELogoutProperty nELogoutProperty = (NELogoutProperty)nEProperties; - (ushort LoginNum, DateTime ExpirationTime) temp; + LoginInfo temp; if (LoginNumDict.TryGetValue(nELogoutProperty.VIN, out temp)) { LoginNum = temp.LoginNum; diff --git a/src/GBNewEnergy.sln b/src/GBNewEnergy.sln index 7de74ac..36b30b5 100644 --- a/src/GBNewEnergy.sln +++ b/src/GBNewEnergy.sln @@ -7,6 +7,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GBNewEnergy.Protocol", "GBN EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GBNewEnergy.Protocol.Test", "GBNewEnergy.Protocol.Test\GBNewEnergy.Protocol.Test.csproj", "{AAA669F5-E689-40CA-97EF-B5BA672B11DD}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "NEEncrypts.Tests", "NEEncrypts.Tests", "{1D162FFB-5944-470C-B6F0-8F7387C706DF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NEEncryptsNET4.Test", "NEEncrypts.Tests\NEEncryptsNET4.Test\NEEncryptsNET4.Test.csproj", "{7C7C1511-EB17-4DBF-B04F-EFE8FF4ECE92}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NEEncryptsNET45.Test", "NEEncrypts.Tests\NEEncryptsNET45.Test\NEEncryptsNET45.Test.csproj", "{1ED77002-D542-4013-B71A-C52CE92D5747}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NEEncryptsNETCore.Test", "NEEncrypts.Tests\NEEncryptsNETCore.Test\NEEncryptsNETCore.Test.csproj", "{361FD688-A06B-4D68-9B76-93C6559278E8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,10 +29,27 @@ Global {AAA669F5-E689-40CA-97EF-B5BA672B11DD}.Debug|Any CPU.Build.0 = Debug|Any CPU {AAA669F5-E689-40CA-97EF-B5BA672B11DD}.Release|Any CPU.ActiveCfg = Release|Any CPU {AAA669F5-E689-40CA-97EF-B5BA672B11DD}.Release|Any CPU.Build.0 = Release|Any CPU + {7C7C1511-EB17-4DBF-B04F-EFE8FF4ECE92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7C7C1511-EB17-4DBF-B04F-EFE8FF4ECE92}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7C7C1511-EB17-4DBF-B04F-EFE8FF4ECE92}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7C7C1511-EB17-4DBF-B04F-EFE8FF4ECE92}.Release|Any CPU.Build.0 = Release|Any CPU + {1ED77002-D542-4013-B71A-C52CE92D5747}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1ED77002-D542-4013-B71A-C52CE92D5747}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1ED77002-D542-4013-B71A-C52CE92D5747}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1ED77002-D542-4013-B71A-C52CE92D5747}.Release|Any CPU.Build.0 = Release|Any CPU + {361FD688-A06B-4D68-9B76-93C6559278E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {361FD688-A06B-4D68-9B76-93C6559278E8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {361FD688-A06B-4D68-9B76-93C6559278E8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {361FD688-A06B-4D68-9B76-93C6559278E8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {7C7C1511-EB17-4DBF-B04F-EFE8FF4ECE92} = {1D162FFB-5944-470C-B6F0-8F7387C706DF} + {1ED77002-D542-4013-B71A-C52CE92D5747} = {1D162FFB-5944-470C-B6F0-8F7387C706DF} + {361FD688-A06B-4D68-9B76-93C6559278E8} = {1D162FFB-5944-470C-B6F0-8F7387C706DF} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {E7AF20B3-4327-4DF7-B503-9FE08AA92A75} EndGlobalSection diff --git a/src/NEEncrypts.Tests/NEEncryptsNET4.Test/NEEncryptsNET4.Test.csproj b/src/NEEncrypts.Tests/NEEncryptsNET4.Test/NEEncryptsNET4.Test.csproj new file mode 100644 index 0000000..2badda2 --- /dev/null +++ b/src/NEEncrypts.Tests/NEEncryptsNET4.Test/NEEncryptsNET4.Test.csproj @@ -0,0 +1,47 @@ + + + + + Debug + AnyCPU + {7C7C1511-EB17-4DBF-B04F-EFE8FF4ECE92} + Exe + NEEncryptsNET4.Test + NEEncryptsNET4.Test + v4.0 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/NEEncrypts.Tests/NEEncryptsNET4.Test/Program.cs b/src/NEEncrypts.Tests/NEEncryptsNET4.Test/Program.cs new file mode 100644 index 0000000..7a26f1e --- /dev/null +++ b/src/NEEncrypts.Tests/NEEncryptsNET4.Test/Program.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; + +namespace NEEncryptsNET4.Test +{ + class Program + { + 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()) + { + 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; + } + } +} diff --git a/src/NEEncrypts.Tests/NEEncryptsNET4.Test/Properties/AssemblyInfo.cs b/src/NEEncrypts.Tests/NEEncryptsNET4.Test/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..dc3d42f --- /dev/null +++ b/src/NEEncrypts.Tests/NEEncryptsNET4.Test/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("NEEncryptsNET4.Test")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("NEEncryptsNET4.Test")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("7c7c1511-eb17-4dbf-b04f-efe8ff4ece92")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/NEEncrypts.Tests/NEEncryptsNET45.Test/App.config b/src/NEEncrypts.Tests/NEEncryptsNET45.Test/App.config new file mode 100644 index 0000000..8e15646 --- /dev/null +++ b/src/NEEncrypts.Tests/NEEncryptsNET45.Test/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/NEEncrypts.Tests/NEEncryptsNET45.Test/NEEncryptsNET45.Test.csproj b/src/NEEncrypts.Tests/NEEncryptsNET45.Test/NEEncryptsNET45.Test.csproj new file mode 100644 index 0000000..d13b991 --- /dev/null +++ b/src/NEEncrypts.Tests/NEEncryptsNET45.Test/NEEncryptsNET45.Test.csproj @@ -0,0 +1,51 @@ + + + + + Debug + AnyCPU + {1ED77002-D542-4013-B71A-C52CE92D5747} + Exe + NEEncryptsNET45.Test + NEEncryptsNET45.Test + v4.5 + 512 + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/NEEncrypts.Tests/NEEncryptsNET45.Test/Program.cs b/src/NEEncrypts.Tests/NEEncryptsNET45.Test/Program.cs new file mode 100644 index 0000000..a41c47c --- /dev/null +++ b/src/NEEncrypts.Tests/NEEncryptsNET45.Test/Program.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace NEEncryptsNET45.Test +{ + class Program + { + static void Main(string[] args) + { + } + } +} diff --git a/src/NEEncrypts.Tests/NEEncryptsNET45.Test/Properties/AssemblyInfo.cs b/src/NEEncrypts.Tests/NEEncryptsNET45.Test/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..4fc5026 --- /dev/null +++ b/src/NEEncrypts.Tests/NEEncryptsNET45.Test/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("NEEncryptsNET45.Test")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("NEEncryptsNET45.Test")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("1ed77002-d542-4013-b71a-c52ce92d5747")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 +// 方法是按如下所示使用“*”: : +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/NEEncrypts.Tests/NEEncryptsNETCore.Test/NEEncryptsNETCore.Test.csproj b/src/NEEncrypts.Tests/NEEncryptsNETCore.Test/NEEncryptsNETCore.Test.csproj new file mode 100644 index 0000000..23df604 --- /dev/null +++ b/src/NEEncrypts.Tests/NEEncryptsNETCore.Test/NEEncryptsNETCore.Test.csproj @@ -0,0 +1,8 @@ + + + + Exe + netcoreapp2.1 + + + diff --git a/src/NEEncrypts.Tests/NEEncryptsNETCore.Test/Program.cs b/src/NEEncrypts.Tests/NEEncryptsNETCore.Test/Program.cs new file mode 100644 index 0000000..bcec350 --- /dev/null +++ b/src/NEEncrypts.Tests/NEEncryptsNETCore.Test/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace NEEncryptsNETCore.Test +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +}