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
| #include <iostream> #include <cstdint> #include <vector>
using namespace std;
vector<uint8_t> sbox = {0xAE, 0x6F, 0xD5, 0xC9, 0xA0, 0x44, 0xB1, 0x95, 0x87, 0x21, 0x65, 0x8B, 0x74, 0x39, 0xA6, 0x84, 0x71, 0x31, 0x5E, 0x93, 0x77, 0xE0, 0xD0, 0x82, 0xBE, 0x3E, 0x1F, 0xC8, 0xC1, 0x67, 0x5C, 0xF9, 0xF6, 0x90, 0x89, 0x2A, 0xB6, 0xE1, 0xDE, 0x83, 0x88, 0x19, 0x3C, 0xC2, 0xE7, 0x1A, 0x4, 0x29, 0x4F, 0x27, 0x2F, 0xCB, 0x5D, 0x7F, 0xBA, 0x64, 0x49, 0x43, 0x30, 0x12, 0x41, 0xDB, 0x33, 0x10, 0x55, 0x3B, 0x35, 0x53, 0x40, 0x3D, 0xFF, 0x2, 0x9C, 0x51, 0x97, 0x5B, 0x22, 0x15, 0xB, 0x58, 0x4D, 0xB3, 0xEA, 0xC, 0x5A, 0xBF, 0xEE, 0xC6, 0xDF, 0xB7, 0x52, 0x60, 0xF8, 0xDC, 0x69, 0x1E, 0x6E, 0x14, 0x91, 0xA3, 0x98, 0x7C, 0xFC, 0x8, 0xCC, 0x7A, 0x46, 0x50, 0xC3, 0xB0, 0x68, 0x76, 0x9E, 0xCE, 0x62, 0x4A, 0xE4, 0x61, 0x2D, 0xE2, 0xD1, 0x4E, 0x85, 0x1B, 0x16, 0x6B, 0x79, 0x9, 0x94, 0x8F, 0xE8, 0x47, 0x7E, 0x81, 0x9D, 0x17, 0x80, 0x11, 0xC5, 0xD8, 0xF, 0x24, 0x96, 0xB2, 0x7, 0xED, 0xCA, 0xEC, 0x25, 0xA2, 0x3, 0xB5, 0x4C, 0xCF, 0x4B, 0xAA, 0xD4, 0xA8, 0xF2, 0x56, 0x9B, 0xAF, 0x57, 0x6C, 0xB8, 0x66, 0xF0, 0x7D, 0xAB, 0xD7, 0x34, 0xBC, 0x32, 0x59, 0x78, 0xD6, 0xC4, 0x1D, 0x99, 0xC0, 0x1, 0xEF, 0x92, 0x75, 0x2C, 0x63, 0x8A, 0xE, 0x38, 0x8D, 0x18, 0x6A, 0xE5, 0x2E, 0x26, 0x8E, 0x3F, 0xA1, 0xA5, 0x20, 0xF4, 0xFE, 0x1C, 0x42, 0xCD, 0xD2, 0x28, 0xBB, 0xD9, 0xA4, 0xEB, 0x9A, 0xC7, 0xA, 0xA9, 0x37, 0x8C, 0x5, 0xF3, 0x2B, 0x45, 0xDD, 0xFB, 0xAD, 0x6, 0xA7, 0x54, 0xD, 0x23, 0xB4, 0xF7, 0x0, 0x9F, 0x36, 0x70, 0xFA, 0x86, 0xE9, 0x6D, 0xF5, 0xDA, 0xD3, 0x72, 0xE6, 0xFD, 0x48, 0xE3, 0xBD, 0x73, 0xAC, 0x3A, 0x13, 0x7B, 0xB9, 0xF1, 0x5F};
void decrypt (uint32_t *v,uint32_t allRoundSum){ uint32_t v0=v[0],v1=v[1],sum=allRoundSum,i=0; uint32_t delta=0xc3c67cdc; do{ v1-=((v0 << 4)^(v0 >> 5) + v0) ^( sbox[(sum >>11)&0xff]+ sum); sum-=delta; v0-=((v1 << 3)^(v1 >> 3) + v1) ^( sbox[(sum &0x3)&0xff]+ sum); i++; } while(0x478 >= i); v[0]=v0;v[1]=v1; }
uint32_t encrypt (uint32_t *v){ uint32_t v0=v[0],v1=v[1],sum=0,i=0; uint32_t delta=0xc3c67cdc; do{ v0+=((v1 << 3)^(v1 >> 3) + v1) ^( sbox[(sum &0x3 )&0xff]+ sum); sum+=delta; v1+=((v0 << 4)^(v0 >> 5) + v0) ^( sbox[(sum >>11)&0xff] + sum); i++; } while(0x478 >= i); v[0]=v0;v[1]=v1; return sum; }
int main(){ uint32_t v[2]={0x64636261,0x68676665}; uint32_t enc[2]={0x3d2c5b74,0xf06203ec}; uint32_t sum = encrypt(v); cout<<hex<<v[0]<<" "<<v[1]<<endl; decrypt(enc,sum); cout<<hex<<enc[0]<<" "<<enc[1]<<endl; return 0; }
|