laatzen/Common/CommonConsole/Program.cs
2024-09-09 16:37:07 +02:00

464 lines
9.7 KiB
C#

namespace CommonConsole
{
using Common.Hardware.Interfaces.Ports.Sirt;
using Common.Hardware.Interfaces.Ports.SirtBroadcaster.Parameters;
using Common.Hardware.WaterMeter.eRegister;
using System;
public class Program
{
public static void Main()
{
var programmingBatch = new JusteRegisterProgrammingBatch();
if (programmingBatch.StartOnCOMPort("COM6"))
{
Console.ReadKey();
programmingBatch.Stop();
}
else
{
}
}
// 01020304050607081112131415161718
// E6C88800DEB868C0D6A84880CE982840
//var encryptionKey = new Byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18 };
//var task = new JusteRegisterProgrammingTask
//{
// Cmd = JustSirtMessage.W_PAM,
// P1 = new P811
// {
// DelPamSemi = true,
// WakeUpCmd = true
// },
// Address = 4291305226,
// // Address = 319020415,
// Data = new Byte[]
// {
// // Change Key
// // 0x6C, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
// // LED on
// // 0x1F, 0x01, 0x08,
// // Reboot
// 0x0E, 0x00,
// // Provide Pin Auth Level I
// // 0x43, 0x01, 0x14, 0x30, 0x68, 0xD0,
// // Provide Pin Auth Level II
// // 0x43, 0x02, 0x14, 0x0E, 0x80, 0x40,
// // Provide Pin Auth Level III
// 0x43, 0x03, 0x63, 0x93, 0x73, 0x22,
// // Reset credits
// // 0x1F, 0x01, 0x03,
// },
// EncryptionKey = encryptionKey
//};
//programmingBatch.Run(task);
//task.Wait();
// 55-81-14-00-FF-C8-1F-0A-0B-07-FA-E3-AA-EA-8A-7B-7B-D9-A7-D7-D5-B8-16
// 55-81-14-00-FF-C8-1F-0A-0B-07-FA-E3-AA-EA-8A-7B-7B-D9-A7-D7-D5-B8-16
//var task = new JusteRegisterTask
//{
// Cmd = JustSirtMessage.W_PAM,
// P1 = new P811
// {
// DelPamSemi = true
// },
// Address = 319020415,
// Data = new Byte[]
// {
// // WUP - stay awake
// 0x00, 0x00,
// // LAT
// 0x02, 0x00,
// // MBUS - 7
// 0x05, 0x07,
// // TX
// 0x10, 0x00, 0x03,
// // Provide Pin Auth Level I
// // 0x43, 0x01, 0x14, 0x30, 0x68, 0xD0,
// // Provide Pin Auth Level II
// 0x43, 0x02, 0x14, 0x0E, 0x80, 0x40,
// // Provide Pin Auth Level III
// // 0x43, 0x03, 0x63, 0x93, 0x73, 0x22,
// // Reset credits
// // 0x1F, 0x01, 0x03,
// }
//};
//programmingBatch.Run(task);
//task.Wait();
//task = new JusteRegisterTask
//{
// Cmd = JustSirtMessage.W_PAM,
// P1 = new P811
// {
// DelPamSemi = true
// },
// Address = 319020415,
// Data = new Byte[]
// {
// // Parametersatz
// // Provide Pin Auth Level I
// // 0x43, 0x01, 0x14, 0x30, 0x68, 0xD0,
// // Provide Pin Auth Level II
// 0x43, 0x02, 0x14, 0x0E, 0x80, 0x40,
// // Provide Pin Auth Level III
// // 0x43, 0x03, 0x63, 0x93, 0x73, 0x22,
// // Reset credits
// // 0x1F, 0x01, 0x03,
// }
//};
//programmingBatch.Run(task);
//task.Wait();
public static byte[] Decrypt(Byte[] key, params Byte[] message)
{
var decryptLength = message.Length;
var decryptedBytes = new Byte[decryptLength];
for (int i = 0; i < decryptLength; i++)
{
decryptedBytes[i] = message[i];
}
var start = 14;
var end = start + decryptedBytes[7] - 6;
var token = decryptedBytes[9];
for (var i = start; i < end; i++)
{
token += (Byte)(decryptedBytes[i - (token & 0x3) - 1] >> ((token >> 6) & 0x1));
decryptedBytes[i] ^= key[(token >> 2) & 0x0F];
if (token > 127)
{
decryptedBytes[i] = (Byte)~decryptedBytes[i];
}
}
return decryptedBytes;
}
public static byte[] Encrypt(Byte[] key, UInt32 address, params Byte[] telegramBytes)
{
Console.WriteLine(address);
Console.WriteLine(BitConverter.ToString(key));
Console.WriteLine(BitConverter.ToString(telegramBytes));
var telegramLength = telegramBytes.Length;
var telegramIndex = 0;
var encryptedBytes = new Byte[telegramLength + 6];
var encryptLength = encryptedBytes.Length;
var index = 0;
encryptedBytes[index++] = (Byte)(address >> 24);
encryptedBytes[index++] = (Byte)(address >> 16);
encryptedBytes[index++] = (Byte)(address >> 8);
encryptedBytes[index++] = (Byte)(address >> 0);
foreach (var _byte in telegramBytes)
{
encryptedBytes[index++] = telegramBytes[telegramIndex++];
}
var telegramCRC = CalculateCrc(encryptedBytes, 1, (UInt16)(encryptLength - 3));
encryptedBytes[index++] = (Byte)(telegramCRC >> 8);
encryptedBytes[index++] = (Byte)(telegramCRC >> 0);
Console.WriteLine(BitConverter.ToString(encryptedBytes));
var token = (Byte)0xFF;
var reference = address;
for (var i = 0; i < encryptLength; i++)
{
// Update pKeyDefinition for this byte
token += (Byte)(((reference >> ((token & 0x3) << 3)) & 0xFF) >> ((token >> 6) & 1));
// Update the unencrypted reference before the byte gets encrypted
reference = (reference << 8);
reference += encryptedBytes[i];
// Encrypt the data by applying the resepective key
encryptedBytes[i] ^= key[((token >> 2) & 0x0F)];
// Check if data has to be inverted
if (token > 127)
{
encryptedBytes[i] = (Byte)~encryptedBytes[i];
}
}
Console.WriteLine(BitConverter.ToString(encryptedBytes));
Console.WriteLine();
return encryptedBytes;
}
public static ushort CalculateCrc(byte[] data, ushort start, ushort length)
{
byte zeichen, test;
ushort crc_result;
ushort ui_index;
byte h;
crc_result = 0xFFFF;
ui_index = start;
if (data.Length >= length + start)
{
while (ui_index < length + start)
{
zeichen = data[ui_index];
ui_index++;
for (h = 0; h < 8; h++)
{
test = 0;
if ((crc_result & 0x8000) != 0)
{
test = 0x80;
}
crc_result <<= 1;
if (((zeichen & 0x80) ^ test) != 0)
{
crc_result ^= 0x1021;
}
zeichen <<= 1;
}
}
}
return crc_result;
}
}
}
//var connector = EregisterConnector.CreateNew("COM6", 1000);
//connector.Add(1, 0319020415, 0319020415);//433
//connector.AddHexCommand(1, "00-00");
//connector.Add(2, 0319020441, "4291317405", "4291317405", null, false); // 433
//connector.InitInspection();
//connector.Add(3, 4291317405, 4291317405);//433
//connector.AddHexCommand(3, "00-00");
// connector.RunCommands();
//var connector = EregisterConnector.CreateNew("COM6", 1000);
//connector.Add(1, 0319020415, 0319020415);//433
//connector.AddHexCommand(1, "00-00");
//connector.Add(2, 0319020441, 0319020441);//433
//connector.AddHexCommand(2, "00-00");
//connector.Add(3, 4291317405, 4291317405);//433
//connector.AddHexCommand(3, "00-00");
//connector.InitInspection();
//var connection = new OmniConnection();
//connection.UseUniPro("COM16");
//var system = connection.ViewSystemParameters();
//connection.Disconnect();
// 0x08, 0x0E, 0x13, 0x03, 0xEE, 0xAC, 0x00, 0x00, 0x01, 0xF4, 0x00, 0x00, 0x92, 0x0A, 0x00, 0x0F, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x13, 0x13, 0x03, 0xEE, 0xAC, 0x07, 0x0E, 0x10, 0xA0, 0x57, 0x4E, 0xDA, 0xC0, 0x00, 0x00, 0x7F, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x03, 0x05, 0xA0, 0x0C, 0x03, 0x01, 0x2E, 0x46, 0x7C, 0x76, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x25, 0x14, 0x3B
// 0x0B, 0x10, 0x13, 0x03, 0xEE, 0xAC, 0x00, 0x0A, 0x11, 0x0B, 0x11, 0x08, 0x04, 0x02, 0xC8, 0xF4, 0xFB, 0xA7, 0x18, 0x00, 0x65, 0x00, 0x35, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x24, 0xEE, 0x34, 0x02, 0x01, 0x01, 0x16, 0x00, 0x25, 0x02, 0x71, 0x00, 0x00, 0x82, 0x71, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x08, 0xC3