ソースを参照

增加加密串方法

tags/v1.3.0
SmallChi 7年前
コミット
415894f3e0
3個のファイルの変更33行の追加3行の削除
  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 ファイルの表示

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

+ 20
- 1
src/JT809.Protocol/ProtocolPacket/Extensions/PackageExtensions.cs ファイルの表示

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


namespace JT809.Protocol.ProtocolPacket.Extensions
@@ -96,5 +97,23 @@ namespace JT809.Protocol.ProtocolPacket.Extensions
crc &= 0xffff;
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 ファイルの表示

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

読み込み中…
キャンセル
保存