From ba6bcd32b3b0fb8b58d32b785dd538cc4ffa6f35 Mon Sep 17 00:00:00 2001
From: SmallChi <564952747@qq.com>
Date: Fri, 21 Sep 2018 17:16:54 +0800
Subject: [PATCH] =?UTF-8?q?1.=E5=A2=9E=E5=8A=A0=E5=8A=A0=E5=AF=86=E5=AE=9E?=
=?UTF-8?q?=E7=8E=B0=202.=E5=A2=9E=E5=8A=A0=E5=8A=A0=E5=AF=86=E6=B5=8B?=
=?UTF-8?q?=E8=AF=95=203.=E5=8E=BB=E6=8E=89=E6=97=A0=E7=94=A8=E9=A1=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../JT809Encrypt/JT809EncryptTest.cs | 45 ++++++++++
.../Encrypt/JT809EncryptImpl.cs | 41 ---------
src/JT809.Protocol/Escape/JT809EscapeImpl.cs | 85 -------------------
src/JT809.Protocol/IEncrypt.cs | 8 --
src/JT809.Protocol/IEscape.cs | 8 --
src/JT809.Protocol/IJT809Encrypt.cs | 8 ++
src/JT809.Protocol/JT809.Protocol.csproj | 8 +-
.../{Configs => JT809Configs}/JT809Config.cs | 0
.../JT809EncryptOptions.cs} | 20 ++---
.../JT809Encrypt/JT809EncryptImpl.cs | 43 ++++++++++
10 files changed, 110 insertions(+), 156 deletions(-)
create mode 100644 src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs
delete mode 100644 src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs
delete mode 100644 src/JT809.Protocol/Escape/JT809EscapeImpl.cs
delete mode 100644 src/JT809.Protocol/IEncrypt.cs
delete mode 100644 src/JT809.Protocol/IEscape.cs
create mode 100644 src/JT809.Protocol/IJT809Encrypt.cs
rename src/JT809.Protocol/{Configs => JT809Configs}/JT809Config.cs (100%)
rename src/JT809.Protocol/{Configs/JT809EncryptConfig.cs => JT809Configs/JT809EncryptOptions.cs} (67%)
create mode 100644 src/JT809.Protocol/JT809Encrypt/JT809EncryptImpl.cs
diff --git a/src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs b/src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs
new file mode 100644
index 0000000..767be5d
--- /dev/null
+++ b/src/JT809.Protocol.Test/JT809Encrypt/JT809EncryptTest.cs
@@ -0,0 +1,45 @@
+using JT809.Protocol.JT809Configs;
+using JT809.Protocol.JT809Encrypt;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Xunit;
+using JT809.Protocol.JT809Extensions;
+
+namespace JT809.Protocol.Test.JT809Encrypt
+{
+ public class JT809EncryptTest
+ {
+ private JT809EncryptOptions options = new JT809EncryptOptions
+ {
+ IA1 = 20000000,
+ IC1 = 20000000,
+ M1 = 30000000,
+ Key = 256178,
+ };
+
+ [Fact]
+ public void Test1()
+ {
+ byte[] bytes = new byte[]
+ {
+ 01,02,03,04,05,06,07
+ };
+ IJT809Encrypt jT809Encrypt = new JT809EncryptImpl(options);
+ var data = jT809Encrypt.Encrypt(bytes).ToHexString();
+ //"D3 4C 70 78 A7 3A 41"
+ }
+
+ [Fact]
+ public void Test2()
+ {
+ byte[] bytes = "D3 4C 70 78 A7 3A 41".ToHexBytes();
+ IJT809Encrypt jT809Encrypt = new JT809EncryptImpl(options);
+ var data = jT809Encrypt.Decrypt(bytes);
+ Assert.Equal(new byte[]
+ {
+ 01,02,03,04,05,06,07
+ }, data);
+ }
+ }
+}
diff --git a/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs b/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs
deleted file mode 100644
index f3d4d30..0000000
--- a/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs
+++ /dev/null
@@ -1,41 +0,0 @@
-using JT809.Protocol.Configs;
-
-namespace JT809.Protocol.Encrypt
-{
- ///
- /// JT809 异或加密解密为同一算法
- ///
- public class JT809EncryptImpl : IEncrypt
- {
- private JT809EncryptConfig Config;
-
-
- public JT809EncryptImpl(JT809EncryptConfig config)
- {
- Config = config;
- }
-
- public void Decrypt(byte[] buffer)
- {
- Encrypt(buffer);
- }
-
- public void Encrypt(byte[] buffer)
- {
- if (0 == Config.Key)
- {
- Config.Key = 1;
- }
- uint mkey = Config.M1;
- if (0 == mkey)
- {
- mkey = 1;
- }
- for (int idx = 0; idx < buffer.Length; idx++)
- {
- Config.Key = Config.IA1 * (Config.Key % mkey) + Config.IC1;
- buffer[idx] ^= (byte)((Config.Key >> 20) & 0xFF);
- }
- }
- }
-}
diff --git a/src/JT809.Protocol/Escape/JT809EscapeImpl.cs b/src/JT809.Protocol/Escape/JT809EscapeImpl.cs
deleted file mode 100644
index d31c671..0000000
--- a/src/JT809.Protocol/Escape/JT809EscapeImpl.cs
+++ /dev/null
@@ -1,85 +0,0 @@
-using System.Collections.Generic;
-
-namespace JT809.Protocol.Escape
-{
- public class JT809EscapeImpl : IEscape
- {
- public byte[] Escape(byte[] bodyBuffer, IEncrypt encrypt)
- {
- List dataList = new List();
- dataList.Add(bodyBuffer[0]);
- for (int i = 1; i < bodyBuffer.Length - 1; i++)
- {
- var item = bodyBuffer[i];
- switch (item)
- {
- case 0x5b:
- dataList.Add(0x5a);
- dataList.Add(0x01);
- break;
- case 0x5a:
- dataList.Add(0x5a);
- dataList.Add(0x02);
- break;
- case 0x5d:
- dataList.Add(0x5e);
- dataList.Add(0x01);
- break;
- case 0x5e:
- dataList.Add(0x5e);
- dataList.Add(0x02);
- break;
- default:
- dataList.Add(item);
- break;
- }
- }
- dataList.Add(bodyBuffer[bodyBuffer.Length - 1]);
- var tempBuffe = dataList.ToArray();
-
- return tempBuffe;
- }
-
- public byte[] UnEscape(byte[] bodyBuffer, IEncrypt encrypt)
- {
- List dataList = new List();
- dataList.Add(bodyBuffer[0]);
- for (int i = 1; i < bodyBuffer.Length - 1; i++)
- {
- byte first = bodyBuffer[i];
- byte second = bodyBuffer[i + 1];
- if (first == 0x5a && second == 0x01)
- {
- dataList.Add(0x5b);
- i++;
- }
- else if (first == 0x5a && second == 0x02)
- {
- dataList.Add(0x5a);
- i++;
- }
- else if (first == 0x5e && second == 0x01)
- {
- dataList.Add(0x5d);
- i++;
- }
- else if (first == 0x5e && second == 0x02)
- {
- dataList.Add(0x5e);
- i++;
- }
- else
- {
- dataList.Add(first);
- }
- }
- dataList.Add(bodyBuffer[bodyBuffer.Length - 1]);
- var tempBuffe = dataList.ToArray();
- if (encrypt != null)
- {
- encrypt.Decrypt(tempBuffe);
- }
- return tempBuffe;
- }
- }
-}
diff --git a/src/JT809.Protocol/IEncrypt.cs b/src/JT809.Protocol/IEncrypt.cs
deleted file mode 100644
index 008b775..0000000
--- a/src/JT809.Protocol/IEncrypt.cs
+++ /dev/null
@@ -1,8 +0,0 @@
-namespace JT809.Protocol
-{
- public interface IEncrypt
- {
- void Encrypt(byte[] buffer);
- void Decrypt(byte[] buffer);
- }
-}
diff --git a/src/JT809.Protocol/IEscape.cs b/src/JT809.Protocol/IEscape.cs
deleted file mode 100644
index 00c2dc5..0000000
--- a/src/JT809.Protocol/IEscape.cs
+++ /dev/null
@@ -1,8 +0,0 @@
-namespace JT809.Protocol
-{
- public interface IEscape
- {
- byte[] Escape(byte[] dataContent, IEncrypt encrypt);
- byte[] UnEscape(byte[] dataContent, IEncrypt encrypt);
- }
-}
diff --git a/src/JT809.Protocol/IJT809Encrypt.cs b/src/JT809.Protocol/IJT809Encrypt.cs
new file mode 100644
index 0000000..f1d497a
--- /dev/null
+++ b/src/JT809.Protocol/IJT809Encrypt.cs
@@ -0,0 +1,8 @@
+namespace JT809.Protocol
+{
+ public interface IJT809Encrypt
+ {
+ byte[] Encrypt(byte[] buffer);
+ byte[] Decrypt(byte[] buffer);
+ }
+}
diff --git a/src/JT809.Protocol/JT809.Protocol.csproj b/src/JT809.Protocol/JT809.Protocol.csproj
index 89ab3f6..1754545 100644
--- a/src/JT809.Protocol/JT809.Protocol.csproj
+++ b/src/JT809.Protocol/JT809.Protocol.csproj
@@ -10,17 +10,17 @@
-
+
-
+
-
+
@@ -29,11 +29,11 @@
-
+
diff --git a/src/JT809.Protocol/Configs/JT809Config.cs b/src/JT809.Protocol/JT809Configs/JT809Config.cs
similarity index 100%
rename from src/JT809.Protocol/Configs/JT809Config.cs
rename to src/JT809.Protocol/JT809Configs/JT809Config.cs
diff --git a/src/JT809.Protocol/Configs/JT809EncryptConfig.cs b/src/JT809.Protocol/JT809Configs/JT809EncryptOptions.cs
similarity index 67%
rename from src/JT809.Protocol/Configs/JT809EncryptConfig.cs
rename to src/JT809.Protocol/JT809Configs/JT809EncryptOptions.cs
index b0a4fed..bc0f216 100644
--- a/src/JT809.Protocol/Configs/JT809EncryptConfig.cs
+++ b/src/JT809.Protocol/JT809Configs/JT809EncryptOptions.cs
@@ -1,10 +1,10 @@
-namespace JT809.Protocol.Configs
-{
- public class JT809EncryptConfig
- {
- public uint M1 { get; set; }
- public uint IA1 { get; set; }
- public uint IC1 { get; set; }
- public uint Key { get; set; }
- }
-}
+namespace JT809.Protocol.JT809Configs
+{
+ public class JT809EncryptOptions
+ {
+ 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/JT809Encrypt/JT809EncryptImpl.cs b/src/JT809.Protocol/JT809Encrypt/JT809EncryptImpl.cs
new file mode 100644
index 0000000..4c1e8e7
--- /dev/null
+++ b/src/JT809.Protocol/JT809Encrypt/JT809EncryptImpl.cs
@@ -0,0 +1,43 @@
+using JT809.Protocol.JT809Configs;
+
+namespace JT809.Protocol.JT809Encrypt
+{
+ ///
+ /// JT809 异或加密解密为同一算法
+ ///
+ public class JT809EncryptImpl : IJT809Encrypt
+ {
+ private JT809EncryptOptions jT809EncryptOptions;
+
+ public JT809EncryptImpl(JT809EncryptOptions jT809EncryptOptions)
+ {
+ this.jT809EncryptOptions = jT809EncryptOptions;
+ }
+
+ public byte[] Decrypt(byte[] buffer)
+ {
+ return Encrypt(buffer);
+ }
+
+ public byte[] Encrypt(byte[] buffer)
+ {
+ byte[] data = new byte[buffer.Length];
+ if (0 == jT809EncryptOptions.Key)
+ {
+ jT809EncryptOptions.Key = 1;
+ }
+ uint mkey = jT809EncryptOptions.M1;
+ if (0 == mkey)
+ {
+ mkey = 1;
+ }
+ for (int idx = 0; idx < buffer.Length; idx++)
+ {
+ jT809EncryptOptions.Key = jT809EncryptOptions.IA1 * (jT809EncryptOptions.Key % mkey) + jT809EncryptOptions.IC1;
+ buffer[idx] ^= (byte)((jT809EncryptOptions.Key >> 20) & 0xFF);
+ data[idx] = buffer[idx];
+ }
+ return data;
+ }
+ }
+}