基本介紹
- 中文名:TEA加密算法
- 發現人:David Wheeler和Roger Needham
- 時間:1994
- 類型:分組密碼算法
內容,歷史,套用,
內容
代碼如下:
void qq_encipher(unsigned long *const plain, const unsigned long *const key, unsigned long *const crypt)
{
unsigned long left = plain[0],right = plain[1],
a = key[0], b = key[1],
c = key[2], d = key[3],
n = 32, sum = 0,
delta = 0x9E3779B9;
// 明文輸入被分為左右兩部分,密鑰分為四部分存入暫存器,n表示加密輪數推薦32。Delta為一常數。
while (n-- > 0) {
sum += delta;
left += ((right << 4) + a) ^ (right + sum) ^ ((right >> 5) + b);
right += ((left << 4) + c) ^ (left + sum) ^ ((left >> 5) + d);
}
crypt[0] = left ;
crypt[1] = right ;
}
void decrypt(unsigned long *v, unsigned long *k) {
unsigned long y=v[0], z=v[1], sum=0xC6EF3720, i; /* set up */
unsigned long delta=0x9e3779b9; /* a key schedule constant */
unsigned long a=k[0], b=k[1], c=k[2], d=k[3]; /* cache key */
for(i=0; i<32; i++){ /* basic cycle start */
z -= ((y<<4) + c) ^ (y + sum) ^ ((y>>5) + d);
y -= ((z<<4) + a) ^ (z + sum) ^ ((z>>5) + b);
sum -= delta; /* end cycle */
}
v[0]=y;
v[1]=z;
}
}
v[0]=y;
v[1]=z;
}