@@ -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 | |||
}; | |||
} | |||
} |
@@ -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); | |||
} | |||
} | |||
} |
@@ -1,6 +1,6 @@ | |||
<?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')" /> | |||
<Import Project="..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props')" /> | |||
<PropertyGroup> | |||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | |||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | |||
@@ -39,10 +39,13 @@ | |||
</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> | |||
<HintPath>..\packages\MSTest.TestFramework.1.2.1\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> | |||
<HintPath>..\packages\MSTest.TestFramework.1.2.1\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath> | |||
</Reference> | |||
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> | |||
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath> | |||
</Reference> | |||
<Reference Include="System" /> | |||
<Reference Include="System.Core" /> | |||
@@ -68,8 +71,8 @@ | |||
<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'))" /> | |||
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.props'))" /> | |||
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.2.1\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')" /> | |||
<Import Project="..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.2.1\build\net45\MSTest.TestAdapter.targets')" /> | |||
</Project> |
@@ -1,5 +1,6 @@ | |||
<?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" /> | |||
<package id="MSTest.TestAdapter" version="1.2.1" targetFramework="net461" /> | |||
<package id="MSTest.TestFramework" version="1.2.1" targetFramework="net461" /> | |||
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net461" /> | |||
</packages> |
@@ -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; } | |||
} | |||
} |
@@ -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); | |||
} | |||
} | |||
} | |||
@@ -0,0 +1,11 @@ | |||
namespace JT809.Protocol.ProtocolPacket | |||
{ | |||
/// <summary> | |||
/// 报文加密标识位 b: 0 表示报文不加密,1 表示报文加密。 | |||
/// </summary> | |||
public enum EncryptEnum : byte | |||
{ | |||
No = 0X00, | |||
Yes = 0X01, | |||
} | |||
} |
@@ -0,0 +1,15 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Text; | |||
namespace JT809.Protocol.ProtocolPacket | |||
{ | |||
/// <summary> | |||
/// Message Header 数据头 | |||
/// </summary> | |||
public class Header | |||
{ | |||
public Version Version { get; private set; } | |||
public EncryptEnum EncryptEnum { get; private set; } | |||
} | |||
} |
@@ -0,0 +1,46 @@ | |||
namespace JT809.Protocol.ProtocolPacket | |||
{ | |||
/// <summary> | |||
/// 协议版本好标识,上下级平台之间采用的标准协议版编号; | |||
/// 长度为 3 个字节来表示, | |||
/// 0x01 0x02 0x0F 标识的版本号是 v1.2.15,以此类推。 | |||
/// </summary> | |||
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}"; | |||
} | |||
} | |||
} |