簡單有效的源碼加密算法-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:  andot@coolcode.cn

*************************************************************************/

#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:  andot@coolcode.cn

*************************************************************************/
#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;
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM