Search code examples
c++encryptionxxteaxtea

how to decrypt xxtea?


I am trying to se if I can use XXTEA in my application.

The code that I found on Wikipedia code is only for encryption and there is no code for decryption.

How can I decrypt with XXTEA?

I am developing c++ in windows (visual studio 2012)


Solution

  • The code snippet in the article apparently uses negative values of n to mean decoding.

    #include <stdio.h>
    #include <stdint.h>
    
    void btea(uint32_t *v, int n, uint32_t const key[4]); /* ... */
    
    int main(void) {
    
      char s[128] = "hello world bla bla bla bla bla bla";
      uint32_t key[4] = {1,2,3,4};
    
      int n = 128 / sizeof(uint32_t);
    
      printf("%s\n",s);
      btea((uint32_t*)s, n, key);
      printf("%s\n",s);
      btea((uint32_t*)s, -n, key);
      printf("%s\n",s);
    }
    

    prints:

    $ ./a.out | hexdump -C
    00000000  68 65 6c 6c 6f 20 77 6f  72 6c 64 20 62 6c 61 20  |hello world bla |
    00000010  62 6c 61 20 62 6c 61 20  62 6c 61 20 62 6c 61 20  |bla bla bla bla |
    00000020  62 6c 61 0a 2f 44 86 75  d5 16 83 bd 5d 20 af f3  |bla./D.u....] ..|
    00000030  a7 dd bf 9f 3a cd a0 13  ef 2b 89 48 2e f6 89 20  |....:....+.H... |
    00000040  e2 ba e4 9f ed 38 d1 86  43 82 9e a6 47 6c e4 6d  |.....8..C...Gl.m|
    00000050  a8 82 22 9e cb 5b d1 a1  18 14 ef 18 ca 23 26 cc  |.."..[.......#&.|
    00000060  18 1d 4f ba 1b d5 f5 d0  45 72 1c 69 b9 22 a3 08  |..O.....Er.i."..|
    00000070  44 71 1f 3b 8f a9 9d 5b  14 93 7b 59 b4 6b e8 1b  |Dq.;...[..{Y.k..|
    00000080  18 97 1b 74 61 d6 e9 e9  60 96 8e 4c 26 be 21 fc  |...ta...`..L&.!.|
    00000090  75 9f 4a 18 67 46 f0 95  2b ca 90 d9 f4 ce 3f 2f  |u.J.gF..+.....?/|
    000000a0  44 82 56 44 c0 0a 39 57  ff 7f 0a 68 65 6c 6c 6f  |D.VD..9W...hello|
    000000b0  20 77 6f 72 6c 64 20 62  6c 61 20 62 6c 61 20 62  | world bla bla b|
    000000c0  6c 61 20 62 6c 61 20 62  6c 61 20 62 6c 61 0a     |la bla bla bla.|
    000000cf