AutoHotkey Community

It is currently May 27th, 2012, 5:28 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 60 posts ]  Go to page Previous  1, 2, 3, 4  Next
Author Message
 Post subject:
PostPosted: March 1st, 2010, 6:30 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 2nd, 2010, 3:40 am 
Offline

Joined: November 16th, 2007, 2:17 am
Posts: 231
Thanks Laszlo, I managed to get it to work.

I had one last question. In your original script if my input file for encrption was:

Quote:
This is a test


it would be decrypted too:

Quote:
This is a test


but in the altered code I provided it decrypts it too:

Quote:
Thisisatest


Any ideas why it is deleting the spaces? Thanks Laszlo.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 2nd, 2010, 6:25 pm 
Offline

Joined: May 12th, 2005, 8:20 am
Posts: 331
Location: Münster, Germany
HI, newpie,
possibly you've lost the "Autotrim, Off" while altering the code?
Sounds like!
Regards,
Klaus


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 3rd, 2010, 3:13 am 
Offline

Joined: November 16th, 2007, 2:17 am
Posts: 231
Thanks Klaus,

I put the AutoTrim at the beginning after the Hotkey, but I guess it didn't stick after the Gui. I inserted it after the labels and it works great. Thanks for the observation and help.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 3rd, 2010, 11:23 am 
Offline

Joined: May 12th, 2005, 8:20 am
Posts: 331
Location: Münster, Germany
Hi, Community,
I was glad to find Laszlo's encrypt-/decrypt-module and I directly startet to attach it to a password application, but a problem came around:

At run time, the passwords and some other attributes reside in a listview. At programme's end I collect these listview entries like this
Code:
Loop, % LV_GetCount()
{
LV_GetText ...
contents=%contents%%data%`n
}
At the end I wrote "contents" to a file.
At the next programme start I read the whole file into a variable and call "decrypt". At return from that module, only the first set is decrypted, the rest stayed crypted.
Watch out that I separated the data sets from the list view only by a `n.

When I changed the separation of the single data sets from `n to `r `n, everything works fine although the hex presentation of the written file does not show any difference.
I can't really explain what's happening!
Can someone contribute anything to this problem?
Regards,
Klaus


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 3rd, 2010, 5:39 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
The script reads lines from the input file, en/decrypts them, and writes lines to the output file, always separated by only " `n " characters. If the original file had " `r`n " newline sequences, this behavior changes them. If it is undesirable, you can change the
Code:
FileAppend %L%`n
lines to
Code:
FileAppend %L%`r`n
or determine the original newline sequences and copy them to the output file.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 11th, 2010, 5:20 am 
Offline

Joined: November 16th, 2007, 2:17 am
Posts: 231
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)
   }
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 11th, 2010, 6:15 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
This is normal with CBC (Cipher Block Chaining) and counter mode encryption (used here): the encryption depends on either the previous data (CBC) or the position of the data (counter mode). If you concatenate two encrypted files, at decryption the second one will have different preceding data, or different positions of the characters, thus you get gibberish.

You could use start-of-data markers in the file, and restart decrypting after them, but it is not secure: the xor of the ciphertext characters in the same position is the same as the xor of the corresponding plaintext (original) characters. Therefore, after the start-of-data marker a random (or sequentially increasing) initial counter value has to be explicitly written, and used at both encrypting and decrypting. This initial counter value need not be encrypted, but should not ever repeat.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 12th, 2010, 2:07 am 
Offline

Joined: January 2nd, 2010, 6:13 pm
Posts: 105
A big thank you to Laszlo and Icarus for such great contributions. It's very very useful. :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 12th, 2010, 3:18 am 
Offline

Joined: November 16th, 2007, 2:17 am
Posts: 231
Laszlo wrote:
Quote:
This is normal with CBC (Cipher Block Chaining) and counter mode encryption (used here): the encryption depends on either the previous data (CBC) or the position of the data (counter mode). If you concatenate two encrypted files, at decryption the second one will have different preceding data, or different positions of the characters, thus you get gibberish.

You could use start-of-data markers in the file, and restart decrypting after them, but it is not secure: the xor of the ciphertext characters in the same position is the same as the xor of the corresponding plaintext (original) characters. Therefore, after the start-of-data marker a random (or sequentially increasing) initial counter value has to be explicitly written, and used at both encrypting and decrypting. This initial counter value need not be encrypted, but should not ever repeat.



Thank you very much for explaining it Laszlo. I sort of get what you are talking about, but have no idea on how to change the encrypting and decrypting part to achieve it in my original script. That is ok, I still think the script is useful and will find something to use it on where I can only encrypt and decrypt once.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 13th, 2010, 7:35 pm 
Offline

Joined: January 2nd, 2010, 6:13 pm
Posts: 105
Is Tiny Encryption Algorithm faster than RC4 encryption to hex stream?

I'm using both algorithms as functions in #Include files and both work fine. RC4txt2hex gives a bit longer output but that is not really an issue. However, I'm very curious about speed.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 13th, 2010, 8:12 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
RC4 is faster if the tables are left in the cache, which is normally the case with modern CPU's. xTEA is more secure. If you are after speed, use machine code or the Windows crypto API. The later is also more secure against simple debugging attempts.

If you use the latest processors (i7 980, VIA...) they provide AES helper functions. There are highly optimized AES code samples, which beat unoptimized other ciphers.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 7:02 am 
Offline

Joined: January 2nd, 2010, 6:13 pm
Posts: 105
Thanks for the prompt response Laszlo. You have always been very helpful.

It's much more convenient to use RC4txt2hex because it's output results in a hex string which is nothing more than a sequence of hex digits.

In contrast, TEA, although being considered more secure, outputs an encrypted string that may contain accent (`) or quote (" and ') characters which need to be handled in a special way in := expressions or may output an encrypted string with a space character in the beginning or end of the encrypted string which makes it unusual for = assignments ( or inifile values). The plus point that it does not increase the length of string even after encryption is very good but does not outweigh the aforementioned disadvantage.

So I'm really glad that RC4txt2hex is the faster one. Double benefit. :D Although I would love to know of more ahk function based text string encryption methods especially if it does not increase the string size even after encryption and the encrypted string does not contain `, ", ' or space character in the beginning or end.

A little explanation will be thankfully appreciated:
Quote:
RC4 is faster if the tables are left in the cache, which is normally the case with modern CPU's.
I did not understand the red part.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 5:48 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
RC4 uses two arrays (tables) of 256 entries each. In AHK they occupy 2*256*4 = 2048 bytes of storage. If other programs are running, the processor cache may be filled up with their data, and each time RC4 comes to play, it has to reload the processor cache from the RAM. It would slow things down.

Search the Forum for the Windows crypto functions. You can call them with a few lines of AHK code, and convert the result to hex or Base64 with just another dll call. The later does not double the size of the result, but still can be handled as AHK string.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: March 14th, 2010, 10:32 pm 
Offline

Joined: January 2nd, 2010, 6:13 pm
Posts: 105
Just the information I needed. Thank you very much. :D


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 60 posts ]  Go to page Previous  1, 2, 3, 4  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher, sks, Stigg and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group