本帖最后由 republican 于 2011-6-11 10:23 编辑
这个是C下的TEA算法:void encipher(unsigned long *const plain, const unsigned long *const key, unsigned long *const crypt)
//参数为8字节的明文输入和16字节的密钥,输出8字节密文
{
unsigned long left = plain[0],right = plain[1],
a = key[0], b = key[1],
c = key[2], d = key[3],
n = 32, sum = 0,
delta = 0x9E3779B9;
// 明文输入被分为左右两部分,密钥分为四部分存入寄存器,n表示加密轮数推荐32。Delta为一常数。
while (n-- > 0) {
sum += delta;
left += ((right << 4) + a) ^ (right + sum) ^ ((right >> 5) + b);
right += ((left << 4) + c) ^ (left + sum) ^ ((left >> 5) + d);
}
crypt[0] = left ;
crypt[1] = right ;
}
而我这样改写了(只进行16次迭代):
_Encipher('afadadadadadadad',5'ef6236e060963c24d2c8531b7ce177f')
Func _Encipher($Text,$EnKey)
Local $Left = Number('0x' & StringLeft($Text,8))
Local $Right = Number('0x' & StringMid($Text,9,8))
;~ _Save($Left,'$Left')
;~ _Save($Right,'$Right')
Local $Key[4] = [ Number('0x' & StringMid($EnKey,1,8)),Number('0x' & StringMid($EnKey,9,8)),Number('0x' & StringMid($EnKey,17,8)),Number('0x' & StringMid($EnKey,25,8))]
Local $PreLeft[4],$PreRight[4]
Local $Sum = 0
Local $delta = 0x9E3779B9
For $i = 1 To 16
$Sum += $delta
$PreLeft[0] = BitShift($Right,-4)+$Key[0]
$PreLeft[1] = $Right + $Sum
$PreLeft[2] = BitShift($Right,5)+$Key[1]
$PreLeft[3] =BitXOR($PreLeft[0],$PreLeft[1],$PreLeft[2])
$Left += $PreLeft[3]
$PreRight[0] = BitShift($Left,-4)+$Key[2]
$PreRight[1] = $Left + $Sum
$PreRight[2] = BitShift($Left,5)+$Key[3]
$PreRight[3] =BitXOR($PreRight[0],$PreRight[1],$PreRight[2])
$Right += $PreRight[3]
Next
_Save($Sum,'$Sum')
;~ _Save($PreLeft[0],'$PreLeft[0]')
;~ _Save($PreLeft[1],'$PreLeft[1]')
;~ _Save($PreLeft[2],'$PreLeft[2]')
;~ _Save($PreLeft[3],'$PreLeft[3]')
_Save($Left,'$Left')
;~ _Save($PreRight[0],'$PreRight[0]')
;~ _Save($PreRight[1],'$PreRight[1]')
;~ _Save($PreRight[2],'$PreRight[2]')
;~ _Save($PreRight[3],'$PreRight[3]')
_Save($Right,'$Right')
Return ;$Left,$Right
EndFunc
Func _Save($Text,$Target = '',$Mode = 0)
If $Target <> '' Then ConsoleWrite('变量: '&$Target&@CRLF)
ConsoleWrite('字符:'&IsString($Text)&@CRLF)
ConsoleWrite('数字:'&IsNumber($Text)&@CRLF)
ConsoleWrite('字节:'&IsBinary($Text)&@CRLF)
ConsoleWrite('值:'&$Text&@CRLF)
ConsoleWrite('Hex:'&Hex($Text)&@CRLF)
ConsoleWrite('Byte:'&Binary($Text)&@CRLF)
ConsoleWrite('------'&@CRLF)
EndFunc
对于明文:afadadadadadadad ,密匙: ef6236e060963c24d2c8531b7ce177f,上述代码给出来的结果为 1E8A7F519F3D550B ,但教程中的结果为 D781D20379F279E0 ,不知道这样的改写是否有错误呢? 先谢谢各位了!
PS:现在发现Autoit在4G内存03上调试非常不稳定,直接运行十几次就会蓝屏..呵呵..08 R2反而很稳定。 |