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
|
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 = DELTA; z = v[n - 1]; do { 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; //printf("%x\n", y); sum -= 0x60A8894A; } 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; sum += 0x60A8894A; } while (--rounds); } }
int main() { // uint32_t v[9] = {0x87e25a01, 0x3c4364d2, 0x0ec3cc97, 0xd06cbb92, 0xb8411653, 0x43e7378d, 0x52947a3b, 0x1e5abac4, 0xd75382e7}; uint32_t v[9] = {0xc5268710, 0x6d7b0e5e, 0xc5d265c7, 0xda3a935f, 0xa0c5a950, 0x2daeb502, 0x228e3628, 0x1db6ceb5, 0xd5386a5}; uint32_t ss[9] = { 0x68E5973E, 0xC20C7367, 0x98AFD41B, 0xFE4B9DE2, 0x01A5B60B, 0x3D36D646, 0xDBCC7BAF, 0xA0414F00, 0x762CE71A}; uint32_t const k[4] = {1, 0x10, 0x100, 0x1000}; int n = 9; //n的绝对值表示v的长度,取正表示加密,取负表示解密 // v为要加密的数据是两个32位无符号整数 // k为加密解密密钥,为4个32位无符号整数,即密钥长度为128位 // printf("加密前原始数据:%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]); //btea(v, n, k); // printf("加密后的数据:%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]); btea(ss, -n, k); printf("解密后的数据:%x %x %x %x %x %x %x %x %x\n", ss[0], ss[1], ss[2], ss[3], ss[4], ss[5], ss[6], ss[7], ss[8]); //printf("加密前原始数据:%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]); return 0; }
|