SafeDispatch/MotoTRBO_XNL_Cmd/MTteaEncoder.cs
2024-02-22 18:43:59 +02:00

67 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MotoTRBO_XNL_Cmd
{
public class MTteaEncoder
{
public MTteaEncoder()
{
}
private void encipher(uint[] v, uint[] w, uint[] k)
{
uint y = v[0], z = v[1], sum = 0;
uint delta = 0x9E3779B9;
//uint delta = 0x12345678;
uint a = k[0], b = k[1], c = k[2], d = k[3];
uint n = 32;
while (n-- > 0)
{
sum += delta;
y += (z << 4) + a ^ z + sum ^ (z >> 5) + b;
z += (y << 4) + c ^ y + sum ^ (y >> 5) + d;
}
w[0] = y; w[1] = z;
}
public uint[] ConvertTea(byte c0, byte c1, byte c2, byte c3, byte c4, byte c5, byte c6, byte c7)
{
uint[] u_auth_key = { 0x152C7E9D, 0x38BE41C7, 0x71E96CA4, 0x6CAC1AFC };
//uint[] u_auth_key = { 0x11223344, 0x55667788, 0x99AABBCC, 0xDDEEFF00 };
byte[] random_num = { 0x72, 0xA4, 0x1f, 0x8f, 0x63, 0x66, 0x01, 0xaa };
random_num[0] = c0; random_num[1] = c1; random_num[2] = c2; random_num[3] = c3; random_num[4] = c4; random_num[5] = c5; random_num[6] = c6; random_num[7] = c7;
uint[] encrypted_num = new uint[2];
byte[] random_num1 = { random_num[0], random_num[1], random_num[2], random_num[3] };
byte[] random_num2 = { random_num[4], random_num[5], random_num[6], random_num[7] };
uint[] rand_num_uint = { htonl(random_num1), htonl(random_num2) };
encipher(rand_num_uint, encrypted_num, u_auth_key);
encrypted_num[0] = htonl(BitConverter.GetBytes(encrypted_num[0]));
encrypted_num[1] = htonl(BitConverter.GetBytes(encrypted_num[1]));
return encrypted_num;
}
private uint htonl(byte[] ar)
{
uint output;
output = ((uint)ar[3]);
output += ((uint)ar[2] << 8);
output += ((uint)ar[1] << 16);
output += ((uint)ar[0] << 24);
return output;
}
}
}