标签:
#include <iostream> #include <stdio.h> using namespace std; #include <stdint.h> #define DELTA 0x9e3779b9 #define MX (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (key[(p&3)^e] ^ z))) void btea(uint32_t *v, int n, uint32_t const key[4]) { uint32_t y, z, sum; unsigned p, rounds, e; if (n > 1) { /* Coding Part */ rounds = 6 + 52/n; sum = 0; z = v[n-1]; do { sum += DELTA; e = (sum >> 2) & 3; for (p=0; p<n-1; p++) { y = v[p+1]; z = v[p] += MX; } y = v[0]; z = v[n-1] += MX; } while (--rounds); } else if (n < -1) { /* Decoding Part */ n = -n; rounds = 6 + 52/n; sum = rounds*DELTA; y = v[0]; do { e = (sum >> 2) & 3; for (p=n-1; p>0; p--) { z = v[p-1]; y = v[p] -= MX; } z = v[n-1]; y = v[0] -= MX; } while ((sum -= DELTA) != 0); } } int main() { uint32_t const key[4]={0x01234567,0x89ABCDEF,0xFEDCBA98,0x76543210}; uint32_t data[2]={0x12345678,0x87654321}; uint32_t *sent=data; btea(sent,2,key); printf("coded:%x %x\n",sent[0],sent[1]); btea(sent,-2,key); printf("decoded:%x %x\n",sent[0],sent[1]); return 0; }
标签:
原文地址:http://blog.csdn.net/libinjlu/article/details/43030369