From 719082920c8191f9f312889ab56e2c24b4068cd4 Mon Sep 17 00:00:00 2001 From: Joe DF Date: Thu, 12 Feb 2015 22:47:53 -0500 Subject: [PATCH] TEA -> xTEA #8 --- examples/{TEA.ahk => xTEA.ahk} | 0 src/TEA.ahk | 180 --------------------------------- src/xTEA.ahk | 70 +++++++++++++ 3 files changed, 70 insertions(+), 180 deletions(-) rename examples/{TEA.ahk => xTEA.ahk} (100%) delete mode 100644 src/TEA.ahk create mode 100644 src/xTEA.ahk diff --git a/examples/TEA.ahk b/examples/xTEA.ahk similarity index 100% rename from examples/TEA.ahk rename to examples/xTEA.ahk diff --git a/src/TEA.ahk b/src/TEA.ahk deleted file mode 100644 index ecdb24c..0000000 --- a/src/TEA.ahk +++ /dev/null @@ -1,180 +0,0 @@ -/* -AutoHotkey Version: 1.0.35+ -Language: English -Platform: Win2000/XP -Author: Laszlo Hars - -Plus-minus Counter Mode: stream cipher using add/subtract with reduced range - (32...126). Characters outside remain unchanged (TAB, CR, LF, ...). - Lines will remain of the same length. If it leaks information, pad! - -The underlying cipher is TEA, the Tiny Encryption Algorithm -http://www.simonshepherd.supanet.com/tea.htm It is one of the fastest and most -efficient cryptographic algorithms. It was developed by David Wheeler and Roger -Needham at the Computer Laboratory of Cambridge University. It is a Feistel -cipher, which uses operations from mixed (orthogonal) algebraic groups - XOR, -ADD and SHIFT in this case. It encrypts 64 data bits at a time using a 128-bit -key. It seems highly resistant to differential cryptanalysis, and achieves -complete diffusion (where a one bit difference in the plaintext will cause -approximately 32 bit differences in the ciphertext) after only six rounds. - -Version: 1.0 2005.07.08 First created -Version: 2.0 2015.02.12 Updated as functions by joedf for Libcrypt -*/ - -LC_TEA_Encrypt(Data,Keys:="") { - - ;Default Keys - static k1 := 0x11111111 ; 128-bit secret key - static k2 := 0x22222222 - static k3 := 0x33333333 ; choose wisely! - static k4 := 0x44444444 - - Loop 4 - { - if StrLen(k%A_Index%) - k%A_Index% := Keys[A_Index] - else - break - } - - bkpBL:=A_BatchLines,bkpSCS:=A_StringCaseSense,bkpAT:=A_AutoTrim,bkpFI:=A_FormatInteger - SetBatchLines -1 - StringCaseSense Off - AutoTrim Off - - k5:=SubStr(A_NowUTC,1,8) ; current time - v:=SubStr(A_NowUTC,-5) - v:=(v*1000)+A_MSec ; in MSec - SetFormat,Integer,H - TEA(k5,v,k1,k2,k3,k4) ; k5 = IV: starting random counter value - SetFormat,Integer,D - u:="0000000" . SubStr(k5,3) - IV:=SubStr(u,-7) ; 8-digit hex w/o 0x - - i := 9 ; pad-index, force restart - p := 0 ; counter to be encrypted - L := IV ; IV prepended to processed text - Loop % StrLen(Data) - { - i++ - if (i>8) { ; all 9 pad values exhausted - u := p - v := k5 ; IV - p++ ; increment counter - TEA(u,v,k1,k2,k3,k4) - s := Stream9(u,v) ; 9 pads from encrypted counter - i := 0 - } - a:=Asc(c:=SubStr(Data,A_Index,1)) - if a between 32 and 126 - { ; chars > 126 or < 31 unchanged - a += s[i] - IfGreater a, 126, SetEnv, a, % a-95 - c := Chr(a) - } - L .= c ; attach encrypted character - } - - SetBatchLines,%bkpBL% - StringCaseSense,%bkpSCS% - AutoTrim,%bkpAT% - SetFormat,Integer,%bkpFI% - - Return L -} - - -LC_TEA_Decrypt(Data,Keys:="") { - - ;Default Keys - static k1 := 0x11111111 ; 128-bit secret key - static k2 := 0x22222222 - static k3 := 0x33333333 ; choose wisely! - static k4 := 0x44444444 - - Loop 4 - { - if StrLen(k%A_Index%) - k%A_Index% := Keys[A_Index] - else - break - } - - bkpBL:=A_BatchLines,bkpSCS:=A_StringCaseSense,bkpAT:=A_AutoTrim - SetBatchLines -1 - StringCaseSense Off - AutoTrim Off - - p:=SubStr(Data,1,8) - If p is not xdigit ; if no IV: Error - { - ErrorLevel := 1 - Return - } - Data:=SubStr(Data,9) ; remove IV from text (no separator) - k5 := "0x" p ; set new IV - p := 0 ; counter to be encrypted - i := 9 ; pad-index, force restart - L := "" ; processed text - - Loop % StrLen(Data) - { - i++ - if (i,8) { ; all 9 pad values exhausted - u := p - v := k5 ; IV - p++ ; increment counter - TEA(u,v,k1,k2,k3,k4) - s := Stream9(u,v) ; 9 pads from encrypted counter - i := 0 - } - a:=Asc(c:=SubStr(Data,A_Index,1)) - if a between 32 and 126 - { ; chars > 126 or < 31 unchanged - a -= s[i] - IfLess a, 32, SetEnv, a, % a+95 - c := Chr(a) - } - L .= c ; attach encrypted character - } - - SetBatchLines,%bkpBL% - StringCaseSense,%bkpSCS% - AutoTrim,%bkpAT% - - Return L -} - -TEA(ByRef y,ByRef z,k0,k1,k2,k3) ; (y,z) = 64-bit I/0 block -{ ; (k0,k1,k2,k3) = 128-bit key - IntFormat := A_FormatInteger - SetFormat, Integer, D ; needed for decimal indices - s := 0 - d := 0x9E3779B9 - Loop 32 - { - k := "k" . s & 3 ; indexing the key - y := 0xFFFFFFFF & (y + ((z << 4 ^ z >> 5) + z ^ s + %k%)) - s := 0xFFFFFFFF & (s + d) ; simulate 32 bit operations - k := "k" . s >> 11 & 3 - z := 0xFFFFFFFF & (z + ((y << 4 ^ y >> 5) + y ^ s + %k%)) - } - SetFormat, Integer, %IntFormat% - y += 0 - z += 0 ; Convert to original ineger format -} - -Stream9(x,y) ; Convert 2 32-bit words to 9 pad values -{ ; 0 <= s0, s1, ... s8 <= 94 - s:=Object() - s[0] := Floor(x*0.000000022118911147) ; 95/2**32 - Loop 8 - { - z := (y << 25) + (x >> 7) & 0xFFFFFFFF - y := (x << 25) + (y >> 7) & 0xFFFFFFFF - x := z - s[A_Index] := Floor(x*0.000000022118911147) - } - return s -} \ No newline at end of file diff --git a/src/xTEA.ahk b/src/xTEA.ahk new file mode 100644 index 0000000..c0eef9d --- /dev/null +++ b/src/xTEA.ahk @@ -0,0 +1,70 @@ +; http://www.cix.co.uk/%7Eklockstone/xtea.pdf +LC_xTEA_Encrypt(Data,Keys:="") { + + ; Default Keys + static k := [ 0x11111111 ; 128-bit secret key + , 0x22222222 + , 0x33333333 ; choose wisely! + , 0x44444444 ] + + Loop 4 + { + if StrLen(k[A_Index]) + k[A_Index] := Keys[A_Index] + else + break + } + + bkpBL:=A_BatchLines,bkpSCS:=A_StringCaseSense,bkpAT:=A_AutoTrim,bkpFI:=A_FormatInteger + SetBatchLines -1 + StringCaseSense Off + AutoTrim Off + + k5:=SubStr(A_NowUTC,1,8) ; current time + v:=SubStr(A_NowUTC,-5) + v:=LC_Hex2Dec((v*1000)+A_MSec) ; in MSec + + + ; Implementation + + + SetBatchLines,%bkpBL% + StringCaseSense,%bkpSCS% + AutoTrim,%bkpAT% + SetFormat,Integer,%bkpFI% + + Return L +} + + +LC_xTEA_Decrypt(Data,Keys:="") { + + ; Default Keys + static k := [ 0x11111111 ; 128-bit secret key + , 0x22222222 + , 0x33333333 ; choose wisely! + , 0x44444444 ] + + Loop 4 + { + if StrLen(k[A_Index]) + k[A_Index] := Keys[A_Index] + else + break + } + + bkpBL:=A_BatchLines,bkpSCS:=A_StringCaseSense,bkpAT:=A_AutoTrim + SetBatchLines -1 + StringCaseSense Off + AutoTrim Off + + + ; Implementation + + + SetBatchLines,%bkpBL% + StringCaseSense,%bkpSCS% + AutoTrim,%bkpAT% + + Return L +} \ No newline at end of file