Explorar el Código

增加加解密接口待实现

tags/1.0.0
SmallChi hace 7 años
padre
commit
a74b28b52a
Se han modificado 6 ficheros con 104 adiciones y 2 borrados
  1. +12
    -0
      src/GBNewEnergy.Protocol/INEEncrypt.cs
  2. +28
    -0
      src/GBNewEnergy.Protocol/NEEncryptFactory.cs
  3. +20
    -0
      src/GBNewEnergy.Protocol/NEEncrypts/NE_AES128EncryptImpl.cs
  4. +20
    -0
      src/GBNewEnergy.Protocol/NEEncrypts/NE_RSAEncryptImpl.cs
  5. +23
    -2
      src/GBNewEnergy.Protocol/NEPackage.cs
  6. +1
    -0
      src/GBNewEnergy.Protocol/NEProperties/NEPackageProperty.cs

+ 12
- 0
src/GBNewEnergy.Protocol/INEEncrypt.cs Ver fichero

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace GBNewEnergy.Protocol
{
public interface INEEncrypt
{
byte[] Encrypt(byte[] buffer);
byte[] Eecrypt(byte[] buffer);
}
}

+ 28
- 0
src/GBNewEnergy.Protocol/NEEncryptFactory.cs Ver fichero

@@ -0,0 +1,28 @@
using GBNewEnergy.Protocol.Enums;
using GBNewEnergy.Protocol.NEEncrypts;
using System;
using System.Collections.Generic;
using System.Text;

namespace GBNewEnergy.Protocol
{
internal class NEEncryptFactory
{
internal static INEEncrypt GetNEEncrypt(NEEncryptMethod nEEncryptMethod)
{
switch (nEEncryptMethod)
{
case NEEncryptMethod.None:
case NEEncryptMethod.Invalid:
case NEEncryptMethod.Exception:
return null;
case NEEncryptMethod.AES128:
return new NE_AES128EncryptImpl();
case NEEncryptMethod.RSA:
return new NE_RSAEncryptImpl();
default:
return null;
}
}
}
}

+ 20
- 0
src/GBNewEnergy.Protocol/NEEncrypts/NE_AES128EncryptImpl.cs Ver fichero

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace GBNewEnergy.Protocol.NEEncrypts
{
#warning 待加解密
public class NE_AES128EncryptImpl : INEEncrypt
{
public byte[] Eecrypt(byte[] buffer)
{
throw new NotImplementedException();
}

public byte[] Encrypt(byte[] buffer)
{
throw new NotImplementedException();
}
}
}

+ 20
- 0
src/GBNewEnergy.Protocol/NEEncrypts/NE_RSAEncryptImpl.cs Ver fichero

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace GBNewEnergy.Protocol.NEEncrypts
{
#warning 待加解密
public class NE_RSAEncryptImpl : INEEncrypt
{
public byte[] Eecrypt(byte[] buffer)
{
throw new NotImplementedException();
}

public byte[] Encrypt(byte[] buffer)
{
throw new NotImplementedException();
}
}
}

+ 23
- 2
src/GBNewEnergy.Protocol/NEPackage.cs Ver fichero

@@ -70,6 +70,11 @@ namespace GBNewEnergy.Protocol
/// 数据体 /// 数据体
/// </summary> /// </summary>
public NEBodies Bodies { get; protected set; } public NEBodies Bodies { get; protected set; }
/// <summary>
/// 数据单元加密
/// 当数据单元存在加密时,应先加密后校验,先校验后解密
/// </summary>
private INEEncrypt Encrypt;


protected override void ToBuffer() protected override void ToBuffer()
{ {
@@ -83,7 +88,14 @@ namespace GBNewEnergy.Protocol
Buffer.WriteLittle(VIN, 4); Buffer.WriteLittle(VIN, 4);
Buffer[21] = (byte)EncryptMethod; Buffer[21] = (byte)EncryptMethod;
Buffer.WriteLittle(DataUnitLength, 22, 2); Buffer.WriteLittle(DataUnitLength, 22, 2);
Buffer.WriteLittle(Bodies.Buffer, 24, DataUnitLength);
if (Encrypt != null)
{
Buffer.WriteLittle(Encrypt.Encrypt(Bodies.Buffer), 24, DataUnitLength);
}
else
{
Buffer.WriteLittle(Bodies.Buffer, 24, DataUnitLength);
}
BCCCode = Buffer.ToXor(2, (HeaderFixedByteLength + DataUnitLength - 1)); BCCCode = Buffer.ToXor(2, (HeaderFixedByteLength + DataUnitLength - 1));
Buffer[HeaderFixedByteLength + DataUnitLength] = BCCCode; Buffer[HeaderFixedByteLength + DataUnitLength] = BCCCode;
Header = new byte[HeaderFixedByteLength]; Header = new byte[HeaderFixedByteLength];
@@ -98,6 +110,7 @@ namespace GBNewEnergy.Protocol
AskId = nEPackageProperty.AskId; AskId = nEPackageProperty.AskId;
Bodies = nEPackageProperty.Bodies; Bodies = nEPackageProperty.Bodies;
EncryptMethod = nEPackageProperty.EncryptMethod; EncryptMethod = nEPackageProperty.EncryptMethod;
Encrypt = NEEncryptFactory.GetNEEncrypt(EncryptMethod);
} }


protected override void InitializePropertiesFromBuffer() protected override void InitializePropertiesFromBuffer()
@@ -107,6 +120,7 @@ namespace GBNewEnergy.Protocol
AskId = (NEAskId)Buffer[3]; AskId = (NEAskId)Buffer[3];
VIN = Buffer.ReadStringLittle(4, 17); VIN = Buffer.ReadStringLittle(4, 17);
EncryptMethod = (NEEncryptMethod)Buffer[21]; EncryptMethod = (NEEncryptMethod)Buffer[21];
Encrypt = NEEncryptFactory.GetNEEncrypt(EncryptMethod);
DataUnitLength = Buffer.ReadUShortH2LLittle(22, 2); DataUnitLength = Buffer.ReadUShortH2LLittle(22, 2);
// 进行BCC校验码 // 进行BCC校验码
// 校验位 = 报文长度 - 最后一位(校验位) - 偏移量(2) // 校验位 = 报文长度 - 最后一位(校验位) - 偏移量(2)
@@ -120,7 +134,14 @@ namespace GBNewEnergy.Protocol
BCCCode = bCCCode2; BCCCode = bCCCode2;
byte[] bodiesBytes = new byte[DataUnitLength + CheckBit]; byte[] bodiesBytes = new byte[DataUnitLength + CheckBit];
Array.Copy(Buffer, HeaderFixedByteLength, bodiesBytes, 0, bodiesBytes.Length); Array.Copy(Buffer, HeaderFixedByteLength, bodiesBytes, 0, bodiesBytes.Length);
Bodies = NEBodiesFactory.GetNEBodiesByMsgId(MsgId, bodiesBytes);
if (Encrypt != null)
{
Bodies = NEBodiesFactory.GetNEBodiesByMsgId(MsgId, Encrypt.Eecrypt(bodiesBytes));
}
else
{
Bodies = NEBodiesFactory.GetNEBodiesByMsgId(MsgId, bodiesBytes);
}
Header = new byte[HeaderFixedByteLength]; Header = new byte[HeaderFixedByteLength];
Array.Copy(Buffer, 0, Header, 0, HeaderFixedByteLength); Array.Copy(Buffer, 0, Header, 0, HeaderFixedByteLength);
} }


+ 1
- 0
src/GBNewEnergy.Protocol/NEProperties/NEPackageProperty.cs Ver fichero

@@ -12,5 +12,6 @@ namespace GBNewEnergy.Protocol.NEProperties
public NEAskId AskId { get; set; } public NEAskId AskId { get; set; }
public NEBodies Bodies { get; set; } public NEBodies Bodies { get; set; }
public NEEncryptMethod EncryptMethod { get; set; } public NEEncryptMethod EncryptMethod { get; set; }
public INEEncrypt Encrypt { get; set; }
} }
} }

Cargando…
Cancelar
Guardar