2.增加加密功能(存在操作系统兼容问题,待测试 【mono、dotnetcore、dotnet】 ) 3.增加加密多环境测试(待测试)tags/1.0.0
@@ -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<string>() { "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<string>() { "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(); | |||
@@ -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++; | |||
} | |||
} | |||
/// <summary> | |||
/// 异或 | |||
/// </summary> | |||
@@ -1,7 +1,7 @@ | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<PropertyGroup> | |||
<TargetFramework>netstandard2.0</TargetFramework> | |||
<TargetFrameworks>netstandard2.0;net40;net45</TargetFrameworks> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
@@ -13,6 +13,14 @@ | |||
<Compile Remove="NEUpStreamBase.cs" /> | |||
</ItemGroup> | |||
<ItemGroup Condition=" '$(TargetFramework)' == 'net40' "> | |||
</ItemGroup> | |||
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' "> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<Folder Include="DownStream\" /> | |||
<Folder Include="Properties\" /> | |||
@@ -11,11 +11,11 @@ namespace GBNewEnergy.Protocol | |||
/// VIN - 登录流水号,过期时间(每天置1) | |||
/// 车载终端登入一次,登入流水号自动加1,从1开始循环累加,最大值为65531,循环周期为天 | |||
/// </summary> | |||
protected static readonly ConcurrentDictionary<string, (ushort LoginNum, DateTime ExpirationTime)> LoginNumDict; | |||
protected static readonly ConcurrentDictionary<string, LoginInfo> LoginNumDict; | |||
static NEBodies() | |||
{ | |||
LoginNumDict = new ConcurrentDictionary<string, (ushort LoginNum, DateTime ExpirationTime)>(); | |||
LoginNumDict = new ConcurrentDictionary<string, LoginInfo>(); | |||
} | |||
/// <summary> | |||
@@ -30,8 +30,14 @@ namespace GBNewEnergy.Protocol | |||
/// </summary> | |||
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; } | |||
} | |||
} | |||
} |
@@ -14,14 +14,14 @@ namespace GBNewEnergy.Protocol | |||
/// <param name="msgId"></param> | |||
/// <param name="buf"></param> | |||
/// <returns></returns> | |||
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; | |||
} | |||
@@ -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(); | |||
} | |||
@@ -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; | |||
} | |||
@@ -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; | |||
/// <summary> | |||
/// 盐字节必须为至少8个字节 | |||
/// </summary> | |||
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; | |||
} | |||
} | |||
} |
@@ -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; | |||
/// <summary> | |||
/// 盐字节必须为至少8个字节 | |||
/// </summary> | |||
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; | |||
} | |||
} | |||
} | |||
} |
@@ -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(); | |||
@@ -0,0 +1,29 @@ | |||
using GBNewEnergy.Protocol.Enums; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace GBNewEnergy.Protocol | |||
{ | |||
public class NEGlobalConfigs | |||
{ | |||
/// <summary> | |||
/// 密钥 | |||
/// </summary> | |||
public string NEEncryptKey { get; set; } | |||
/// <summary> | |||
/// 密钥 | |||
/// </summary> | |||
public byte[] NEEncryptKeyBytes => Encoding.UTF8.GetBytes(NEEncryptKey); | |||
/// <summary> | |||
/// 数据单元加密方式 | |||
/// 0x01:数据不加密;0x02:数据经过 RSA 算法加密;0x03:数据经过 AES128 位算法加密;“0xFE”表示异常,“0xFF”表示无效 | |||
/// </summary> | |||
public NEEncryptMethod EncryptMethod { get; set; } = NEEncryptMethod.None; | |||
/// <summary> | |||
/// 数据单元加密 | |||
/// 当数据单元存在加密时,应先加密后校验,先校验后解密 | |||
/// </summary> | |||
public INEEncrypt Encrypt { get; set; } | |||
} | |||
} |
@@ -12,16 +12,16 @@ namespace GBNewEnergy.Protocol | |||
/// </summary> | |||
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) | |||
{ } | |||
/// <summary> | |||
@@ -70,11 +70,6 @@ namespace GBNewEnergy.Protocol | |||
/// 数据体 | |||
/// </summary> | |||
public NEBodies Bodies { get; protected set; } | |||
/// <summary> | |||
/// 数据单元加密 | |||
/// 当数据单元存在加密时,应先加密后校验,先校验后解密 | |||
/// </summary> | |||
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); | |||
@@ -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; } | |||
} | |||
} |
@@ -12,12 +12,11 @@ namespace GBNewEnergy.Protocol.UpStream | |||
/// </summary> | |||
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; | |||
@@ -12,15 +12,18 @@ namespace GBNewEnergy.Protocol.UpStream | |||
/// </summary> | |||
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; | |||
@@ -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 | |||
@@ -0,0 +1,47 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | |||
<PropertyGroup> | |||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | |||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | |||
<ProjectGuid>{7C7C1511-EB17-4DBF-B04F-EFE8FF4ECE92}</ProjectGuid> | |||
<OutputType>Exe</OutputType> | |||
<RootNamespace>NEEncryptsNET4.Test</RootNamespace> | |||
<AssemblyName>NEEncryptsNET4.Test</AssemblyName> | |||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion> | |||
<FileAlignment>512</FileAlignment> | |||
</PropertyGroup> | |||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | |||
<PlatformTarget>AnyCPU</PlatformTarget> | |||
<DebugSymbols>true</DebugSymbols> | |||
<DebugType>full</DebugType> | |||
<Optimize>false</Optimize> | |||
<OutputPath>bin\Debug\</OutputPath> | |||
<DefineConstants>DEBUG;TRACE</DefineConstants> | |||
<ErrorReport>prompt</ErrorReport> | |||
<WarningLevel>4</WarningLevel> | |||
</PropertyGroup> | |||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | |||
<PlatformTarget>AnyCPU</PlatformTarget> | |||
<DebugType>pdbonly</DebugType> | |||
<Optimize>true</Optimize> | |||
<OutputPath>bin\Release\</OutputPath> | |||
<DefineConstants>TRACE</DefineConstants> | |||
<ErrorReport>prompt</ErrorReport> | |||
<WarningLevel>4</WarningLevel> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<Reference Include="System" /> | |||
<Reference Include="System.Core" /> | |||
<Reference Include="System.Xml.Linq" /> | |||
<Reference Include="System.Data.DataSetExtensions" /> | |||
<Reference Include="Microsoft.CSharp" /> | |||
<Reference Include="System.Data" /> | |||
<Reference Include="System.Xml" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<Compile Include="Program.cs" /> | |||
<Compile Include="Properties\AssemblyInfo.cs" /> | |||
</ItemGroup> | |||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | |||
</Project> |
@@ -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; | |||
} | |||
} | |||
} |
@@ -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")] |
@@ -0,0 +1,6 @@ | |||
<?xml version="1.0" encoding="utf-8" ?> | |||
<configuration> | |||
<startup> | |||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> | |||
</startup> | |||
</configuration> |
@@ -0,0 +1,51 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | |||
<PropertyGroup> | |||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | |||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | |||
<ProjectGuid>{1ED77002-D542-4013-B71A-C52CE92D5747}</ProjectGuid> | |||
<OutputType>Exe</OutputType> | |||
<RootNamespace>NEEncryptsNET45.Test</RootNamespace> | |||
<AssemblyName>NEEncryptsNET45.Test</AssemblyName> | |||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | |||
<FileAlignment>512</FileAlignment> | |||
</PropertyGroup> | |||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | |||
<PlatformTarget>AnyCPU</PlatformTarget> | |||
<DebugSymbols>true</DebugSymbols> | |||
<DebugType>full</DebugType> | |||
<Optimize>false</Optimize> | |||
<OutputPath>bin\Debug\</OutputPath> | |||
<DefineConstants>DEBUG;TRACE</DefineConstants> | |||
<ErrorReport>prompt</ErrorReport> | |||
<WarningLevel>4</WarningLevel> | |||
</PropertyGroup> | |||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | |||
<PlatformTarget>AnyCPU</PlatformTarget> | |||
<DebugType>pdbonly</DebugType> | |||
<Optimize>true</Optimize> | |||
<OutputPath>bin\Release\</OutputPath> | |||
<DefineConstants>TRACE</DefineConstants> | |||
<ErrorReport>prompt</ErrorReport> | |||
<WarningLevel>4</WarningLevel> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<Reference Include="System" /> | |||
<Reference Include="System.Core" /> | |||
<Reference Include="System.Xml.Linq" /> | |||
<Reference Include="System.Data.DataSetExtensions" /> | |||
<Reference Include="Microsoft.CSharp" /> | |||
<Reference Include="System.Data" /> | |||
<Reference Include="System.Net.Http" /> | |||
<Reference Include="System.Xml" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<Compile Include="Program.cs" /> | |||
<Compile Include="Properties\AssemblyInfo.cs" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<None Include="App.config" /> | |||
</ItemGroup> | |||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | |||
</Project> |
@@ -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) | |||
{ | |||
} | |||
} | |||
} |
@@ -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")] |
@@ -0,0 +1,8 @@ | |||
<Project Sdk="Microsoft.NET.Sdk"> | |||
<PropertyGroup> | |||
<OutputType>Exe</OutputType> | |||
<TargetFramework>netcoreapp2.1</TargetFramework> | |||
</PropertyGroup> | |||
</Project> |
@@ -0,0 +1,12 @@ | |||
using System; | |||
namespace NEEncryptsNETCore.Test | |||
{ | |||
class Program | |||
{ | |||
static void Main(string[] args) | |||
{ | |||
Console.WriteLine("Hello World!"); | |||
} | |||
} | |||
} |