您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

TEA XTEA XXTEA

TEA、XTEA、XXTEA

一:TEA

简介:一种分组加密算法,TEA算法使用64位的明文分组和128位的密钥,使用一个神秘常数作为倍数(也可更改)

下面两段只是加解密的基本流程,具体的还要考虑寄存器溢出的问题(&0xffffffff),SYC二面

关键加密函数
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;
}

关键解密函数
void DecryptTEA(unsigned int *firstChunk, unsigned int secondChunk, unsigned intk 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;

}

SYC二面flag2WP:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    //异或后我们在内存中得到的数据dafbde108b5962f40e786cf1c994830d29bc0a29fea23e0bdc87ea0513c7350d
    unsigned int array[] = {0x10defbda,0xf462598b,0xf16c780e,0x0d8394c9,0x290abc29,0x0b3ea2fe,0x05ea87dc,0x0d35c713};//转化为内存小端储存,为位运算做准备//本身的值{0xdafbde10,0x8b5962f4,0x0e786cf1,0xc994830d,0x29bc0a29,0xfea23e0b,0xdc87ea05,0x13c7350d};
    unsigned int key[]= {0x6C637953,0x76656C6F,0xD754C061,0x023BA78B};//将key转化为dword型在内存中的储存(小端),为位运算做准备  //本身的值{0x5379636c,0x6f6c6576,0x61c054d7,0x8ba73b02};
    unsigned int sum=0;
    unsigned int delta = 0x6C7A6E62;//这个不用位运算,不用转化
    unsigned int x,y;
    int i,j,k;
    for(k=0;k<8;k+=2)
   {
    x = array[k];
    y = array[k+1];
    for(i=0,j=31;i<32;i++,j--)
    {
        sum = delta*(j+1)&0xffffffff;
        y = j^y;
        x = j^x;
        y -=  ((key[2] + (x<<2))&0xffffffff)^(((sum + x) ^ (key[3]+(x>>5)))&0xffffffff);
        x -=  (((key[0] + (y<<2))^(sum + y))&0xffffffff) ^ ((key[1]+(y>>5))&0xffffffff);
    }
    array[k] = x;
    array[k+1] = y;
   }
   for(k=0;k<8;k++)
   {
       printf("%x\n",array[k]);          //结果是内存中的小端形式,我们手动转化即可
   }
   //最后4个4个的手动返回来得到5379636c6f7665727b737433707343616e4265416363756d7531617465647e7d
    return 0;
}

二:XTEA

XTEA是TEA的升级版,增加了更多的密钥表,移位和异或操作等等,设计者是Roger Needham, David Wheeler

加密过程:

https://ewr1.vultrobjects.com/imgspice/000/002/001/186_8da_56c.jpg

算法实现:

示例代码:

也就加了一些内容,加解密过程基本没变。

#include <stdio.h>
#include <stdint.h>
/* take 64 bits of data in v[0] and v[1] and 128 bits of key[0] - key[3] */
void encipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) 
{
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], sum=0, delta=0x9E3779B9;
    for (i=0; i < num_rounds; i++) {
        v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
        sum += delta;
        v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
    }
    v[0]=v0; v[1]=v1;
}

void decipher(unsigned int num_rounds, uint32_t v[2], uint32_t const key[4]) {
    unsigned int i;
    uint32_t v0=v[0], v1=v[1], delta=0x9E3779B9, sum=delta*num_rounds;
    for (i=0; i < num_rounds; i++) {
        v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + key[(sum>>11) & 3]);
        sum -= delta;
        v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + key[sum & 3]);
    }
    v[0]=v0; v[1]=v1;
}

int main()
{
    uint32_t v[2]={1,2};
    uint32_t const k[4]={2,2,3,4};
    unsigned int r=32;//num_rounds建议取值为32// v为要加密的数据是两个32位无符号整数// k为加密解密密钥,为4个32位无符号整数,即密钥长度为128位printf("加密前原始数据:%u %u\n",v[0],v[1]);
    encipher(r, v, k);
    printf("加密后的数据:%u %u\n",v[0],v[1]);
    decipher(r, v, k);
    printf("解密后的数据:%u %u\n",v[0],v[1]);
    return 0;
}

三:XXTEA

XXTEA,又称Corrected Block TEA,是XTEA的升级版 ,设计者是Roger Needham, David Wheeler

特点归纳:

①:原字符串长度可以不是4的倍数了

加密过程:

https://ewr1.vultrobjects.com/imgspice/000/002/001/188_7ed_7ad.jpg

算法实现:

示例代码:

#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])   //v是要加密的两个元素的数组
{                                                      //n为数组的长度
    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];
                v[p] += MX;
			          z = v[p];      
						}
            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[2]= {1,2};
    uint32_t const k[4]= {2,2,3,4};
    int n= 2; //n的绝对值表示v的长度,取正表示加密,取负表示解密
	// v为要加密的数据是两个32位无符号整数
  // k为加密解密密钥,为4个32位无符号整数,即密钥长度为128位
    printf("加密前原始数据:%u %u\n",v[0],v[1]);
    btea(v, n, k);
    printf("加密后的数据:%u %u\n",v[0],v[1]);
    btea(v, -n, k);
    printf("解密后的数据:%u %u\n",v[0],v[1]);
    return 0;
}

附上例题:

2021hgame-alpacha

链接:https://pan.baidu.com/s/1H9jq_VoynN5_2rS3Kh205A
提取码:0syj
复制这段内容后打开百度网盘手机App,操作更方便哦–来自百度网盘超级会员V1的分享

#include <stdio.h>
#include <stdlib.h>
#define DELTA 0x9e3779b9//0x61C88647
int main()
{
    unsigned int v[] = {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};
    unsigned int key[] = {1,2,3,4};
    unsigned int sum = 0;
    unsigned int y,z,p,rounds,e;
    int n = 35;  //
    int i = 0;
    rounds = 6 + 52/n;
    y = v[0];
    sum = (rounds*DELTA)&0xffffffff;
     do                 //0x9E3779B9*(52/35)-0x4AB325AA,测试来要循环7次
     {
        e = sum >> 2 & 3;
        for(p=n-1;p>0;p--)    //34次循环
        {
            z = v[p-1];
            v[p] = (v[p] - ((((z>>5)^(y<<2))+((y>>3)^(z<<4))) ^ ((key[(p^e)&3]^z)+(y ^ sum)))) & 0xffffffff;
            y = v[p];
        }
        z = v[n-1];
        v[0] = (v[0] - (((key[(p^e)&3]^z)+(y ^ sum)) ^ (((y<<2)^(z>>5))+((z<<4)^(y>>3))))) & 0xffffffff;
        y = v[0];
        sum = (sum-DELTA)&0xffffffff;
     }while(--rounds);
    for(i=0;i<n;i++)
    {
        printf("%c",v[i]);
    }
    return 0;
}
//hgame{l00ks_1ike_y0u_f0Und_th3_t34}

分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进