[TOC]
TEA算法简介
TEA算法是一种分组密码算法,其明文密文块为64比特,密钥长度为128比特。TEA算法利用不断增加的Delta(黄金分割率)值作为变化,使得每轮的加密是不同,该加密算法的迭代次数可以改变,建议的迭代次数为32轮。
TEA采用移位和异或操作,所以加密解密算法形式非常相似
TEA
#include <stdio.h>
#include <stdint.h>
//加密函数
void encrypt(uint32_t* data, uint32_t* key){
uint32_t data0 = data[0], data1 = data[1];
uint32_t sum = 0, i = 0;
uint32_t delta = 0x9e3779b9;
for(i = 0; i < 32; i++){
sum += delta;
data0 += ((data1<<4) + key[0]) ^ (data1 + sum) ^ ((data1>>5) + key[1]);
data1 += ((data0<<4) + key[2]) ^ (data0 + sum) ^ ((data0>>5) + key[3]);
}
data[0] = data0;
data[1] = data1;
}
//解密函数
void decrypt(uint32_t* data, uint32_t* key){
uint32_t data0 = data[0], data1 = data[1];
uint32_t sum = 0, i = 0;
uint32_t delta = 0x9e3779b9;
sum = delta * 32;
for(i = 0; i < 32; i++){
data1 -= ((data0<<4) + key[2]) ^ (data0 + sum) ^ ((data0>>5) + key[3]);
data0 -= ((data1<<4) + key[0]) ^ (data1 + sum) ^ ((data1>>5) + key[1]);
sum -= delta;
}
data[0] = data0;
data[1] = data1;
}
int main(){
uint32_t data[2] = {200, 100};
uint32_t key[4] = {2, 3, 4, 5};
printf("加密前原始数据:%u, %u\n", data[0], data[1]);
encrypt(data, key);
printf("加密后数据:%u, %u\n", data[0], data[1]);
decrypt(data, key);
printf("解密后原始数据:%u, %u\n", data[0], data[1]);
return 0;
}
XTEA
XTEA是TEA的扩展,也称做TEAN,它使用与TEA相同的简单运算,同样是一个64位块的Feistel密码,使用128位密钥,建议64轮, 但四个子密钥采取不正规的方式进行混合以阻止密钥表攻击;
#include <stdio.h>
#include <stdint.h>
//加密函数
void encrypt(unsigned int num_rounds, uint32_t* data, uint32_t* key){
uint32_t data0 = data[0], data1 = data[1];
uint32_t delta = 0x9e3779b9, i = 0;
uint32_t sum = 0;
for(i = 0; i < 32; i++){
data0 += (((data1<<4) ^ (data1>>5)) + data1) ^ (sum + key[sum&3]);
sum += delta;
data1 += (((data0<<4) ^ (data0>>5)) + data0) ^ (sum + key[(sum>>11)&3]);
}
data[0] = data0;
data[1] = data1;
}
//解密函数
void decrypt(unsigned int num_rounds, uint32_t* data, uint32_t* key){
uint32_t data0 = data[0], data1 = data[1];
uint32_t delta = 0x9e3779b9, i = 0;
uint32_t sum = 0;
sum = delta * num_rounds;
for(i = 0; i < 32; i++){
data1 -= (((data0<<4) ^ (data0>>5)) + data0) ^ (sum + key[(sum>>11)&3]);
sum -= delta;
data0 -= (((data1<<4) ^ (data1>>5)) + data1) ^ (sum + key[sum&3]);
}
data[0] = data0;
data[1] = data1;
}
int main(){
uint32_t data[2] = {200, 100};
uint32_t key[4] = {2, 3, 4, 5};
printf("加密前原始数据:%u, %u\n", data[0], data[1]);
encrypt(32, data, key);
printf("加密后数据:%u, %u\n", data[0], data[1]);
decrypt(32, data, key);
printf("解密后原始数据:%u, %u\n", data[0], data[1]);
return 0;
}
XXTEA
XXTEA是一个非平衡Feistel网络分组密码,在可变长度块上运行,这些块是32位大小的任意倍数(最小64位),使用128位密钥, 是目前TEA系列中最安全的算法,但性能较上两种有所降低。
#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 encrypt(int n, uint32_t* data, uint32_t* key){
uint32_t y, z, sum;
unsigned int p, rounds, e;
rounds = 6 + 52/n;
sum = 0;
z = data[n-1];
do{
sum += delta;
e = (sum>>2) & 3;
for(p = 0; p < n-1; p++){
y = data[p+1];
z = data[p] += MX;
}
y = data[0];
z = data[n-1] += MX;
}while(--rounds);
}
//解密算法
void decrypt(int n, uint32_t* data, uint32_t* key){
uint32_t y, z, sum;
unsigned int p, rounds, e;
rounds = 6 + 52/n;
sum = delta * rounds;
y = data[0];
do{
e = (sum>>2) & 3;
for(p = n-1; p > 0; p--){
z = data[p-1];
y = data[p] -= MX;
}
z = data[n-1];
y = data[0] -= MX;
sum -= delta;
}while(--rounds);
}
int main(){
uint32_t data[3] = {200, 100, 600};
uint32_t key[4] = {2, 3, 4, 5};
printf("加密前原始数据:%u, %u, %u\n", data[0], data[1], data[2]);
encrypt(3, data, key);
printf("加密后数据:%u, %u, %u\n", data[0], data[1], data[2]);
decrypt(3, data, key);
printf("解密后原始数据:%u, %u, %u\n", data[0], data[1], data[2]);
return 0;
}