Laszlo wrote:
Quote:
The above code worked for me, perfectly. I chose the script.ahk file, and after encryption and decryption the script.dec file was what I started with. The only problem is that the output file was not cleared (deleted) before new data was written to it, so the next time you end up with the results of the last run remaining before the new results.
Hey Laszlo, I thought I fixed it, but the whole time I wasn't actually encrypting it, so I am back to square one. I basically want to keep adding to the encrypted file over and over and then at my choosing decrypt the whole thing in the end to the .dec file. As you said before it works perfectly the first time, just the second and so forth. it doesn't. What output file should I delete. Will this let me keep adding to the test.enc file.
Sorry for the confusion.
I appreciate any help with this. Thank you.
With the following example I encrypted it 3 times in a row. Here is the data and results.
Test file:
Code:
this is supposed to work
but it doesn't
so I will keep trying
-Blank Line / Carriage return-
When encrypted 3 times with my code it turns into:
Code:
JLl/,z4M^nfq>%a.a{jXN^Z(
`I]M-UTb"EHU5F
h1I:*l/!6}w%JfrI/yw9'
JLl/,z4M^nfq>%a.a{jXN^Z(
`I]M-UTb"EHU5F
h1I:*l/!6}w%JfrI/yw9'
JLl/,z4M^nfq>%a.a{jXN^Z(
`I]M-UTb"EHU5F
h1I:*l/!6}w%JfrI/yw9'
-Blank Line / Carriage return-
Then when I decrypt it I get:
Code:
this is supposed to work
but it doesn't
so I will keep trying
>48Rdtl(z2o!&u{O&hPS0urk
2}Y4DR2HXOl{XS
Dv@ HTOiw)(Y%/6EyVDi'
~dm*b#;CN-)N16NEBuE19H/p
Q]Q]NqXUJ/k\~
/@Di/nxPni%@!^dXgg/ot
-Blank Line / Carriage return-
My previous script I posted:
Code:
SetBatchLines -1
StringCaseSense Off
AutoTrim Off
pwd = test
PWD2Key(pwd, k1,k2,k3,k4,k5)
Gui, Add, Button, x10 y140 w100 h30 gENC, Encrypt
Gui, Add, Button, x210 y140 w100 h30 gDEC, Decrypt
Gui, Show, Center, Encoder-Decoder
Return
enc: ;--------------------
InputBox InpFile, Input, Enter the input file,, 400, 160,,,,,%A_ScriptDir%\test.txt
IfNotEqual ErrorLevel,0, ExitApp
StringTrimRight File, InpFile, 4
InputBox EncFile, Encrypted file, Enter a name for the encrypted file,, 400, 160,,,,,%File%.enc
IfNotEqual ErrorLevel,0, ExitApp
Gosub, GuiClose
Gosub, run
return
dec: ;--------------------
InputBox EncFile, Encrypted file, Enter the file to be decrypted,, 400, 160,,,,,%A_ScriptDir%\test.enc
IfNotEqual ErrorLevel,0, ExitApp
InputBox DecFile, Decrypted file, Enter a name for the decrypted file,, 400, 160,,,,,%A_ScriptDir%\test.dec
IfNotEqual ErrorLevel,0, ExitApp
Gosub, Guiclose
Gosub, run
return
GuiEscape:
ButtonDone:
GuiClose:
Gui,destroy
return
run:
ENCRYPT(EncFile,InpFile,k1,k2,k3,k4,k5)
DECRYPT(DecFile,EncFile,k1,k2,k3,k4,k5)
;ExitApp
PWD2Key(PW, ByRef k1, ByRef k2, ByRef k3, ByRef k4, ByRef k5)
{
CBC(k1,k2, PW, 1,2,3,4)
CBC(k3,k4, PW, 4,3,2,1)
k5 := k1^k2^k3^k4
}
CBC(ByRef u, ByRef v, x, k0,k1,k2,k3)
{
u = 0
v = 0
Loop % Ceil(StrLen(x)/8)
{
p = 0
StringLeft c, x, 4
Loop Parse, c
p := (p<<8) + Asc(A_LoopField)
u := u ^ p
p = 0
StringMid c, x, 5, 4
Loop Parse, c
p := (p<<8) + Asc(A_LoopField)
v := v ^ p
TEA(u,v,k0,k1,k2,k3)
StringTrimLeft x, x, 8
}
}
ENCRYPT(EncFile,InpFile,k1=0,k2=0,k3=0,k4=0,k5=0) ; encrypt InpFile to EncFile with key k1..k5
{
i = 9 ; pad-index, force restart
p = 0 ; counter to be encrypted
Loop Read, %InpFile%, %EncFile%
{
L = ; processed line
Loop % StrLen(A_LoopReadLine)
{
i++
IfGreater i,8, { ; all 9 pad values exhausted
u := p
v := k5 ; another secret
p++ ; increment counter
TEA(u,v, k1,k2,k3,k4)
Stream9(u,v) ; 9 pads from encrypted counter
i = 0
}
StringMid c, A_LoopReadLine, A_Index, 1
a := Asc(c)
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 = %L%%c% ; attach encrypted character
}
FileAppend %L%`n
}
}
DECRYPT(DecFile,EncFile,k1=0,k2=0,k3=0,k4=0,k5=0) ; decrypt EncFile to DecFile with key k1..k5
{
FileDelete %DecFile%
i = 9 ; pad-index, force restart
p = 0 ; counter to be encrypted
Loop Read, %EncFile%, %DecFile%
{
L = ; processed line
Loop % StrLen(A_LoopReadLine)
{
i++
IfGreater i,8, { ; all 9 pad values exhausted
u := p
v := k5 ; another secret
p++ ; increment counter
TEA(u,v, k1,k2,k3,k4)
Stream9(u,v) ; 9 pads from encrypted counter
i = 0
}
StringMid c, A_LoopReadLine, A_Index, 1
a := Asc(c)
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 = %L%%c% ; attach encrypted character
}
FileAppend %L%`n
}
}
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
Local z ; makes all s%i% global
s0 := 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)
}
}