XTEA

Hi, does anybody have the xtea implemented in c++? I have found this:
http://www.daniweb.com/software-development/cpp/threads/52759/246419#post246419
but for some reasons the encripted string isn't displayed - outbuf and i do not why.
Here you go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <fstream>
#include <stdint.h>
using namespace std;


unsigned int key[4]={0xFDA5,0xD54E,0xFC00,0xB55A}; // encryption key

#define BLOCK_SIZE 8
void xtea_encipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], sum=0, delta=0x9E3779B9;
    for (i=0; i < num_rounds; i++) {
        v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
        sum += delta;
        v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
    }
    v[0]=v0; v[1]=v1;
}

void xtea_decipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], delta=0x9E3779B9, sum=delta*num_rounds;
    for (i=0; i < num_rounds; i++) {
        v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
        sum -= delta;
        v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
    }
    v[0]=v0; v[1]=v1;
}

void FileCrypt(string filename,bool encrypt)
{
  fstream file(filename.c_str(),ios::in | ios::out | ios::binary);

  if(!file)
    {
      cout <<"Could not open file";
      return;
    }

  unsigned size;

  file.seekg(0,ios::end);
  size=file.tellg();
  file.seekg(ios::beg);

  file.clear();

  unsigned pos;

  int n_blocks=size/BLOCK_SIZE;
  if(size%BLOCK_SIZE!=0)
      ++n_blocks;

  for(int i=0;i<n_blocks;i++)
    {
      unsigned char data[BLOCK_SIZE];
      pos=file.tellg();

      file.read((char*)data,BLOCK_SIZE); // read data block

      if(encrypt)
          xtea_encipher(32,(uint32_t*)data,key);
      else
          xtea_decipher(32,(uint32_t*)data,key);

      file.seekp(pos);
      file.write((char*)data,BLOCK_SIZE);

      memset(data,0,BLOCK_SIZE);
    }
  file.close();
}

int main()
{
   FileCrypt("file.txt",true); // encrypt
   FileCrypt("file.txt",false); // decrypt
}


edit: fixed a few bugs
Last edited on
Thanks but for the strings do you have it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
void StringCrypt(char *inout,int len,bool encrypt)
{
  for(int i=0;i<len/BLOCK_SIZE;i++)
    {
      if(encrypt)
          xtea_encipher(32,(uint32_t*)(inout+(i*BLOCK_SIZE)),key);
      else
          xtea_decipher(32,(uint32_t*)(inout+(i*BLOCK_SIZE)),key);
    }
  if(len%BLOCK_SIZE!=0)
    {
        int mod=len%BLOCK_SIZE;
        int offset=(len/BLOCK_SIZE)*BLOCK_SIZE;
        char data[BLOCK_SIZE];
        memcpy(data,inout+offset,mod);

        if(encrypt)
            xtea_encipher(32,(uint32_t*)data,key);
        else
            xtea_decipher(32,(uint32_t*)data,key);

        memcpy(inout+offset,data,mod);
    }
}



int main()
{
  char str[]={"Hello, world!"};
  int len=strlen(str)+1; // length of the string including null character

  StringCrypt(str,len,true);

  cout <<"Encrypted string: ";
  for(int i=0;i<len;i++)
      cout <<str[i];
  cout <<endl;

  StringCrypt(str,len,false);

  cout <<"Decrypted string: ";
  cout <<str;
}
@Null
Not a huge deal but, you really should acknowledge your sources http://en.wikipedia.org/wiki/XTEA#Implementations
Last edited on
Thanks a lot but -
1
2
Encrypted string: Jÿæt¬æX¤zk[ÙB4´w¨0?
Decrypted string: Hello, wuf«Ç˜GB4´w¨0?

Where could be the problem?
And also the key cannot be a string or a simple int value?
Last edited on
I guess the problem is that size of char str[] is not divisible by BLOCK_SIZE.

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  int main()
{
  char str[16]={"Hello, world!"}; // 16 mod 8 =0
  int len=sizeof(str);

  StringCrypt(str,len,true);

  cout <<"Encrypted string: ";
  for(int i=0;i<len;i++)
      cout <<str[i];
  cout <<endl;

  StringCrypt(str,len,false);

  cout <<"Decrypted string: ";
  cout <<str;
}


And also the key cannot be a string or a simple int value?

I'm not sure but I think no because Wikipedia says that key size for XTEA algorithm is 128 bits
Now is fine, thanks.

http://www.php-einfach.de/sonstiges_generator_xtea.php
So a key could be a string/int but the question is how.
Last edited on
Topic archived. No new replies allowed.