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 81 82 83 84 85 86 87 88 89 90
|
#include <iostream>
#include <iomanip>
void encrypt(uint32_t* v, const uint32_t k[4])
{
uint32_t sum{0};
uint32_t delta{0x9E3779B9};
for (int i = 0; i < 32; i++)
{
sum += delta;
v[0] += ((v[1]<<4) + k[0]) ^ (v[1] + sum) ^ ((v[1]>>5) + k[1]);
v[1] += ((v[0]<<4) + k[2]) ^ (v[0] + sum) ^ ((v[0]>>5) + k[3]);
}
}
void decrypt(uint32_t* v, const uint32_t k[4])
{
uint32_t sum = 0xC6EF3720;
uint32_t delta = 0x9E3779B9;
for (int i = 0; i < 32; i++)
{
v[1] -= ((v[0]<<4) + k[2]) ^ (v[0] + sum) ^ ((v[0]>>5) + k[3]);
v[0] -= ((v[1]<<4) + k[0]) ^ (v[1] + sum) ^ ((v[1]>>5) + k[1]);
sum -= delta;
}
}
void demonstrate(uint32_t* v, const uint32_t k[4])
{
std::cout
<< std::hex << std::showbase
<< " Original: " << v[0] << ' ' << v[1] << '\n';
encrypt(v,k);
std::cout
<< "Encrypted: " << v[0] << ' ' << v[1] << '\n';
decrypt(v,k);
std::cout
<< "Decrypted: " << v[0] << ' ' << v[1] << '\n'
<< '\n';
}
int main()
{
uint32_t a = 0x4e238eb9;
std::cout
<< std::showbase
<< std::hex << a << '\n'
<< std::dec << a << '\n';
uint32_t v1[]{1105869322,2495260992};//{99,88};
uint32_t k1[]{2495260992,1105869322,802249527,659110869};//{0,0,0,0};
demonstrate(v1, k1);
uint32_t v2[]{0x31c5fa6c, 0x241756d6};
uint32_t k2[]{0xbc3a7de2,0x845846cf,0x2794a127,0x6b8ea8b8};
demonstrate(v2, k2);
uint32_t v3[]{0x2bb0f1b3,0xc023ed11};
uint32_t k3[]{0x5c60bff2,0x7072d01c,0x4513c5eb,0x8f3a38ab};
demonstrate(v3, k3);
return 0;
}
/*
1 0 9e3779b9 0 0 0 0
2 ec01a1de aaa0256d 0 0 0 0
4 bc3a7de2 4e238eb9 0 0 ec01a1de 114f6d74
8 31c5fa6c 241756d6 bc3a7de2 845846cf 2794a127 6b8ea8b8
16 1d8e6992 9a478905 6a1d78c8 8c86d67 2a65bfbe b4bd6e46
32 d26428af a202283 27f917b1 c1da8993 60e2acaa a6eb923d
64 7a01cbc9 b03d6068 62ee209f 69b7afc 376a8936 cdc9e923
1 0 0 0 0 0 0
tea does the same but the number of iterations
is constant at 32
1 41ea3a0a 94baa940 0 0 0 0
2 4e8e7829 7d8236d8 0 0 0 41ea3a0a
4 b84e28af b6b62088 0 41ea3a0a 4e8e7829 c88ba95e
8 5ddf75d9 7a4ce68f b84e28af a0a47295 ed650698 1024eea0
16 4ec5d2e2 5ada1d89 a9c3801a 32a1e654 8b63bb9 21410574
32 7d2c6c57 7a6adb4d 69c53e0f 60388ada df70a1f5 d9cb4e09
64 2bb0f1b3 c023ed11 5c60bff2 7072d01c 4513c5eb 8f3a38ab
1 0 0 0 0 0 0
*/
|