首页
论坛
课程
招聘
[分享]TEA、XTEA、XXTEA加解密
2021-9-16 17:28 5321

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

2021-9-16 17:28
5321

目录

简介

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

参考

《加密与解密》
维基百科


【公告】欢迎大家踊跃尝试高研班11月试题,挑战自己的极限!

收藏
点赞0
打赏
分享
最新回复 (2)
雪    币: 2988
活跃值: 活跃值 (2748)
能力值: ( LV9,RANK:181 )
在线值:
发帖
回帖
粉丝
nevinhappy 活跃值 2 2021-9-16 17:38
2
0

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

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

最后于 2021-9-16 17:39 被nevinhappy编辑 ,原因:
雪    币: 1471
活跃值: 活跃值 (2043)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
Max_hhg 活跃值 2021-9-17 13:45
3
0
nevinhappy https://blog.csdn.net/gsls200808/article/details/48243019https://blog.csdn.net/m0_51713041/article/d ...
谢谢大佬补充
游客
登录 | 注册 方可回帖
返回