Key_TinyEncryptionAlgorithm

Cant find Key_TinyEncryptionAlgorithm in 4.2??

I have tested GHI xtea and it works when encoding/decoding localy on G120 but to decode on PC the code from Jens Kühner doesn’t decode it back.

He has a parameter called IV and I don’t know what it should be to match GHI xtea function.

not sure if this will help but: https://www.ghielectronics.com/community/forum/topic?id=1592

Also you might want to browse the entries in the assembly System.Security, might be something there that works better for you.

Actually, the GHI implementation is the “standard” implementation, which supports blocks of 8 bytes. The code is shown on Wikipedia here http://en.wikipedia.org/wiki/XTEA

Here is a PC C# console app that includes the decoder and uses the same Key and example as found in the documentation http://www.ghielectronics.com/docs/49/xtea. I’ll be updating the documentation to include this decoder (as well as an encoder … as soon as I finish coding and testing it);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args){
            byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8,
                                       9, 10, 11, 12, 13, 14, 15, 16 };
            byte[] eline = new byte[] {214,35,6,78,132,80,106,174,188,254,196,59,5,135,9,255 };
            Console.WriteLine("hello world");
            Console.ReadLine();
            int length = 16;
           
           byte[] decrypted_bytes = new byte[16];
               
              Boolean result = Xtea.Decrypt(key,eline, decrypted_bytes,
                                             eline.Length);
            Console.WriteLine(result);
           string decrypted_string =
          new string(Encoding.UTF8.GetChars(decrypted_bytes));
           Console.WriteLine(decrypted_string);
           Console.ReadLine();
        }
    }
 public static class Xtea
    {
       
     public static Boolean Decrypt(byte[] key, byte[] encryptedData, byte[] plainData, int dataSize)
    {
	if( (dataSize % 8) == 0)
	{
		if(dataSize != 0)
		{
			dataSize /= 8;
		UInt32 i;
			UInt32[] v = new UInt32[2];
            UInt32 sum;
            UInt32[] lkey = new UInt32[4];
            int eoff = 0;
            int poff = 0;

            Buffer.BlockCopy(key,0,lkey,0,16);
			const UInt32 delta = 0x9E3779B9;
			while(dataSize-- !=0 )
			{
                Buffer.BlockCopy(encryptedData , eoff, v,0,8);
				sum=0xC6EF3720;

				for(i=0; i<32; i++)
				{
					v[1] -= (v[0] << 4 ^ v[0] >> 5) + v[0] ^ sum + lkey[sum>>11 & 3];
					sum -= delta;
					v[0] -= (v[1] << 4 ^ v[1] >> 5) + v[1] ^ sum + lkey[sum&3];
				}
                Buffer.BlockCopy(v,0,plainData,poff,8);
				poff+=8;
				eoff+=8;
			}
		}
		return true;
	}
	return false;
    }
    }
}
1 Like

@ Jeff -
Thanks Jeff!!!

It works :slight_smile: :slight_smile: :slight_smile: