Tencent QQ的加密算法就采用了
TEA算法
TEA(Tiny Encryption Algorithm) 是一种优秀的数据加密算法,虽然它比 DES(Data Encryption Standard) 要简单得多, 但有很强的抗差分分析能力,加密速度也比 DES 快得多,而且对 64 位数据加密的密钥长达 128 位,安全性相当好。
Pascal(Delphi)代码如下:
ntohl函数来自
winsock单元
type
TLongintArray = array[0..3] of Longword;
PLongintArray = ^TLongintArray;
TLongintArray = array[0..3] of Longword;
PLongintArray = ^TLongintArray;
procedure Encipher(v, k, w: PLongintArray);
var
y, z, a, b, c, d, sum, delta: Longword;
n: Longint;
begin
y := ntohl(v^[0]); z := ntohl(v^[1]); a := ntohl(k^[0]); b := ntohl(k^[1]);
var
y, z, a, b, c, d, sum, delta: Longword;
n: Longint;
begin
y := ntohl(v^[0]); z := ntohl(v^[1]); a := ntohl(k^[0]); b := ntohl(k^[1]);
c := ntohl(k^[2]); d := ntohl(k^[3]); sum := 0; delta := $9E3779B9;
for n := 1 to 16 do
begin
Inc(sum, delta);
Inc(y, ((z shl 4) + a) xor (z + sum) xor ((z shr 5) + b));
Inc(z, ((y shl 4) + c) xor (y + sum) xor ((y shr 5) + d));
end;
w^[0] := htonl(y);
w^[1] := htonl(z);
end;
for n := 1 to 16 do
begin
Inc(sum, delta);
Inc(y, ((z shl 4) + a) xor (z + sum) xor ((z shr 5) + b));
Inc(z, ((y shl 4) + c) xor (y + sum) xor ((y shr 5) + d));
end;
w^[0] := htonl(y);
w^[1] := htonl(z);
end;
procedure Decipher(v, k, w: PLongintArray);
var
y, z, a, b, c, d, sum, delta: Longword;
n: Longint;
begin
y := ntohl(v^[0]); z := ntohl(v^[1]); a := ntohl(k^[0]); b := ntohl(k^[1]);
c := ntohl(k^[2]); d := ntohl(k^[3]); sum := $E3779B90; delta := $9E3779B9;
for n := 1 to 16 do
begin
Dec(z, ((y shl 4) + c) xor (y + sum) xor ((y shr 5) + d));
Dec(y, ((z shl 4) + a) xor (z + sum) xor ((z shr 5) + b));
Dec(sum, delta);
end;
w^[0] := htonl(y);
w^[1] := htonl(z);
end;
var
y, z, a, b, c, d, sum, delta: Longword;
n: Longint;
begin
y := ntohl(v^[0]); z := ntohl(v^[1]); a := ntohl(k^[0]); b := ntohl(k^[1]);
c := ntohl(k^[2]); d := ntohl(k^[3]); sum := $E3779B90; delta := $9E3779B9;
for n := 1 to 16 do
begin
Dec(z, ((y shl 4) + c) xor (y + sum) xor ((y shr 5) + d));
Dec(y, ((z shl 4) + a) xor (z + sum) xor ((z shr 5) + b));
Dec(sum, delta);
end;
w^[0] := htonl(y);
w^[1] := htonl(z);
end;
C/C++代码如下:
转的地方没注作者
void encipher( unsigned long * const v, const unsigned long * const k )
{
register unsigned long y = v[0], z = v[1], sum = 0, delta = 0x9E3779B9,
a = k[0], b = k[1], c = k[2], d = k[3], n = 32;
{
register unsigned long y = v[0], z = v[1], sum = 0, delta = 0x9E3779B9,
a = k[0], b = k[1], c = k[2], d = k[3], n = 32;
while ( n-- > 0 )
{
sum += delta;
y += ( z << 4 ) + a ^ z + sum ^ ( z >> 5 ) + b;
z += ( y << 4 ) + c ^ y + sum ^ ( y >> 5 ) + d;
}
v[0] = y;
v[1] = z;
}
{
sum += delta;
y += ( z << 4 ) + a ^ z + sum ^ ( z >> 5 ) + b;
z += ( y << 4 ) + c ^ y + sum ^ ( y >> 5 ) + d;
}
v[0] = y;
v[1] = z;
}
void decipher( unsigned long * const v, const unsigned long * const k )
{
register unsigned long y = v[0], z = v[1], sum = 0xC6EF3720, delta = 0x9E3779B9,
a = k[0], b = k[1], c = k[2], d = k[3], n = 32;
{
register unsigned long y = v[0], z = v[1], sum = 0xC6EF3720, delta = 0x9E3779B9,
a = k[0], b = k[1], c = k[2], d = k[3], n = 32;
// sum = delta << 5, in general sum = delta * n
while ( n-- > 0 )
{
z -= ( y << 4 ) + c ^ y + sum ^ ( y >> 5 ) + d;
y -= ( z << 4 ) + a ^ z + sum ^ ( z >> 5 ) + b;
sum -= delta;
}
v[0] = y;
v[1] = z;
}
while ( n-- > 0 )
{
z -= ( y << 4 ) + c ^ y + sum ^ ( y >> 5 ) + d;
y -= ( z << 4 ) + a ^ z + sum ^ ( z >> 5 ) + b;
sum -= delta;
}
v[0] = y;
v[1] = z;
}
头次用MSN 空间见笑
,其实模拟QQ的程序我很早以前就写好了,没事弄弄MSN Spaces也没什么好发的就发发这个吧!Didi MSN:banzcn@hotmail.com EMAIL:banzcn@gmail.com!
首发于MSN Spaces
http://spaces.msn.com/banzcn/ 欢迎大家来看~ 呵~
下载单元
http://didi.99ss.com/AboutTencentQQ/UniTEA.pas
其它的等我慢满从MSN转过来吧!
Didi无聊第二篇(MSN空间,CSDN Blog)