diff --git a/src/JT809.Protocol.Test/BaseTest.cs b/src/JT809.Protocol.Test/BaseTest.cs new file mode 100644 index 0000000..3e4f5c6 --- /dev/null +++ b/src/JT809.Protocol.Test/BaseTest.cs @@ -0,0 +1,14 @@ +using JT809.Protocol.Configs; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JT809.Protocol.Test +{ + public class BaseTest + { + public JT809EncryptConfig JT809EncryptConfig = new JT809EncryptConfig(); + } +} diff --git a/src/JT809.Protocol.Test/Encrypt/JT809EncryptImplTest.cs b/src/JT809.Protocol.Test/Encrypt/JT809EncryptImplTest.cs new file mode 100644 index 0000000..e356990 --- /dev/null +++ b/src/JT809.Protocol.Test/Encrypt/JT809EncryptImplTest.cs @@ -0,0 +1,14 @@ +using System; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace JT809.Protocol.Test.Encrypt +{ + [TestClass] + public class JT809EncryptImplTest: BaseTest + { + [TestMethod] + public void TestMethod1() + { + } + } +} diff --git a/src/JT809.Protocol.Test/Escape/JT809EscapeImplTest.cs b/src/JT809.Protocol.Test/Escape/JT809EscapeImplTest.cs new file mode 100644 index 0000000..995cb1d --- /dev/null +++ b/src/JT809.Protocol.Test/Escape/JT809EscapeImplTest.cs @@ -0,0 +1,41 @@ +using System; +using System.Text; +using JT809.Protocol.Encrypt; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace JT809.Protocol.Test.Escape +{ + [TestClass] + public class JT809EscapeImplTest:BaseTest + { + public IEncrypt encrypt; + + public IEncrypt encrypt2; + + public JT809EscapeImplTest() + { + encrypt = new JT809EncryptImpl(0x01,base.JT809EncryptConfig); + encrypt2 = new JT809EncryptImpl(0x01, base.JT809EncryptConfig); + } + + + [TestMethod] + public void Encrypt() + { + var pre = System.Text.Encoding.UTF8.GetBytes("smallchi"); + encrypt.Encrypt(pre); + } + + [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); + } + } +} diff --git a/src/JT809.Protocol.Test/JT809.Protocol.Test.csproj b/src/JT809.Protocol.Test/JT809.Protocol.Test.csproj new file mode 100644 index 0000000..c6ca57e --- /dev/null +++ b/src/JT809.Protocol.Test/JT809.Protocol.Test.csproj @@ -0,0 +1,75 @@ + + + + + Debug + AnyCPU + {D1C5968B-741D-4D75-BB33-BD006C897D05} + Library + Properties + JT809.Protocol.Test + JT809.Protocol.Test + v4.6.1 + 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + 15.0 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages + False + UnitTest + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll + + + ..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll + + + + + + + + + + + + + + + + {4d33a5c9-f583-4230-9791-ab0394efad57} + JT809.Protocol + + + + + + + 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 + + + + + + \ No newline at end of file diff --git a/src/JT809.Protocol.Test/Properties/AssemblyInfo.cs b/src/JT809.Protocol.Test/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..d5dfc22 --- /dev/null +++ b/src/JT809.Protocol.Test/Properties/AssemblyInfo.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +[assembly: AssemblyTitle("JT809.Protocol.Test")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("JT809.Protocol.Test")] +[assembly: AssemblyCopyright("Copyright © 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +[assembly: ComVisible(false)] + +[assembly: Guid("d1c5968b-741d-4d75-bb33-bd006c897d05")] + +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/JT809.Protocol.Test/packages.config b/src/JT809.Protocol.Test/packages.config new file mode 100644 index 0000000..6384d0b --- /dev/null +++ b/src/JT809.Protocol.Test/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/JT809.Protocol.sln b/src/JT809.Protocol.sln index 01c0e59..490d49e 100644 --- a/src/JT809.Protocol.sln +++ b/src/JT809.Protocol.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.27428.2005 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT809.Protocol", "JT809.Protocol\JT809.Protocol.csproj", "{4D33A5C9-F583-4230-9791-AB0394EFAD57}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT809.Protocol", "JT809.Protocol\JT809.Protocol.csproj", "{4D33A5C9-F583-4230-9791-AB0394EFAD57}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT809.Protocol.Test", "JT809.Protocol.Test\JT809.Protocol.Test.csproj", "{D1C5968B-741D-4D75-BB33-BD006C897D05}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +17,10 @@ Global {4D33A5C9-F583-4230-9791-AB0394EFAD57}.Debug|Any CPU.Build.0 = Debug|Any CPU {4D33A5C9-F583-4230-9791-AB0394EFAD57}.Release|Any CPU.ActiveCfg = Release|Any CPU {4D33A5C9-F583-4230-9791-AB0394EFAD57}.Release|Any CPU.Build.0 = Release|Any CPU + {D1C5968B-741D-4D75-BB33-BD006C897D05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D1C5968B-741D-4D75-BB33-BD006C897D05}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D1C5968B-741D-4D75-BB33-BD006C897D05}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D1C5968B-741D-4D75-BB33-BD006C897D05}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/JT809.Protocol/Configs/JT809EncryptConfig.cs b/src/JT809.Protocol/Configs/JT809EncryptConfig.cs new file mode 100644 index 0000000..eaf20ce --- /dev/null +++ b/src/JT809.Protocol/Configs/JT809EncryptConfig.cs @@ -0,0 +1,9 @@ +namespace JT809.Protocol.Configs +{ + public class JT809EncryptConfig + { + public uint M1 { get; set; } = 10000000; + public uint IA1 { get; set; } = 20000000; + public uint IC1 { get; set; } = 30000000; + } +} diff --git a/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs b/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs new file mode 100644 index 0000000..0f4a900 --- /dev/null +++ b/src/JT809.Protocol/Encrypt/JT809EncryptImpl.cs @@ -0,0 +1,43 @@ +using JT809.Protocol.Configs; + +namespace JT809.Protocol.Encrypt +{ + /// + /// JT809 异或加密解密为同一算法 + /// + public class JT809EncryptImpl : IEncrypt + { + private JT809EncryptConfig Config; + + private uint Key { get; set; } + + public JT809EncryptImpl(uint key,JT809EncryptConfig config) + { + Config = config; + Key = key; + } + + public void Decrypt(byte[] buffer) + { + Encrypt(buffer); + } + + public void Encrypt(byte[] buffer) + { + if (0 == Key) + { + Key = 1; + } + uint mkey = Config.M1; + if (0 == mkey) + { + mkey = 1; + } + for (int idx = 0; idx < buffer.Length; idx++) + { + Key = Config.IA1 * (Key % mkey) + Config.IC1; + buffer[idx] ^= (byte)((Key >> 20) & 0xFF); + } + } + } +} diff --git a/src/JT809.Protocol/Escape/JT809EscapeImpl.cs b/src/JT809.Protocol/Escape/JT809EscapeImpl.cs new file mode 100644 index 0000000..3110e6d --- /dev/null +++ b/src/JT809.Protocol/Escape/JT809EscapeImpl.cs @@ -0,0 +1,88 @@ +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(); + if (encrypt != null) + { + encrypt.Encrypt(tempBuffe); + } + 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 new file mode 100644 index 0000000..008b775 --- /dev/null +++ b/src/JT809.Protocol/IEncrypt.cs @@ -0,0 +1,8 @@ +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 new file mode 100644 index 0000000..00c2dc5 --- /dev/null +++ b/src/JT809.Protocol/IEscape.cs @@ -0,0 +1,8 @@ +namespace JT809.Protocol +{ + public interface IEscape + { + byte[] Escape(byte[] dataContent, IEncrypt encrypt); + byte[] UnEscape(byte[] dataContent, IEncrypt encrypt); + } +} diff --git a/src/JT809.Protocol/JT809.Protocol.csproj b/src/JT809.Protocol/JT809.Protocol.csproj index 72764a6..d009509 100644 --- a/src/JT809.Protocol/JT809.Protocol.csproj +++ b/src/JT809.Protocol/JT809.Protocol.csproj @@ -4,4 +4,8 @@ netstandard2.0 + + + +