#include "tea.h"
#include <cstring> //for memcpy,memset
using namespace std;
TEA::TEA(const byte *key, int round /*= 32*/, bool isNetByte /*= false*/)
:_round(round)
,_isNetByte(isNetByte) {
if (key != 0)
memcpy(_key, key, 16);
else
memset(_key, 0, 16);
}
TEA::TEA(const TEA &rhs)
:_round(rhs._round)
,_isNetByte(rhs._isNetByte) {
memcpy(_key, rhs._key, 16);
}
TEA& TEA::operator=(const TEA &rhs) {
if (&rhs != this) {
_round = rhs._round;
_isNetByte = rhs._isNetByte;
memcpy(_key, rhs._key, 16);
}
return *this;
}
void TEA::encrypt(const byte *in, byte *out) {
encrypt((const ulong*)in, (ulong*)out);
}
void TEA::decrypt(const byte *in, byte *out) {
decrypt((const ulong*)in, (ulong*)out);
}
void TEA::encrypt(const ulong *in, ulong *out) {
ulong *k = (ulong*)_key;
register ulong y = ntoh(in[0]);
register ulong z = ntoh(in[1]);
register ulong a = ntoh(k[0]);
register ulong b = ntoh(k[1]);
register ulong c = ntoh(k[2]);
register ulong d = ntoh(k[3]);
register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */
register int round = _round;
register ulong sum = 0;
while (round--) { /* basic cycle start */
sum += delta;
y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
} /* end cycle */
out[0] = ntoh(y);
out[1] = ntoh(z);
}
void TEA::decrypt(const ulong *in, ulong *out) {
ulong *k = (ulong*)_key;
register ulong y = ntoh(in[0]);
register ulong z = ntoh(in[1]);
register ulong a = ntoh(k[0]);
register ulong b = ntoh(k[1]);
register ulong c = ntoh(k[2]);
register ulong d = ntoh(k[3]);
register ulong delta = 0x9E3779B9; /* (sqrt(5)-1)/2*2^32 */
register int round = _round;
register ulong sum = 0;
if (round == 32)
sum = 0xC6EF3720; /* delta << 5*/
else if (round == 16)
sum = 0xE3779B90; /* delta << 4*/
else
sum = delta << static_cast<int>(logbase(2, round));
while (round--) { /* basic cycle start */
z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d);
y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b);
sum -= delta;
} /* end cycle */
out[0] = ntoh(y);
out[1] = ntoh(z);
}
tea_test.rar_tea_tea_test_加密 解密_加密解密_字符串 加密 解密
版权申诉
30 浏览量
2022-09-24
13:25:49
上传
评论
收藏 5KB RAR 举报
JonSco
- 粉丝: 67
- 资源: 1万+
最新资源
- 基于c++的个人收支管理系统设计
- docker一键安装包
- Screenshot_20240430_144340_com.ss.android.ugc.live.jpg
- 回到山沟沟.mp3
- 基于matlab实现自适应波束形成RLS及LMS算法仿真源程序1.rar
- 基于matlab实现自己编写的基于卡尔曼滤波的利用加速度传感器的计步器,测试数据是传感器放在腰部和手臂 .rar
- 基于matlab实现阵列信号处理,波束形成.rar
- 111111111111111111
- 基于matlab实现计步器编程;对当前的计步器装置的数值算法模拟 .rar
- Mdb学习查看PW;access;mdb;pw;password;patch
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈