73 lines
2.3 KiB
C#
73 lines
2.3 KiB
C#
namespace CommonConsole
|
|
{
|
|
using Common.Utils.Extensions;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
|
|
public class Program
|
|
{
|
|
public static void Main()
|
|
{
|
|
var econnector = new EregisterConnector.EregisterConnector();
|
|
|
|
econnector.InitInspection();
|
|
}
|
|
|
|
//var payloadLength = 32;
|
|
//var payload = new Byte[payloadLength];
|
|
|
|
//for (var i = 0; i < 32; i++)
|
|
//{
|
|
// payload[i] = (Byte)(i + 1);
|
|
//}
|
|
|
|
//var withAddress = new Byte[payloadLength + 4];
|
|
|
|
//Array.Copy(payload, 0, withAddress, 4, payloadLength);
|
|
|
|
//var keyLength = 16;
|
|
//var key = new Byte[keyLength];
|
|
|
|
//for (var i = 0; i < keyLength; i++)
|
|
//{
|
|
// payload[i] = (Byte)(keyLength - i);
|
|
//}
|
|
|
|
//Console.WriteLine(BitConverter.ToString(key));
|
|
//Console.WriteLine(BitConverter.ToString(withAddress));
|
|
//Console.WriteLine(BitConverter.ToString(Encrypt(payload, key, 0)));
|
|
|
|
static Byte[] Encrypt(Byte[] bytes, Byte[] key, Int32 address)
|
|
{
|
|
var requestBytesList = new List<Byte>(bytes);
|
|
var addressBytes = address.ToBigEndianBytes();
|
|
requestBytesList.InsertRange(9, addressBytes);
|
|
|
|
var bytesToEncryptCount = requestBytesList.Count; // 4(address) + 2(crc)
|
|
var keyByte = Byte.MaxValue;
|
|
var reference = address;
|
|
|
|
for (Int32 encryptIndex = 0; encryptIndex < bytesToEncryptCount; encryptIndex++, encryptIndex++)
|
|
{
|
|
// Update KeyDefinition for this byte
|
|
keyByte += (Byte)(((reference >> ((keyByte & 0x3) << 3)) & 0xFF) >> ((keyByte >> 6) & 1));
|
|
|
|
// Update the unencrypted reference before the byte gets encrypted
|
|
reference = reference << 8;
|
|
reference += requestBytesList[encryptIndex];
|
|
|
|
// Encrypt the data by applying the resepective key
|
|
requestBytesList[encryptIndex] ^= key[(keyByte >> 2) & 0x0F];
|
|
|
|
// Check if data has to be inverted
|
|
if (keyByte > 127)
|
|
{
|
|
requestBytesList[encryptIndex] = (Byte)~requestBytesList[encryptIndex];
|
|
}
|
|
}
|
|
|
|
return requestBytesList.ToArray();
|
|
}
|
|
}
|
|
} |