@@ -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(); | |||
} | |||
} |
@@ -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() | |||
{ | |||
} | |||
} | |||
} |
@@ -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); | |||
} | |||
} | |||
} |
@@ -0,0 +1,75 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | |||
<Import Project="..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props')" /> | |||
<PropertyGroup> | |||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | |||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | |||
<ProjectGuid>{D1C5968B-741D-4D75-BB33-BD006C897D05}</ProjectGuid> | |||
<OutputType>Library</OutputType> | |||
<AppDesignerFolder>Properties</AppDesignerFolder> | |||
<RootNamespace>JT809.Protocol.Test</RootNamespace> | |||
<AssemblyName>JT809.Protocol.Test</AssemblyName> | |||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> | |||
<FileAlignment>512</FileAlignment> | |||
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> | |||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion> | |||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | |||
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath> | |||
<IsCodedUITest>False</IsCodedUITest> | |||
<TestProjectType>UnitTest</TestProjectType> | |||
<NuGetPackageImportStamp> | |||
</NuGetPackageImportStamp> | |||
</PropertyGroup> | |||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | |||
<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' "> | |||
<DebugType>pdbonly</DebugType> | |||
<Optimize>true</Optimize> | |||
<OutputPath>bin\Release\</OutputPath> | |||
<DefineConstants>TRACE</DefineConstants> | |||
<ErrorReport>prompt</ErrorReport> | |||
<WarningLevel>4</WarningLevel> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> | |||
<HintPath>..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath> | |||
</Reference> | |||
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> | |||
<HintPath>..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath> | |||
</Reference> | |||
<Reference Include="System" /> | |||
<Reference Include="System.Core" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<Compile Include="BaseTest.cs" /> | |||
<Compile Include="Encrypt\JT809EncryptImplTest.cs" /> | |||
<Compile Include="Escape\JT809EscapeImplTest.cs" /> | |||
<Compile Include="Properties\AssemblyInfo.cs" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<None Include="packages.config" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ProjectReference Include="..\JT809.Protocol\JT809.Protocol.csproj"> | |||
<Project>{4d33a5c9-f583-4230-9791-ab0394efad57}</Project> | |||
<Name>JT809.Protocol</Name> | |||
</ProjectReference> | |||
</ItemGroup> | |||
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" /> | |||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> | |||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> | |||
<PropertyGroup> | |||
<ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText> | |||
</PropertyGroup> | |||
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.props'))" /> | |||
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets'))" /> | |||
</Target> | |||
<Import Project="..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.0\build\net45\MSTest.TestAdapter.targets')" /> | |||
</Project> |
@@ -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")] |
@@ -0,0 +1,5 @@ | |||
<?xml version="1.0" encoding="utf-8"?> | |||
<packages> | |||
<package id="MSTest.TestAdapter" version="1.2.0" targetFramework="net461" /> | |||
<package id="MSTest.TestFramework" version="1.2.0" targetFramework="net461" /> | |||
</packages> |
@@ -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 | |||
@@ -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; | |||
} | |||
} |
@@ -0,0 +1,43 @@ | |||
using JT809.Protocol.Configs; | |||
namespace JT809.Protocol.Encrypt | |||
{ | |||
/// <summary> | |||
/// JT809 异或加密解密为同一算法 | |||
/// </summary> | |||
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); | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,88 @@ | |||
using System.Collections.Generic; | |||
namespace JT809.Protocol.Escape | |||
{ | |||
public class JT809EscapeImpl : IEscape | |||
{ | |||
public byte[] Escape(byte[] bodyBuffer, IEncrypt encrypt) | |||
{ | |||
List<byte> dataList = new List<byte>(); | |||
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<byte> dataList = new List<byte>(); | |||
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; | |||
} | |||
} | |||
} |
@@ -0,0 +1,8 @@ | |||
namespace JT809.Protocol | |||
{ | |||
public interface IEncrypt | |||
{ | |||
void Encrypt(byte[] buffer); | |||
void Decrypt(byte[] buffer); | |||
} | |||
} |
@@ -0,0 +1,8 @@ | |||
namespace JT809.Protocol | |||
{ | |||
public interface IEscape | |||
{ | |||
byte[] Escape(byte[] dataContent, IEncrypt encrypt); | |||
byte[] UnEscape(byte[] dataContent, IEncrypt encrypt); | |||
} | |||
} |
@@ -4,4 +4,8 @@ | |||
<TargetFramework>netstandard2.0</TargetFramework> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<Folder Include="Encrypt\" /> | |||
</ItemGroup> | |||
</Project> |