diff --git a/src/JT809.Protocol.Test/BaseTest.cs b/src/JT809.Protocol.Test/BaseTest.cs index 3e4f5c6..669f4f8 100644 --- a/src/JT809.Protocol.Test/BaseTest.cs +++ b/src/JT809.Protocol.Test/BaseTest.cs @@ -9,6 +9,12 @@ namespace JT809.Protocol.Test { public class BaseTest { - public JT809EncryptConfig JT809EncryptConfig = new JT809EncryptConfig(); + public JT809EncryptConfig JT809EncryptConfig = new JT809EncryptConfig() + { + Key=0x01, + IA1 = 20000000, + IC1= 30000000, + M1= 10000000 + }; } } diff --git a/src/JT809.Protocol.Test/Escape/JT809EscapeImplTest.cs b/src/JT809.Protocol.Test/Escape/JT809EscapeImplTest.cs index 995cb1d..ff38ea5 100644 --- a/src/JT809.Protocol.Test/Escape/JT809EscapeImplTest.cs +++ b/src/JT809.Protocol.Test/Escape/JT809EscapeImplTest.cs @@ -1,7 +1,11 @@ using System; +using System.Diagnostics; +using System.Linq; using System.Text; +using JT809.Protocol.Configs; using JT809.Protocol.Encrypt; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json; namespace JT809.Protocol.Test.Escape { @@ -10,32 +14,29 @@ namespace JT809.Protocol.Test.Escape { public IEncrypt encrypt; - public IEncrypt encrypt2; - public JT809EscapeImplTest() { - encrypt = new JT809EncryptImpl(0x01,base.JT809EncryptConfig); - encrypt2 = new JT809EncryptImpl(0x01, base.JT809EncryptConfig); + encrypt = new JT809EncryptImpl(base.JT809EncryptConfig); } - [TestMethod] public void Encrypt() { - var pre = System.Text.Encoding.UTF8.GetBytes("smallchi"); - encrypt.Encrypt(pre); + byte[] buffer = System.Text.Encoding.UTF8.GetBytes("smallchi"); + Debug.WriteLine(JsonConvert.SerializeObject(buffer.Select(s => s).ToList())); + encrypt.Encrypt(buffer); + Debug.WriteLine(JsonConvert.SerializeObject(buffer.Select(s => s).ToList())); } [TestMethod] public void Decrypt() { - byte[] buffer = System.Text.Encoding.UTF8.GetBytes("smallchi"); - byte[] oldBuffer=new byte[buffer.Length]; - buffer.CopyTo(oldBuffer, 0); - encrypt.Encrypt(buffer); - encrypt2.Decrypt(buffer); - string name=System.Text.Encoding.UTF8.GetString(buffer); - Assert.AreEqual("smallchi",name); + byte[] buffer =new byte [8]{ 92, 113, 125, 112, 112, 127, 116, 117}; + Debug.WriteLine(JsonConvert.SerializeObject(buffer.Select(s => s).ToList())); + encrypt.Decrypt(buffer); + Debug.WriteLine(JsonConvert.SerializeObject(buffer.Select(s => s).ToList())); + string name = System.Text.Encoding.UTF8.GetString(buffer); + Assert.AreEqual("smallchi", name); } } } diff --git a/src/JT809.Protocol.Test/JT809.Protocol.Test.csproj b/src/JT809.Protocol.Test/JT809.Protocol.Test.csproj index c6ca57e..fc90444 100644 --- a/src/JT809.Protocol.Test/JT809.Protocol.Test.csproj +++ b/src/JT809.Protocol.Test/JT809.Protocol.Test.csproj @@ -1,6 +1,6 @@  - + Debug AnyCPU @@ -39,10 +39,13 @@ - ..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + ..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll - ..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + ..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + ..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll @@ -68,8 +71,8 @@ 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 - - + + - + \ No newline at end of file diff --git a/src/JT809.Protocol.Test/packages.config b/src/JT809.Protocol.Test/packages.config index 6384d0b..b48b200 100644 --- a/src/JT809.Protocol.Test/packages.config +++ b/src/JT809.Protocol.Test/packages.config @@ -1,5 +1,6 @@  - - + + + \ No newline at end of file diff --git a/src/JT809.Protocol/Configs/JT809EncryptConfig.cs b/src/JT809.Protocol/Configs/JT809EncryptConfig.cs index eaf20ce..b0a4fed 100644 --- a/src/JT809.Protocol/Configs/JT809EncryptConfig.cs +++ b/src/JT809.Protocol/Configs/JT809EncryptConfig.cs @@ -2,8 +2,9 @@ { public class JT809EncryptConfig { - public uint M1 { get; set; } = 10000000; - public uint IA1 { get; set; } = 20000000; - public uint IC1 { get; set; } = 30000000; + public uint M1 { get; set; } + public uint IA1 { get; set; } + public uint IC1 { get; set; } + public uint Key { get; set; } } } diff --git a/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs b/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs index 0f4a900..03a93e9 100644 --- a/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs +++ b/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs @@ -9,12 +9,10 @@ namespace JT809.Protocol.Encrypt { private JT809EncryptConfig Config; - private uint Key { get; set; } - public JT809EncryptImpl(uint key,JT809EncryptConfig config) + public JT809EncryptImpl(JT809EncryptConfig config) { Config = config; - Key = key; } public void Decrypt(byte[] buffer) @@ -24,9 +22,9 @@ namespace JT809.Protocol.Encrypt public void Encrypt(byte[] buffer) { - if (0 == Key) + if (0 == Config.Key) { - Key = 1; + Config.Key = 1; } uint mkey = Config.M1; if (0 == mkey) @@ -35,8 +33,8 @@ namespace JT809.Protocol.Encrypt } for (int idx = 0; idx < buffer.Length; idx++) { - Key = Config.IA1 * (Key % mkey) + Config.IC1; - buffer[idx] ^= (byte)((Key >> 20) & 0xFF); + Config.Key = Config.IA1 * (Config.Key % mkey) + Config.IC1; + buffer[idx] ^= (byte)((Config.Key >> 20) & 0xFF); } } } diff --git a/src/JT809.Protocol/ProtocolPacket/EncryptEnum.cs b/src/JT809.Protocol/ProtocolPacket/EncryptEnum.cs new file mode 100644 index 0000000..f2cb699 --- /dev/null +++ b/src/JT809.Protocol/ProtocolPacket/EncryptEnum.cs @@ -0,0 +1,11 @@ +namespace JT809.Protocol.ProtocolPacket +{ + /// + /// 报文加密标识位 b: 0 表示报文不加密,1 表示报文加密。 + /// + public enum EncryptEnum : byte + { + No = 0X00, + Yes = 0X01, + } +} diff --git a/src/JT809.Protocol/ProtocolPacket/Header.cs b/src/JT809.Protocol/ProtocolPacket/Header.cs new file mode 100644 index 0000000..abfc0d9 --- /dev/null +++ b/src/JT809.Protocol/ProtocolPacket/Header.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace JT809.Protocol.ProtocolPacket +{ + /// + /// Message Header 数据头 + /// + public class Header + { + public Version Version { get; private set; } + public EncryptEnum EncryptEnum { get; private set; } + } +} diff --git a/src/JT809.Protocol/ProtocolPacket/Version.cs b/src/JT809.Protocol/ProtocolPacket/Version.cs new file mode 100644 index 0000000..6801953 --- /dev/null +++ b/src/JT809.Protocol/ProtocolPacket/Version.cs @@ -0,0 +1,46 @@ +namespace JT809.Protocol.ProtocolPacket +{ + /// + /// 协议版本好标识,上下级平台之间采用的标准协议版编号; + /// 长度为 3 个字节来表示, + /// 0x01 0x02 0x0F 标识的版本号是 v1.2.15,以此类推。 + /// + public class Version + { + public byte[] Buffer { get; } = new byte[3]; + private const int MajorIndex = 0; + private const int MinorIndex = 1; + private const int BuildIndex = 2; + public byte Major + { + get { return Buffer[MajorIndex]; } + private set { Buffer[MajorIndex] = value; } + } + public byte Minor + { + get { return Buffer[MinorIndex]; } + private set { Buffer[MinorIndex] = value; } + } + public byte Build + { + get { return Buffer[BuildIndex]; } + private set { Buffer[BuildIndex] = value; } + } + public Version() + { + Major = 1; + Minor = 0; + Build = 0; + } + public Version(byte major, byte minor, byte buid) + { + Major = major; + Minor = minor; + Build = buid; + } + public override string ToString() + { + return $"{Major}.{Minor}.{Build}"; + } + } +}