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
| #include <stdio.h> #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) { rounds = 52/n+6; 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); printf("\n%x\n",sum); } else if (n < -1) { n = -n; rounds = 52/n+6; sum = 0xcc623af3; 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; sum -= DELTA; } while (--rounds); printf("\n%x\n",sum); } } int main() { uint32_t v[11]= {689085350, 626885696, 1894439255, 1204672445, 1869189675, 475967424, 1932042439, 1280104741, 2808893494}; uint32_t const k[4]={12345678, 12398712, 91283904, 12378192}; int i,n= 9; printf(" 解 密 前 原 始 数 据:%x %x %x %x %x %x %x %x %x %x %x\n",v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8]); btea(v, -n, k); printf(" 解 密 后 的 数 据:%x %x %x %x %x %x %x %x %x %x %x\n",v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8]); for(i=0;i<9;i++) { printf("%c",v[i]&0xff); printf("%c",(v[i]&0xff00)%0xff); printf("%c",(v[i]&0xff0000)%0xffff); printf("%c",(v[i]&0xff000000)%0xffffff); } btea(v, n, k); printf("\n 加 密 后 的 数 据:%x %x %x %x %x %x %x %x %x %x %x\n",v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8]); system("pause"); return 0; }
|