temp commit: branch changing
This commit is contained in:
@@ -3,14 +3,71 @@
|
||||
using Common.Utils.Extensions;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main()
|
||||
{
|
||||
//var connector = new EregisterConnector();
|
||||
var econnector = new EregisterConnector.EregisterConnector();
|
||||
|
||||
//connector.InitInspection();
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user