Want more features on Pastebin? Sign Up, it's FREE!
Guest

xtea

By: rasmusfaber on Mar 30th, 2012  |  syntax: C++  |  size: 1.33 KB  |  views: 65  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void encipher(long *v, long *k)
  5. {
  6.     long v0 = v[0], v1 = v[1];
  7.     long sum = 0;  
  8.     long  delta = 0x9e3779b9;
  9.     short rounds = 32;
  10.     for(long i = 0; i<rounds; i++)
  11.     {
  12.         v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
  13.       sum += delta;
  14.         v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum>>11) & 3]);
  15.     }
  16.     v[0] = v0;
  17.     v[1] = v1;
  18. }
  19.  
  20. void decipher(long* v, long *key) {
  21.     unsigned int i;
  22.     short rounds = 32;
  23.     long v0=v[0], v1=v[1], delta=0x9E3779B9, sum=delta*rounds;
  24.     for (i=0; i < rounds; i++) {
  25.         v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
  26.         sum -= delta;
  27.         v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
  28.     }
  29.     v[0]=v0; v[1]=v1;
  30. }
  31.  
  32. int main(){
  33.     long data[2]; // v0 and v1, 64bits
  34.     data[0] = 1;
  35.     data[1] = 1;
  36.  
  37.     long key[4]; // 4 * 4 bytes = 16bytes = 128bits
  38.     key[0] = 0x12345678;
  39.     key[1] = 0x90ABCDEF;
  40.     key[2] = 0xFEDCBA09;
  41.     key[3] = 0x87654321;
  42.  
  43.     cout << "READ: \t\t" << data[0] << endl << "\t\t" <<  data[1] << endl;
  44.     encipher(data, key);
  45.     cout << "ENCIPHERED: \t" << data[0] << endl << "\t\t" << data[1] << endl;
  46.     decipher(data, key);
  47.     cout << "DECIPHERED: \t" << data[0] << endl <<  "\t\t" <<  data[1] << endl;
  48. }
clone this paste RAW Paste Data