首页
社区
课程
招聘
[分享]TEA、XTEA、XXTEA加解密
发表于: 2021-9-16 17:28 25506

[分享]TEA、XTEA、XXTEA加解密

2021-9-16 17:28
25506

目录

简介

TEA的分组长度为64位,密钥长度为128位,采用Feistel网络,建议32次循环加密即64轮

 

XTEA是TEA的扩展,同样是一个64位块的Feistel密码,使用128位密钥,建议64轮

 

XXTEA是一个非平衡Feistel网络分组密码,在可变长度块上运行,这些块是32位大小的任意倍数(最小64位),使用128位密钥

符号介绍

  • 按位异或,⊕
  • 模加,⊞

TEA加密

图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
void encrypt(unsigned int* v, unsigned int* key) {
  unsigned int l = v[0], r = v[1], sum = 0, delta = 0x9e3779b9;
  for (size_t i = 0; i < 32; i++) {
    sum += delta;
    l += ((r << 4) + key[0]) ^ (r + sum) ^ ((r >> 5) + key[1]);
    r += ((l << 4) + key[2]) ^ (l + sum) ^ ((l >> 5) + key[3]);
  }
  v[0] = l;
  v[1] = r;
}
 
void decrypt(unsigned int* v, unsigned int* key) {
  unsigned int l = v[0], r = v[1], sum = 0, delta = 0x9e3779b9;
  sum = delta *32;
  for (size_t i = 0; i < 32; i++) {
    r -= ((l << 4) + key[2]) ^ (l + sum) ^ ((l >> 5) + key[3]);
    l -= ((r << 4) + key[0]) ^ (r + sum) ^ ((r >> 5) + key[1]);
    sum -= delta;
  }
  v[0] = l;
  v[1] = r;
}
 
int main(int argc, char const *argv[])
{
    //test
    unsigned int v[2]={1,2},key[4]={1,2,3,4};
    printf("%u,%u\n",v[0],v[1]);
    encrypt(v,key);
    printf("%u,%u\n",v[0],v[1]);
    decrypt(v,key);
    printf("%u,%u\n",v[0],v[1]);
    return 0;
}

XTEA加密

图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
void encrypt(unsigned int* v, unsigned int* key) {
  unsigned int l = v[0], r = v[1], sum = 0, delta = 0x9e3779b9;
  for (size_t i = 0; i < 32; i++) {
    l += (((r << 4) ^ (r >> 5)) + r) ^ (sum + key[sum & 3]);
    sum += delta;
    r += (((l << 4) ^ (l >> 5)) + l) ^ (sum + key[(sum >> 11) & 3]);
  }
  v[0] = l;
  v[1] = r;
}
 
void decrypt(unsigned int* v, unsigned int* key) {
  unsigned int l = v[0], r = v[1], sum = 0, delta = 0x9e3779b9;
  sum = delta * 32;
  for (size_t i = 0; i < 32; i++) {
    r -= (((l << 4) ^ (l >> 5)) + l) ^ (sum + key[(sum >> 11) & 3]);
    sum -= delta;
    l -= (((r << 4) ^ (r >> 5)) + r) ^ (sum + key[sum & 3]);
  }
  v[0] = l;
  v[1] = r;
}
 
int main(int argc, char const *argv[])
{
    //test
    unsigned int v[2]={1,2},key[4]={1,2,3,4};
    printf("%u,%u\n",v[0],v[1]);
    encrypt(v,key);
    printf("%u,%u\n",v[0],v[1]);
    decrypt(v,key);
    printf("%u,%u\n",v[0],v[1]);
    return 0;
}

XXTEA加密

图片描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#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[2]= {1,2}; 
    uint32_t const k[4]= {2,2,3,4}; 
    int n= 2; //n的绝对值表示v的长度,取正表示加密,取负表示解密 
    // v为要加密的数据是两个32位无符号整数 
    // k为加密解密密钥,为432位无符号整数,即密钥长度为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
}

逆向特征

TEA系列算法典型特征是采用密钥调度常数0x9e3779b9

参考

《加密与解密》
维基百科


[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 2
支持
分享
最新回复 (3)
雪    币: 5229
活跃值: (9798)
能力值: ( LV9,RANK:181 )
在线值:
发帖
回帖
粉丝
2

https://blog.csdn.net/gsls200808/article/details/48243019

https://blog.csdn.net/m0_51713041/article/details/115535776

最后于 2021-9-16 17:39 被nevinhappy编辑 ,原因:
2021-9-16 17:38
0
雪    币: 4453
活跃值: (6273)
能力值: ( LV6,RANK:90 )
在线值:
发帖
回帖
粉丝
3
nevinhappy https://blog.csdn.net/gsls200808/article/details/48243019https://blog.csdn.net/m0_51713041/article/d ...
谢谢大佬补充
2021-9-17 13:45
0
雪    币: 62
活跃值: (2052)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4

TEA系列算法典型特征是采用密钥调度常数0x9e3779b9

请问楼主:

有加密函数,解密函数,知道明文,密文,可不可求出KEY

2022-1-23 17:09
0
游客
登录 | 注册 方可回帖
返回
//