简单有效的源码加密算法-TEA和XTEA算法
Posted 371502685
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单有效的源码加密算法-TEA和XTEA算法相关的知识,希望对你有一定的参考价值。
最近在项目中用到了XTEA源码加密算法,写下来总结一下:
TEA(Tiny Encryption Algorithm)是一种小型的对称加密解密算法,支持128位密码,与BlowFish一样TEA每次只能加密/解密8字节数据。TEA特点是速度 快、效率高,实现也非常简单。由于针对TEA的攻击不断出现,所以TEA也发展出几个版本,分别是XTEA、Block TEA和XXTEA。
TEA算法实现非常简单,不到20代码,分享一个加强版的TEA算法XTEA算法实现:
/************************************************************************
Copyright 2006-2007 Ma Bingyao
These sources is free software. Redistributions of source code must
retain the above copyright notice. Redistributions in binary form
must reproduce the above copyright notice. You can redistribute it
freely. You can use it with any free or commercial software.
These sources is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. Without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You may contact the author by:
e-mail: [email protected]
*************************************************************************/
#ifndef XXTEA_H
#define XXTEA_H
#include <stddef.h> /** for size_t & NULL declarations */
#if defined(_MSC_VER)
typedef unsigned __int32 xxtea_long;
#else
#if defined(__FreeBSD__) && __FreeBSD__ < 5
/** FreeBSD 4 doesn’t have stdint.h file */
#include <inttypes.h>
#else
#include <stdint.h>
#endif
typedef uint32_t xxtea_long;
#endif /** end of if defined(_MSC_VER) */
#define XXTEA_MX (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z)
#define XXTEA_DELTA 0x9e3779b9
void xxtea_long_encrypt(xxtea_long *v, xxtea_long len, xxtea_long *k);
void xxtea_long_decrypt(xxtea_long *v, xxtea_long len, xxtea_long *k);
#endif
/************************************************************************
Copyright 2006-2007 Ma Bingyao
These sources is free software. Redistributions of source code must
retain the above copyright notice. Redistributions in binary form
must reproduce the above copyright notice. You can redistribute it
freely. You can use it with any free or commercial software.
These sources is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY. Without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
You may contact the author by:
e-mail: [email protected]
*************************************************************************/
#include “xxtea.h”
void xxtea_long_encrypt(xxtea_long *v, xxtea_long len, xxtea_long *k) {
xxtea_long n = len – 1;
xxtea_long z = v[n], y = v[0], p, q = 6 + 52 / (n + 1), sum = 0, e;
if (n < 1) {
return;
}
while (0 < q–) {
sum += XXTEA_DELTA;
e = sum >> 2 & 3;
for (p = 0; p < n; p++) {
y = v[p + 1];
z = v[p] += XXTEA_MX;
}
y = v[0];
z = v[n] += XXTEA_MX;
}
}
void xxtea_long_decrypt(xxtea_long *v, xxtea_long len, xxtea_long *k) {
xxtea_long n = len – 1;
xxtea_long z = v[n], y = v[0], p, q = 6 + 52 / (n + 1), sum = q * XXTEA_DELTA, e;
if (n < 1) {
return;
}
while (sum != 0) {
e = sum >> 2 & 3;
for (p = n; p > 0; p–) {
z = v[p - 1];
y = v[p] -= XXTEA_MX;
}
z = v[n];
y = v[0] -= XXTEA_MX;
sum -= XXTEA_DELTA;
}
}
以上是关于简单有效的源码加密算法-TEA和XTEA算法的主要内容,如果未能解决你的问题,请参考以下文章