簡單有效源碼密碼編譯演算法-TEA和XTEA演算法

來源:互聯網
上載者:User

標籤:對稱式加密   分享   useful   and   soft   支援   define   加密解密   項目   

最近在項目中用到了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演算法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.