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; sum -= DELTA; } while (--rounds); } }
int main() { for (int i = 0; i <= 0xff; i++) { unsigned int b = 0x02003678; b = b + (i << 16); uint32_t v[2] = { (unsigned int)0x70797e55, b }; uint32_t const k[4] = { (unsigned int)0xE0C7E0C7, (unsigned int)0XC6F1D3D7, (unsigned int)0xc6d3c6d3, (unsigned int)0xc4d0d2ce }; int n = 2; //n的绝对值表示v的长度,取正表示加密,取负表示解密 // v为要加密的数据是两个32位无符号整数 // k为加密解密密钥,为4个32位无符号整数,即密钥长度为128位 btea(v, n, k); printf("%x%x\n", v[0], v[1]); } }