@@ -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); | |||
} | |||
} | |||
} |
@@ -1,41 +0,0 @@ | |||
using JT809.Protocol.Configs; | |||
namespace JT809.Protocol.Encrypt | |||
{ | |||
/// <summary> | |||
/// JT809 异或加密解密为同一算法 | |||
/// </summary> | |||
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); | |||
} | |||
} | |||
} | |||
} |
@@ -1,85 +0,0 @@ | |||
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(); | |||
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; | |||
} | |||
} | |||
} |
@@ -1,8 +0,0 @@ | |||
namespace JT809.Protocol | |||
{ | |||
public interface IEncrypt | |||
{ | |||
void Encrypt(byte[] buffer); | |||
void Decrypt(byte[] buffer); | |||
} | |||
} |
@@ -1,8 +0,0 @@ | |||
namespace JT809.Protocol | |||
{ | |||
public interface IEscape | |||
{ | |||
byte[] Escape(byte[] dataContent, IEncrypt encrypt); | |||
byte[] UnEscape(byte[] dataContent, IEncrypt encrypt); | |||
} | |||
} |
@@ -0,0 +1,8 @@ | |||
namespace JT809.Protocol | |||
{ | |||
public interface IJT809Encrypt | |||
{ | |||
byte[] Encrypt(byte[] buffer); | |||
byte[] Decrypt(byte[] buffer); | |||
} | |||
} |
@@ -10,17 +10,17 @@ | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<Compile Remove="Configs\**" /> | |||
<Compile Remove="JT809Configs\**" /> | |||
<Compile Remove="Encrypt\**" /> | |||
<Compile Remove="JT809Enums\**" /> | |||
<Compile Remove="Escape\**" /> | |||
<Compile Remove="ProtocolPacket\**" /> | |||
<EmbeddedResource Remove="Configs\**" /> | |||
<EmbeddedResource Remove="JT809Configs\**" /> | |||
<EmbeddedResource Remove="Encrypt\**" /> | |||
<EmbeddedResource Remove="JT809Enums\**" /> | |||
<EmbeddedResource Remove="Escape\**" /> | |||
<EmbeddedResource Remove="ProtocolPacket\**" /> | |||
<None Remove="Configs\**" /> | |||
<None Remove="JT809Configs\**" /> | |||
<None Remove="Encrypt\**" /> | |||
<None Remove="JT809Enums\**" /> | |||
<None Remove="Escape\**" /> | |||
@@ -29,11 +29,11 @@ | |||
<ItemGroup> | |||
<Compile Remove="Constants.cs" /> | |||
<Compile Remove="IEncrypt.cs" /> | |||
<Compile Remove="IEscape.cs" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<Compile Include="JT809Configs\JT809EncryptOptions.cs" /> | |||
<Compile Include="JT809Enums\JT809ErrorCode.cs" /> | |||
<Compile Include="JT809Enums\JT809BusinessType.cs" /> | |||
<Compile Include="JT809Enums\JT809SubBusinessType.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; } | |||
} | |||
} |
@@ -0,0 +1,43 @@ | |||
using JT809.Protocol.JT809Configs; | |||
namespace JT809.Protocol.JT809Encrypt | |||
{ | |||
/// <summary> | |||
/// JT809 异或加密解密为同一算法 | |||
/// </summary> | |||
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; | |||
} | |||
} | |||
} |