概述
TEA算法
1.加密核心函数
void EncryptTEA(unsigned int *firstChunk, unsigned int *secondChunk, unsigned int* key)
{
unsigned int y = *firstChunk;
unsigned int z = *secondChunk;
unsigned int sum = 0;
unsigned int delta = 0x9e3779b9;
for (int i = 0; i < 8; i++)//8轮运算(需要对应下面的解密核心函数的轮数一样)
{
sum += delta;
y += ((z << 4) + key[0]) ^ (z + sum) ^ ((z >> 5) + key[1]);
z += ((y << 4) + key[2]) ^ (y + sum) ^ ((y >> 5) + key[3]);
}
*firstChunk = y;
*secondChunk = z;
}
2.解密核心函数
注意项:sum的初值不一定是0,根据加密函数写出
void DecryptTEA(unsigned int *firstChunk, unsigned int *secondChunk, unsigned int* key)
{
unsigned int sum = 0;
unsigned int y = *firstChunk;
unsigned int z = *secondChunk;
unsigned int delta = 0x9e3779b9;
sum = delta << 3; //32轮运算,所以是2的5次方;16轮运算,所以是2的4次方;8轮运算,所以是2的3次方
for (int i = 0; i < 8; i++) //8轮运算
{
z -= (y << 4) + key[2] ^ y + sum ^ (y >> 5) + key[3];
y -= (z << 4) + key[0] ^ z + sum ^ (z >> 5) + key[1];
sum -= delta;
}
*firstChunk = y;
*secondChunk = z;
}
XXTEA算法
HGAME2021_weak1_apacha
#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) /* 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()
{
uint32_t v[35] = {0xe74eb323,0xb7a72836,0x59ca6fe2,0x967cc5c1,0xe7802674,0x3d2d54e6,0x8a9d0356,0x99dcc39c,0x7026d8ed,0x6a33fdad,0xf496550a,0x5c9c6f9e,0x1be5d04c,0x6723ae17,0x5270a5c2,0xac42130a,0x84be67b2,0x705cc779,0x5c513d98,0xfb36da2d,0x22179645,0x5ce3529d,0xd189e1fb,0xe85bd489,0x73c8d11f,0x54b5c196,0xb67cb490,0x2117e4ca,0x9de3f994,0x2f5aa1aa,0xa7e801fd,0xc30d6eab,0x1baddc9c,0x3453b04a,0x92a406f9};
uint32_t const k[4] = {1,2,3,4};
int n = -35; //n的绝对值表示v的长度,取正表示加密,取负表示解密
// v为要加密的数据是两个32位无符号整数
// k为加密解密密钥,为4个32位无符号整数,即密钥长度为128位;
btea(v, n, k);
for (int i = 0; i < 35; i++)
{
printf("%c", (char)v[i]);
}
return 0;
}
//hgame{l00ks_1ike_y0u_f0Und_th3_t34}
最后
以上就是魁梧皮卡丘为你收集整理的TEA算法的全部内容,希望文章能够帮你解决TEA算法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复