c - How is sum constant in TEA calculated
问题描述
I'm taking a look at TEA algorithm on encryption. I have one problem understanding implementation from here:
https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm
#include <stdint.h>
void encrypt (uint32_t v[2], uint32_t k[4]) {
uint32_t v0=v[0], v1=v[1], sum=0, i; /* set up */
uint32_t delta=0x9E3779B9; /* a key schedule constant */
uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
for (i=0; i<32; i++) { /* basic cycle start */
sum += delta;
v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
} /* end cycle */
v[0]=v0; v[1]=v1;
}
void decrypt (uint32_t v[2], uint32_t k[4]) {
uint32_t v0=v[0], v1=v[1], sum=0xC6EF3720, i; /* set up; sum is 32*delta */
uint32_t delta=0x9E3779B9; /* a key schedule constant */
uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
for (i=0; i<32; i++) { /* basic cycle start */
v1 -= ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3);
v0 -= ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1);
sum -= delta;
} /* end cycle */
v[0]=v0; v[1]=v1;
}
In decrypt()
function it clearly says sum
is 32 times delta
, 32 being number of rounds, however... 32(0x20) times 2654435769(0x9E3779B9) is 84941944608(0x13C6EF3720), not 3337565984(0xC6EF3720)!
What am I failing to grasp here? The algorithm is correct and I tested it myself.
Thanks for any help in advance,
Cheers
解决方案
sum
是总和的低 32 位。TEA 在 32 位空间中执行。
推荐阅读
- function - Haskell 中的函数 - 理解
- ios - Swift 结合递归重试
- json - ADO:通过 HTTP REST 请求将附件上传到 Azure DevOps
- java - 手动创建 LocalContainerEntityManagerFactoryBean 时不考虑 HibernateProperties
- firebird - 在 Firebird DB 和 Python 中插入重复行
- python - 使用 seaborn 获得绘图 - matplotlib
- javascript - 使用护照js res.redirection时何时将会话设置为false不起作用
- azure-web-app-service - 容器 Test1 没有响应端口:80 上的 HTTP ping,站点启动失败问题 - Azure Linux Web 服务
- r - 分割向量,按正则表达式分类
- tkinter - 为什么我的进度条没有出现在 tkinter