Browse Source

增加加密串方法

tags/v1.3.0
SmallChi 7 years ago
parent
commit
415894f3e0
3 changed files with 33 additions and 3 deletions
  1. +2
    -1
      src/JT809.Protocol/Enums/ErrorCode.cs
  2. +20
    -1
      src/JT809.Protocol/ProtocolPacket/Extensions/PackageExtensions.cs
  3. +11
    -1
      src/JT809.Protocol/ProtocolPacket/Package.cs

+ 2
- 1
src/JT809.Protocol/Enums/ErrorCode.cs View File

@@ -6,6 +6,7 @@ namespace JT809.Protocol.Enums
{ {
public enum ErrorCode public enum ErrorCode
{ {
CRC16CheckInvalid = 1001
CRC16CheckInvalid = 1001,
HeaderLengthNotEqualBodyLength=1002
} }
} }

+ 20
- 1
src/JT809.Protocol/ProtocolPacket/Extensions/PackageExtensions.cs View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using JT809.Protocol.Configs;
using System.Collections.Generic;




namespace JT809.Protocol.ProtocolPacket.Extensions namespace JT809.Protocol.ProtocolPacket.Extensions
@@ -96,5 +97,23 @@ namespace JT809.Protocol.ProtocolPacket.Extensions
crc &= 0xffff; crc &= 0xffff;
return crc; return crc;
} }
internal static byte[] Encrypt(this Package packege, byte[] buffer, int size, JT809EncryptConfig Config)
{
if (0 == Config.Key)
{
Config.Key = 1;
}
uint mkey = Config.M1;
if (0 == mkey)
{
mkey = 1;
}
for (int idx = 0; idx < size; idx++)
{
Config.Key = Config.IA1 * (Config.Key % mkey) + Config.IC1;
buffer[idx] ^= (byte)((Config.Key >> 20) & 0xFF);
}
return buffer;
}
} }
} }

+ 11
- 1
src/JT809.Protocol/ProtocolPacket/Package.cs View File

@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Text; using System.Text;
using JT809.Protocol.Configs;
using JT809.Protocol.Enums; using JT809.Protocol.Enums;
using JT809.Protocol.Exceptions; using JT809.Protocol.Exceptions;
using JT809.Protocol.ProtocolPacket.Extensions; using JT809.Protocol.ProtocolPacket.Extensions;
@@ -18,6 +19,8 @@ namespace JT809.Protocol.ProtocolPacket
public const byte BeginFlag = 0X5B; public const byte BeginFlag = 0X5B;
public const byte EndFlag = 0X5D; public const byte EndFlag = 0X5D;
public JT809EncryptConfig Config;
public Header Header { get; private set; } public Header Header { get; private set; }
public MessageBody Body { get; private set; } public MessageBody Body { get; private set; }
@@ -52,14 +55,21 @@ namespace JT809.Protocol.ProtocolPacket
case EncryptOpitions.None: case EncryptOpitions.None:
break; break;
case EncryptOpitions.Common: case EncryptOpitions.Common:
// bodyBuffer01 = this.EncryptExt(Header.KeyForCommonEncrypt, bodyBuffer01, bodyBuffer01.Length, Config.Value.ServiceGB809Info.MasterLinkInfo.M1, Config.Value.ServiceGB809Info.MasterLinkInfo.IA1, Config.Value.ServiceGB809Info.MasterLinkInfo.IC1);
bodyBuffer01 = this.Encrypt(bodyBuffer01, bodyBuffer01.Length, Config);
break; break;
} }
if (Header.Length != (bodyBuffer01.Length + NotDataLength)) throw new JT809Exception(ErrorCode.HeaderLengthNotEqualBodyLength);
Body = GenerateBody(Header.BusinessID, bodyBuffer01);
} }
protected override void OnWriteToBuffer(BinaryWriter writer) protected override void OnWriteToBuffer(BinaryWriter writer)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public static MessageBody GenerateBody(BusinessType businessID, byte[] bodyBuffer)
{
return Activator.CreateInstance(typeof(object), bodyBuffer) as MessageBody;
}
} }
} }

Loading…
Cancel
Save