From 3ac7fe6d35cc77d8c6a990cbfdf63c879a136d95 Mon Sep 17 00:00:00 2001 From: SmallChi <564952747@qq.com> Date: Sat, 13 Oct 2018 16:47:57 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"1.=E4=BF=AE=E6=94=B9=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E9=85=8D=E7=BD=AE=E5=8F=8A=E6=8E=A5=E5=8F=A3=EF=BC=88?= =?UTF-8?q?=E5=AF=B9=E5=BA=94=E6=96=87=E6=A1=A3=EF=BC=89;"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 2b668f1b1f2e4fc6317ddcb343641616bcbbe94f. --- .../JT809SerializerContext.cs | 2 +- ...Extensions.DependencyInjection.Test.csproj | 27 ---------- .../Program.cs | 27 ---------- .../appsettings.json | 29 ----------- .../DependencyInjectionExtensions.cs | 52 ------------------- ...ocol.Extensions.DependencyInjection.csproj | 15 ------ .../Options/JT809Options.cs | 23 -------- .../JT809Encrypt/JT809EncryptTest.cs | 7 +-- src/JT809.Protocol.Test/JT809HeaderTest.cs | 18 +------ .../JT809_0x1001EncryptPackageTest.cs | 12 +++-- .../JT809Packages/JT809_0x1001PackageTest.cs | 2 +- .../JT809_0x9400_0x9401PackageTest.cs | 4 +- src/JT809.Protocol.sln | 14 +---- src/JT809.Protocol/IJT809Encrypt.cs | 4 +- .../JT809Configs/JT809EncryptOptions.cs | 1 + .../JT809Configs/JT809HeaderOptions.cs | 2 +- .../JT809Encrypt/JT809EncryptImpl.cs | 14 ++--- .../JT809Extensions/JT809BCDExtensions.cs | 20 ++----- .../JT809Formatters/JT809HeaderFormatter.cs | 4 +- .../JT809Formatters/JT809PackageFormatter.cs | 4 +- .../JT809_0x9500_0x9505Formatter.cs | 2 +- src/JT809.Protocol/JT809GlobalConfig.cs | 13 ----- src/JT809.Protocol/JT809Header.cs | 3 +- src/JT809.Protocol/JT809Header_Version.cs | 12 ++--- 24 files changed, 44 insertions(+), 267 deletions(-) delete mode 100644 src/JT809.Protocol.Extensions.DependencyInjection.Test/JT809.Protocol.Extensions.DependencyInjection.Test.csproj delete mode 100644 src/JT809.Protocol.Extensions.DependencyInjection.Test/Program.cs delete mode 100644 src/JT809.Protocol.Extensions.DependencyInjection.Test/appsettings.json delete mode 100644 src/JT809.Protocol.Extensions.DependencyInjection/DependencyInjectionExtensions.cs delete mode 100644 src/JT809.Protocol.Extensions.DependencyInjection/JT809.Protocol.Extensions.DependencyInjection.csproj delete mode 100644 src/JT809.Protocol.Extensions.DependencyInjection/Options/JT809Options.cs diff --git a/src/JT809.Protocol.Benchmark/JT809SerializerContext.cs b/src/JT809.Protocol.Benchmark/JT809SerializerContext.cs index 040d128..37492c8 100644 --- a/src/JT809.Protocol.Benchmark/JT809SerializerContext.cs +++ b/src/JT809.Protocol.Benchmark/JT809SerializerContext.cs @@ -50,7 +50,7 @@ namespace JT809.Protocol.Benchmark MsgSN = 1666, EncryptKey = 9999, EncryptFlag = JT809Header_Encrypt.None, - Version = "010000", + Version = new JT809Header_Version(1, 0, 0), BusinessType = JT809Enums.JT809BusinessType.从链路报警信息交互消息, MsgGNSSCENTERID = 20180920, }; diff --git a/src/JT809.Protocol.Extensions.DependencyInjection.Test/JT809.Protocol.Extensions.DependencyInjection.Test.csproj b/src/JT809.Protocol.Extensions.DependencyInjection.Test/JT809.Protocol.Extensions.DependencyInjection.Test.csproj deleted file mode 100644 index 87d789f..0000000 --- a/src/JT809.Protocol.Extensions.DependencyInjection.Test/JT809.Protocol.Extensions.DependencyInjection.Test.csproj +++ /dev/null @@ -1,27 +0,0 @@ - - - - Exe - netcoreapp2.1 - 7.3 - - - - - - - - - - - - - - - - - Always - - - - diff --git a/src/JT809.Protocol.Extensions.DependencyInjection.Test/Program.cs b/src/JT809.Protocol.Extensions.DependencyInjection.Test/Program.cs deleted file mode 100644 index 63b1994..0000000 --- a/src/JT809.Protocol.Extensions.DependencyInjection.Test/Program.cs +++ /dev/null @@ -1,27 +0,0 @@ -using JT809.Protocol.Extensions.DependencyInjection.Options; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using System; -using System.Threading.Tasks; - -namespace JT809.Protocol.Extensions.DependencyInjection.Test -{ - class Program - { - static async Task Main(string[] args) - { - var serverHostBuilder = new HostBuilder() - .ConfigureAppConfiguration((hostingContext, config) => - { - config.SetBasePath(AppDomain.CurrentDomain.BaseDirectory); - config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true); - }) - .ConfigureServices((hostContext, services) => - { - services.AddJT809Configure(hostContext.Configuration.GetSection("JT809Options")); - }); - await serverHostBuilder.RunConsoleAsync(); - } - } -} diff --git a/src/JT809.Protocol.Extensions.DependencyInjection.Test/appsettings.json b/src/JT809.Protocol.Extensions.DependencyInjection.Test/appsettings.json deleted file mode 100644 index 726373d..0000000 --- a/src/JT809.Protocol.Extensions.DependencyInjection.Test/appsettings.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "Logging": { - "IncludeScopes": false, - "Debug": { - "LogLevel": { - "Default": "Trace" - } - }, - "Console": { - "LogLevel": { - "Default": "Trace" - } - } - }, - "JT809Options": { - "HeaderOptions": { - "MsgGNSSCENTERID": 20181012, - "EncryptFlag": "None", - "EncryptKey": 9999, - "Version": "1.0.0" - }, - "EncryptOptions": { - "M1": 0, - "IA1": 0, - "IC1": 0, - "Key": 0 - } - } -} diff --git a/src/JT809.Protocol.Extensions.DependencyInjection/DependencyInjectionExtensions.cs b/src/JT809.Protocol.Extensions.DependencyInjection/DependencyInjectionExtensions.cs deleted file mode 100644 index 458b219..0000000 --- a/src/JT809.Protocol.Extensions.DependencyInjection/DependencyInjectionExtensions.cs +++ /dev/null @@ -1,52 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; -using System; -using System.Collections.Generic; -using System.Text; -using JT809.Protocol.JT809Configs; -using Microsoft.Extensions.Configuration; -using JT809.Protocol.JT809Encrypt; -using JT809.Protocol.Extensions.DependencyInjection.Options; -using Microsoft.Extensions.Options; - -namespace JT809.Protocol.Extensions.DependencyInjection -{ - public static class DependencyInjectionExtensions - { - public static IServiceCollection AddJT809Configure(this IServiceCollection services, IConfiguration configuration) - { - services.Configure(configuration); - ServiceProvider serviceProvider = services.BuildServiceProvider(); - JT809Options options = serviceProvider.GetRequiredService>().Value; - var versions = options.Version.Split(new string []{ "." }, StringSplitOptions.RemoveEmptyEntries); - if (versions.Length == 3) - { - options.HeaderOptions.Version = new JT809Header_Version(byte.Parse(versions[0]), byte.Parse(versions[1]), byte.Parse(versions[2])); - } - else if(versions.Length == 2) - { - options.HeaderOptions.Version = new JT809Header_Version(byte.Parse(versions[0]), byte.Parse(versions[1]),0); - } - else if (versions.Length == 1) - { - options.HeaderOptions.Version = new JT809Header_Version(byte.Parse(versions[0]), 0, 0); - } - JT809GlobalConfig.Instance.SetHeaderOptions(options.HeaderOptions); - JT809GlobalConfig.Instance.SetSkipCRCCode(options.SkipCRCCode); - if (options.HeaderOptions.EncryptFlag == JT809Header_Encrypt.Common) - { - JT809GlobalConfig.Instance.SetEncrypt(new JT809EncryptImpl(options.EncryptOptions)); - } - try - { - var msgSNDistributedImpl = services.BuildServiceProvider().GetRequiredService(); - JT809GlobalConfig.Instance.SetMsgSNDistributed(msgSNDistributedImpl); - } - catch - { - - } - return services; - } - } -} diff --git a/src/JT809.Protocol.Extensions.DependencyInjection/JT809.Protocol.Extensions.DependencyInjection.csproj b/src/JT809.Protocol.Extensions.DependencyInjection/JT809.Protocol.Extensions.DependencyInjection.csproj deleted file mode 100644 index cafd451..0000000 --- a/src/JT809.Protocol.Extensions.DependencyInjection/JT809.Protocol.Extensions.DependencyInjection.csproj +++ /dev/null @@ -1,15 +0,0 @@ - - - - netstandard2.0 - - - - - - - - - - - diff --git a/src/JT809.Protocol.Extensions.DependencyInjection/Options/JT809Options.cs b/src/JT809.Protocol.Extensions.DependencyInjection/Options/JT809Options.cs deleted file mode 100644 index faff3dd..0000000 --- a/src/JT809.Protocol.Extensions.DependencyInjection/Options/JT809Options.cs +++ /dev/null @@ -1,23 +0,0 @@ -using JT809.Protocol.JT809Configs; -using System; -using System.Collections.Generic; -using System.Text; - -namespace JT809.Protocol.Extensions.DependencyInjection.Options -{ - public class JT809Options - { - public JT809EncryptOptions EncryptOptions { get; set; } - public JT809HeaderOptions HeaderOptions { get; set; } - /// - /// 版本号: - /// 默认 1.0.0 - /// - public string Version { get; set; } - /// - /// 设置跳过校验码 - /// 场景:测试的时候,可能需要收到改数据,所以测试的时候有用 - /// - public bool SkipCRCCode { get; set; } - } -} diff --git a/src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs b/src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs index 5151ec5..767be5d 100644 --- a/src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs +++ b/src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs @@ -14,7 +14,8 @@ namespace JT809.Protocol.Test.JT809Encrypt { IA1 = 20000000, IC1 = 20000000, - M1 = 30000000 + M1 = 30000000, + Key = 256178, }; [Fact] @@ -25,7 +26,7 @@ namespace JT809.Protocol.Test.JT809Encrypt 01,02,03,04,05,06,07 }; IJT809Encrypt jT809Encrypt = new JT809EncryptImpl(options); - var data = jT809Encrypt.Encrypt(bytes, 256178).ToHexString(); + var data = jT809Encrypt.Encrypt(bytes).ToHexString(); //"D3 4C 70 78 A7 3A 41" } @@ -34,7 +35,7 @@ namespace JT809.Protocol.Test.JT809Encrypt { byte[] bytes = "D3 4C 70 78 A7 3A 41".ToHexBytes(); IJT809Encrypt jT809Encrypt = new JT809EncryptImpl(options); - var data = jT809Encrypt.Decrypt(bytes, 256178); + var data = jT809Encrypt.Decrypt(bytes); Assert.Equal(new byte[] { 01,02,03,04,05,06,07 diff --git a/src/JT809.Protocol.Test/JT809HeaderTest.cs b/src/JT809.Protocol.Test/JT809HeaderTest.cs index e96f1c4..3c4926e 100644 --- a/src/JT809.Protocol.Test/JT809HeaderTest.cs +++ b/src/JT809.Protocol.Test/JT809HeaderTest.cs @@ -19,7 +19,7 @@ namespace JT809.Protocol.Test jT809Header.MsgSN = 1024; jT809Header.BusinessType = JT809Enums.JT809BusinessType.从链路静态信息交换消息; jT809Header.MsgGNSSCENTERID = 1200; - jT809Header.Version = "010000"; + jT809Header.Version = new JT809Header_Version(); jT809Header.EncryptFlag = JT809Header_Encrypt.None; jT809Header.EncryptKey = 0; var hex = JT809Serializer.Serialize(jT809Header).ToHexString(); @@ -35,25 +35,11 @@ namespace JT809.Protocol.Test Assert.Equal((uint)1024, jT809Header.MsgSN); Assert.Equal(JT809Enums.JT809BusinessType.从链路静态信息交换消息, jT809Header.BusinessType); Assert.Equal((uint)1200, jT809Header.MsgGNSSCENTERID); - Assert.Equal("010000", jT809Header.Version); + Assert.Equal(new JT809Header_Version().ToString(), jT809Header.Version.ToString()); Assert.Equal(JT809Header_Encrypt.None, jT809Header.EncryptFlag); Assert.Equal((uint)0, jT809Header.EncryptKey); } - [Fact] - public void Test4() - { - JT809Header jT809Header = new JT809Header(); - jT809Header.MsgLength = 24; - jT809Header.MsgSN = 1024; - jT809Header.BusinessType = JT809Enums.JT809BusinessType.从链路静态信息交换消息; - jT809Header.MsgGNSSCENTERID = 1200; - jT809Header.Version = "0F0A0B"; - jT809Header.EncryptFlag = JT809Header_Encrypt.None; - jT809Header.EncryptKey = 0; - var hex = JT809Serializer.Serialize(jT809Header).ToHexString(); - } - [Fact] public void Test3() { diff --git a/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001EncryptPackageTest.cs b/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001EncryptPackageTest.cs index 93faa64..62b2ecc 100644 --- a/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001EncryptPackageTest.cs +++ b/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001EncryptPackageTest.cs @@ -17,7 +17,8 @@ namespace JT809.Protocol.Test.JT809Packages { IA1 = 20000000, IC1 = 20000000, - M1 = 30000000 + M1 = 30000000, + Key = 256178, })); } @@ -29,7 +30,7 @@ namespace JT809.Protocol.Test.JT809Packages { EncryptFlag= JT809Header_Encrypt.Common, MsgSN= 133, - EncryptKey= 256178, + EncryptKey=5819, BusinessType= JT809Enums.JT809BusinessType.主链路登录请求消息, MsgGNSSCENTERID= 20180920, }; @@ -46,15 +47,16 @@ namespace JT809.Protocol.Test.JT809Packages [Fact] public void Test2() { - var bytes = "5B000000480000008510010133EFB8010000010003E8B2D37D9CC4900C77DC78F8676527D8AE12243CFB64CC2FBA619AEFAD33ACCB3256F67BFF19DF33097841098665703FE36E5D".ToStr2HexBytes(); + var bytes = "5B 00 00 00 48 00 00 00 85 10 01 01 33 EF B8 01 00 00 01 00 00 16 BB D3 7D 9C C4 90 0C 77 DC 78 F8 67 65 27 D8 AE 12 24 3C FB 64 CC 2F BA 61 9A EF AD 33 AC CB 32 56 F6 7B FF 19 DF 33 09 78 41 09 86 65 70 3F 2E B5 5D".ToHexBytes(); JT809Package jT809Package = JT809Serializer.Deserialize(bytes); Assert.Equal(JT809Header_Encrypt.Common, jT809Package.Header.EncryptFlag); - Assert.Equal((uint)256178, jT809Package.Header.EncryptKey); + Assert.Equal((uint)5819, jT809Package.Header.EncryptKey); Assert.Equal((uint)72, jT809Package.Header.MsgLength); Assert.Equal((uint)133, jT809Package.Header.MsgSN); + Assert.Equal((uint)5819, jT809Package.Header.EncryptKey); Assert.Equal((uint)20180920, jT809Package.Header.MsgGNSSCENTERID); Assert.Equal(JT809Enums.JT809BusinessType.主链路登录请求消息, jT809Package.Header.BusinessType); - Assert.Equal("010000", jT809Package.Header.Version); + Assert.Equal(new JT809Header_Version().ToString(), jT809Package.Header.Version.ToString()); JT809_0x1001 jT809_0X1001 = (JT809_0x1001)jT809Package.Bodies; Assert.Equal((uint)20180920, jT809_0X1001.UserId); Assert.Equal("20180920", jT809_0X1001.Password); diff --git a/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001PackageTest.cs b/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001PackageTest.cs index 61531fd..aa4a1e5 100644 --- a/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001PackageTest.cs +++ b/src/JT809.Protocol.Test/JT809Packages/JT809_0x1001PackageTest.cs @@ -54,7 +54,7 @@ namespace JT809.Protocol.Test.JT809Packages Assert.Equal((uint)9999, jT809Package.Header.EncryptKey); Assert.Equal((uint)20180920, jT809Package.Header.MsgGNSSCENTERID); Assert.Equal(JT809Enums.JT809BusinessType.主链路登录请求消息, jT809Package.Header.BusinessType); - Assert.Equal("010000", jT809Package.Header.Version); + Assert.Equal(new JT809Header_Version().ToString(), jT809Package.Header.Version.ToString()); JT809_0x1001 jT809_0X1001 = (JT809_0x1001)jT809Package.Bodies; Assert.Equal((uint)20180920, jT809_0X1001.UserId); Assert.Equal("20180920", jT809_0X1001.Password); diff --git a/src/JT809.Protocol.Test/JT809Packages/JT809_0x9400_0x9401PackageTest.cs b/src/JT809.Protocol.Test/JT809Packages/JT809_0x9400_0x9401PackageTest.cs index 60937d9..2affc45 100644 --- a/src/JT809.Protocol.Test/JT809Packages/JT809_0x9400_0x9401PackageTest.cs +++ b/src/JT809.Protocol.Test/JT809Packages/JT809_0x9400_0x9401PackageTest.cs @@ -22,7 +22,7 @@ namespace JT809.Protocol.Test.JT809Packages MsgSN = 1666, EncryptKey = 9999, EncryptFlag= JT809Header_Encrypt.None, - Version = "010000", + Version = new JT809Header_Version(1, 0, 0), BusinessType = JT809Enums.JT809BusinessType.从链路报警信息交互消息, MsgGNSSCENTERID = 20180920, }; @@ -64,7 +64,7 @@ namespace JT809.Protocol.Test.JT809Packages Assert.Equal(JT809Header_Encrypt.None, jT809Package.Header.EncryptFlag); Assert.Equal((uint)20180920, jT809Package.Header.MsgGNSSCENTERID); Assert.Equal(JT809Enums.JT809BusinessType.从链路报警信息交互消息, jT809Package.Header.BusinessType); - Assert.Equal("010000", jT809Package.Header.Version); + Assert.Equal(new JT809Header_Version().ToString(), jT809Package.Header.Version.ToString()); JT809_0x9400 jT809_0X400 = (JT809_0x9400)jT809Package.Bodies; Assert.Equal("粤A12345", jT809_0X400.VehicleNo); diff --git a/src/JT809.Protocol.sln b/src/JT809.Protocol.sln index 202e29e..34f4c48 100644 --- a/src/JT809.Protocol.sln +++ b/src/JT809.Protocol.sln @@ -7,11 +7,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT809.Protocol", "JT809.Pro EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT809.Protocol.Test", "JT809.Protocol.Test\JT809.Protocol.Test.csproj", "{59D2D876-8D81-4CCE-839A-B153912C0C27}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT809.Protocol.Benchmark", "JT809.Protocol.Benchmark\JT809.Protocol.Benchmark.csproj", "{47CE50B3-A0D6-4F5F-907B-01BD7B8AB87F}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT809.Protocol.Extensions.DependencyInjection", "JT809.Protocol.Extensions.DependencyInjection\JT809.Protocol.Extensions.DependencyInjection.csproj", "{974E9013-A19E-4643-8AB4-504D916AF4BD}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT809.Protocol.Extensions.DependencyInjection.Test", "JT809.Protocol.Extensions.DependencyInjection.Test\JT809.Protocol.Extensions.DependencyInjection.Test.csproj", "{F3E0933D-13A9-48AA-958A-7903221D5DFA}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT809.Protocol.Benchmark", "JT809.Protocol.Benchmark\JT809.Protocol.Benchmark.csproj", "{47CE50B3-A0D6-4F5F-907B-01BD7B8AB87F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -31,14 +27,6 @@ Global {47CE50B3-A0D6-4F5F-907B-01BD7B8AB87F}.Debug|Any CPU.Build.0 = Debug|Any CPU {47CE50B3-A0D6-4F5F-907B-01BD7B8AB87F}.Release|Any CPU.ActiveCfg = Release|Any CPU {47CE50B3-A0D6-4F5F-907B-01BD7B8AB87F}.Release|Any CPU.Build.0 = Release|Any CPU - {974E9013-A19E-4643-8AB4-504D916AF4BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {974E9013-A19E-4643-8AB4-504D916AF4BD}.Debug|Any CPU.Build.0 = Debug|Any CPU - {974E9013-A19E-4643-8AB4-504D916AF4BD}.Release|Any CPU.ActiveCfg = Release|Any CPU - {974E9013-A19E-4643-8AB4-504D916AF4BD}.Release|Any CPU.Build.0 = Release|Any CPU - {F3E0933D-13A9-48AA-958A-7903221D5DFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F3E0933D-13A9-48AA-958A-7903221D5DFA}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F3E0933D-13A9-48AA-958A-7903221D5DFA}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F3E0933D-13A9-48AA-958A-7903221D5DFA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/JT809.Protocol/IJT809Encrypt.cs b/src/JT809.Protocol/IJT809Encrypt.cs index 8fc4396..f1d497a 100644 --- a/src/JT809.Protocol/IJT809Encrypt.cs +++ b/src/JT809.Protocol/IJT809Encrypt.cs @@ -2,7 +2,7 @@ { public interface IJT809Encrypt { - byte[] Encrypt(byte[] buffer, uint privateKey); - byte[] Decrypt(byte[] buffer, uint privateKey); + byte[] Encrypt(byte[] buffer); + byte[] Decrypt(byte[] buffer); } } diff --git a/src/JT809.Protocol/JT809Configs/JT809EncryptOptions.cs b/src/JT809.Protocol/JT809Configs/JT809EncryptOptions.cs index e00bc99..bc0f216 100644 --- a/src/JT809.Protocol/JT809Configs/JT809EncryptOptions.cs +++ b/src/JT809.Protocol/JT809Configs/JT809EncryptOptions.cs @@ -5,5 +5,6 @@ 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/JT809Configs/JT809HeaderOptions.cs b/src/JT809.Protocol/JT809Configs/JT809HeaderOptions.cs index 169cbaf..ec8be43 100644 --- a/src/JT809.Protocol/JT809Configs/JT809HeaderOptions.cs +++ b/src/JT809.Protocol/JT809Configs/JT809HeaderOptions.cs @@ -15,7 +15,7 @@ namespace JT809.Protocol.JT809Configs /// 编号;长度为 3 个字节来表示,0x01 0x02 0x0F 标识 /// 的版本号是 v1.2.15,以此类推。 /// - public string Version { get; set; } = "010000"; + public JT809Header_Version Version { get; set; } = new JT809Header_Version(); /// /// 报文加密标识位 b: 0 表示报文不加密,1 表示报文加密。 /// diff --git a/src/JT809.Protocol/JT809Encrypt/JT809EncryptImpl.cs b/src/JT809.Protocol/JT809Encrypt/JT809EncryptImpl.cs index 79e2e49..4c1e8e7 100644 --- a/src/JT809.Protocol/JT809Encrypt/JT809EncryptImpl.cs +++ b/src/JT809.Protocol/JT809Encrypt/JT809EncryptImpl.cs @@ -14,17 +14,17 @@ namespace JT809.Protocol.JT809Encrypt this.jT809EncryptOptions = jT809EncryptOptions; } - public byte[] Decrypt(byte[] buffer, uint privateKey) + public byte[] Decrypt(byte[] buffer) { - return Encrypt(buffer, privateKey); + return Encrypt(buffer); } - public byte[] Encrypt(byte[] buffer, uint privateKey) + public byte[] Encrypt(byte[] buffer) { byte[] data = new byte[buffer.Length]; - if (0 == privateKey) + if (0 == jT809EncryptOptions.Key) { - privateKey = 1; + jT809EncryptOptions.Key = 1; } uint mkey = jT809EncryptOptions.M1; if (0 == mkey) @@ -33,8 +33,8 @@ namespace JT809.Protocol.JT809Encrypt } for (int idx = 0; idx < buffer.Length; idx++) { - privateKey = jT809EncryptOptions.IA1 * (privateKey % mkey) + jT809EncryptOptions.IC1; - buffer[idx] ^= (byte)((privateKey >> 20) & 0xFF); + jT809EncryptOptions.Key = jT809EncryptOptions.IA1 * (jT809EncryptOptions.Key % mkey) + jT809EncryptOptions.IC1; + buffer[idx] ^= (byte)((jT809EncryptOptions.Key >> 20) & 0xFF); data[idx] = buffer[idx]; } return data; diff --git a/src/JT809.Protocol/JT809Extensions/JT809BCDExtensions.cs b/src/JT809.Protocol/JT809Extensions/JT809BCDExtensions.cs index dc2a6e6..b616839 100644 --- a/src/JT809.Protocol/JT809Extensions/JT809BCDExtensions.cs +++ b/src/JT809.Protocol/JT809Extensions/JT809BCDExtensions.cs @@ -19,24 +19,14 @@ namespace JT809.Protocol.JT809Extensions return bcdSb.ToString(); } - public static int WriteBCDLittle(IMemoryOwner memoryOwner, int offset, string data, int len) + public static int WriteBCDLittle(IMemoryOwner memoryOwner, int offset, string data, int digit, int len) { - string bcdText = data == null ? "" : data; - byte[] bytes = new byte[len]; - int startIndex = 0; - int noOfZero = len * 2 - data.Length; - if (noOfZero > 0) + ReadOnlySpan bcd = data.PadLeft(len, '0').AsSpan(); + for (int i = 0; i < digit; i++) { - bcdText = bcdText.Insert(startIndex, new string('0', noOfZero)); + memoryOwner.Memory.Span[offset + i] = Convert.ToByte(bcd.Slice(i * 2, 2).ToString(), 16); } - int byteIndex = 0; - while (startIndex < bcdText.Length && byteIndex < len) - { - memoryOwner.Memory.Span[offset + byteIndex] = Convert.ToByte(bcdText.Substring(startIndex, 2), 16); - startIndex += 2; - byteIndex++; - } - return len; + return digit; } } } diff --git a/src/JT809.Protocol/JT809Formatters/JT809HeaderFormatter.cs b/src/JT809.Protocol/JT809Formatters/JT809HeaderFormatter.cs index b9fa8e6..0926234 100644 --- a/src/JT809.Protocol/JT809Formatters/JT809HeaderFormatter.cs +++ b/src/JT809.Protocol/JT809Formatters/JT809HeaderFormatter.cs @@ -17,7 +17,7 @@ namespace JT809.Protocol.JT809Formatters jT809Header.MsgSN= JT809BinaryExtensions.ReadUInt32Little(bytes, ref offset); jT809Header.BusinessType = (JT809BusinessType)JT809BinaryExtensions.ReadUInt16Little(bytes, ref offset); jT809Header.MsgGNSSCENTERID = JT809BinaryExtensions.ReadUInt32Little(bytes, ref offset); - jT809Header.Version = JT809BinaryExtensions.ReadBCDLittle(bytes, ref offset, 3); + jT809Header.Version = new JT809Header_Version(JT809BinaryExtensions.ReadBytesLittle(bytes, ref offset, JT809Header_Version.FixedByteLength)); jT809Header.EncryptFlag= (JT809Header_Encrypt)JT809BinaryExtensions.ReadByteLittle(bytes, ref offset); jT809Header.EncryptKey= JT809BinaryExtensions.ReadUInt32Little(bytes, ref offset); readSize = offset; @@ -30,7 +30,7 @@ namespace JT809.Protocol.JT809Formatters offset += JT809BinaryExtensions.WriteUInt32Little(memoryOwner, offset, value.MsgSN); offset += JT809BinaryExtensions.WriteUInt16Little(memoryOwner, offset, (ushort)value.BusinessType); offset += JT809BinaryExtensions.WriteUInt32Little(memoryOwner, offset, value.MsgGNSSCENTERID); - offset += JT809BinaryExtensions.WriteBCDLittle(memoryOwner, offset, value.Version,3); + offset += JT809BinaryExtensions.WriteBytesLittle(memoryOwner, offset, value.Version.Buffer); offset += JT809BinaryExtensions.WriteByteLittle(memoryOwner, offset, (byte)value.EncryptFlag); offset += JT809BinaryExtensions.WriteUInt32Little(memoryOwner, offset, value.EncryptKey); return offset; diff --git a/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs b/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs index 83b2dc1..b89e0e3 100644 --- a/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs +++ b/src/JT809.Protocol/JT809Formatters/JT809PackageFormatter.cs @@ -65,7 +65,7 @@ namespace JT809.Protocol.JT809Formatters jT809Package.Bodies = JT809FormatterResolverExtensions.JT809DynamicDeserialize(JT809FormatterExtensions.GetFormatter(jT809BodiesTypeAttribute.JT809BodiesType), buffer.Slice(offset, checkIndex - offset), out readSize); break; case JT809Header_Encrypt.Common: - byte[] bodiesData = JT809GlobalConfig.Instance.Encrypt.Decrypt(buffer.Slice(offset, checkIndex - offset).ToArray(), jT809Package.Header.EncryptKey); + byte[] bodiesData = JT809GlobalConfig.Instance.Encrypt.Decrypt(buffer.Slice(offset, checkIndex - offset).ToArray()); jT809Package.Bodies = JT809FormatterResolverExtensions.JT809DynamicDeserialize(JT809FormatterExtensions.GetFormatter(jT809BodiesTypeAttribute.JT809BodiesType), bodiesData, out readSize); break; } @@ -104,7 +104,7 @@ namespace JT809.Protocol.JT809Formatters case JT809Header_Encrypt.None: break; case JT809Header_Encrypt.Common: - messageBodyData = JT809GlobalConfig.Instance.Encrypt.Encrypt(messageBodyData, value.Header.EncryptKey); + messageBodyData = JT809GlobalConfig.Instance.Encrypt.Encrypt(messageBodyData); break; } } diff --git a/src/JT809.Protocol/JT809Formatters/JT809SubMessageBodyFormatters/JT809_0x9500_0x9505Formatter.cs b/src/JT809.Protocol/JT809Formatters/JT809SubMessageBodyFormatters/JT809_0x9500_0x9505Formatter.cs index 7ffab0c..9604eb2 100644 --- a/src/JT809.Protocol/JT809Formatters/JT809SubMessageBodyFormatters/JT809_0x9500_0x9505Formatter.cs +++ b/src/JT809.Protocol/JT809Formatters/JT809SubMessageBodyFormatters/JT809_0x9500_0x9505Formatter.cs @@ -27,7 +27,7 @@ namespace JT809.Protocol.JT809Formatters.JT809SubMessageBodyFormatters public int Serialize(IMemoryOwner memoryOwner, int offset, JT809_0x9500_0x9505 value) { - offset += JT809BinaryExtensions.WriteBCDLittle(memoryOwner, offset, value.AuthenticationCode,20); + offset += JT809BinaryExtensions.WriteBCDLittle(memoryOwner, offset, value.AuthenticationCode,10,20); offset += JT809BinaryExtensions.WriteStringLittle(memoryOwner, offset, value.AccessPointName,20); offset += JT809BinaryExtensions.WriteStringLittle(memoryOwner, offset, value.UserName, 49); offset += JT809BinaryExtensions.WriteStringLittle(memoryOwner, offset, value.Password, 22); diff --git a/src/JT809.Protocol/JT809GlobalConfig.cs b/src/JT809.Protocol/JT809GlobalConfig.cs index 1764de6..d772d0d 100644 --- a/src/JT809.Protocol/JT809GlobalConfig.cs +++ b/src/JT809.Protocol/JT809GlobalConfig.cs @@ -1,5 +1,4 @@ using JT809.Protocol.JT809Configs; -using JT809.Protocol.JT809Encrypt; using JT809.Protocol.JT809Internal; using System; using System.Collections.Generic; @@ -37,18 +36,6 @@ namespace JT809.Protocol /// public bool SkipCRCCode { get; private set; } /// - /// 设置加密算法选项值 - /// 不同的上下级平台之间,加密的算法是一致的,但是针对 M1, IA1, IC1 的不同。 - /// 数据先经过加密而后解密。 - /// - /// - /// - public JT809GlobalConfig SetEncryptOptions(JT809EncryptOptions jT809EncryptOptions) - { - instance.Value.Encrypt = new JT809EncryptImpl(jT809EncryptOptions); - return instance.Value; - } - /// /// 设置加密算法实现 /// /// diff --git a/src/JT809.Protocol/JT809Header.cs b/src/JT809.Protocol/JT809Header.cs index 64e6aa2..7defbc8 100644 --- a/src/JT809.Protocol/JT809Header.cs +++ b/src/JT809.Protocol/JT809Header.cs @@ -38,9 +38,8 @@ namespace JT809.Protocol /// 协议版本号标识,上下级平台之间采用的标准协议版 /// 编号;长度为 3 个字节来表示,0x01 0x02 0x0F 标识 /// 的版本号是 v1.2.15,以此类推。 - /// BCD编码 /// - public string Version { get; set; } = "010000"; + public JT809Header_Version Version { get; set; } = new JT809Header_Version(); /// /// 报文加密标识位 b: 0 表示报文不加密,1 表示报文加密。 /// diff --git a/src/JT809.Protocol/JT809Header_Version.cs b/src/JT809.Protocol/JT809Header_Version.cs index ef75d3e..f7958f5 100644 --- a/src/JT809.Protocol/JT809Header_Version.cs +++ b/src/JT809.Protocol/JT809Header_Version.cs @@ -4,13 +4,9 @@ using System.Text; namespace JT809.Protocol { - /// - /// 协议版本号标识 - /// BCD编码 - /// public class JT809Header_Version { - public byte[] Buffer { get;} = new byte[3]; + public byte[] Buffer { get; } = new byte[3]; private const int MajorIndex = 0; private const int MinorIndex = 1; private const int BuildIndex = 2; @@ -19,19 +15,19 @@ namespace JT809.Protocol public byte Major { get { return Buffer[MajorIndex]; } - set { Buffer[MajorIndex] = value; } + private set { Buffer[MajorIndex] = value; } } public byte Minor { get { return Buffer[MinorIndex]; } - set { Buffer[MinorIndex] = value; } + private set { Buffer[MinorIndex] = value; } } public byte Build { get { return Buffer[BuildIndex]; } - set { Buffer[BuildIndex] = value; } + private set { Buffer[BuildIndex] = value; } } ///