AutoHotkey Community

It is currently May 27th, 2012, 11:45 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 14 posts ] 
Author Message
 Post subject: Random Encryption
PostPosted: June 16th, 2010, 5:49 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
I mentioned the idea here and it got such a good reaction I decided to make a function for it.
This uses the fact that Random generates pseudorandom numers to advantage if you set the seed to a known value you can generate the same string of "random" numbers twice. In ths code I used them as offsets within a known string.
Code:
Var=y^E88-m!FsQ'zQ;Ep|k|R

MsgBox % "This is a coded string`n`n" Code("Hello There",1,1578)
MsgBox % "This is a decoded string`n`n" Code(Var,0,87681)

Code(x,y,Seed=12345) { ;x is string y=1 is code y=0 is decode Seed is the decode key
String=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 -=.,?':;/\!@#$`%^&*()_+|
Random, ,Seed
Loop, Parse, x
{
Pos:=InStr(String,A_LoopField,1)
Random, Offset , 1, 86
Coded.=SubStr(String, y ? (Pos+Offset)>86 ? Pos+Offset-86 : Pos+Offset : (Pos-Offset)<1 ? Pos-Offset+86 : Pos-Offset,1)
}
Return Coded
}
Edit: eliminated the extra u from the string
Edit 2: Added missing v


Last edited by None on September 6th, 2010, 10:54 pm, edited 3 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 16th, 2010, 12:02 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Nice! Very simple. However, there are only 2**32 seed values, which can be all tried in hours, so the security is not that high.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 16th, 2010, 7:20 pm 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
Laszlo wrote:
security is not that high.
Yes but that assumes all they lack is the seed.
If they don't know the source code they would have to try those 2**32 seeds for each:
order of the string (feel free to change it)
range of random offset (I would not make it more than 86 but less should work)
offset added or subtracted (use y=0 to code and y=1 to decode)

And yes I read this
Random's Comments wrote:
Do NOT use for CRYPTOGRAPHY without securely hashing several returned values together, otherwise the generator state can be learned after reading 624 consecutive values.
I would not call it high security but probably good enough for most things.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 17th, 2010, 9:04 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
We can improve the script a bit.
- with mod() and +/-1 selector of encrypt/decrypt the code can be written shorter
- we can use all characters of the keyboard, here I used the English ones
- removed an extra "u" in the string of characters
- the escape character: the back apostrophe (`) needs special handling, or for simplicity, excluded from the allowed characters, because it could modify the n, r, t... letter after it, messing up the data in printing
- For higher security the user can provide more than one key (seed), and we add the corresponding random numbers together.
Code:
Code(x,E,K1=0,K2=1,K3=2,K4=3) { ; x: data string, E=1|-1: encode|decode, K1..4: 32 bit unsigned keys
   Static S:="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 -=~!@#$%^&*()_+[\]{|};:'"",./<?>"
   Loop % StrLen(x)
      D%A_Index% := 0
   Loop 4 {
      Random,,K%A_Index%
      Loop % StrLen(x) {
         Random D, 0, 93
         D%A_Index% := mod(D+D%A_Index%,94)
      }
   }
   Loop Parse, x
      C .= SubStr(S, mod(InStr(S,A_LoopField,1)+93+E*D%A_Index%,94)+1, 1)
   Return C
}

When the Random command is used in this way, the security warning from the Help is not directly applicable: the offset values are not know to an attacker. It does not mean another attack would not break the cipher, but we don't know.

Don't forget, it is a character-by character encryption, therefore the same characters in the same position in two texts would encrypt to the same characters, which could leak information about your secret message if you use a key more than once. You could use one of the k1..k4 keys (or a fifth one) for a nonce (salt), which you can transmit with your message in the clear. It diversifies the cipher, so it does not leak equality of chars in the same place.

Edit 20100906: added missing "v" to the chars, as "None" noted


Last edited by Laszlo on September 6th, 2010, 11:48 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 6th, 2010, 9:56 pm 
Offline

Joined: June 12th, 2009, 11:36 pm
Posts: 1173
Location: Indianapolis IN, USA
I created a simple GUI for this. Although I've found a bug. Just decrypt the message it auto fills and you will notice that the word 'have' is typo'd 'hae' even though it was spelled correctly in the original message.

*EDIT* Also I have found that it doesn't encrypt very large messages well. :/

None's:
Code:
Gui, Add, Edit, vmte x6 y26 w150 h50 , 
Gui, Add, Text, x6 y6 w150 h20 , Message to encrypt.
Gui, Add, Edit, vek x106 y6 h15 w50, Key
Gui, Add, Text, x6 y86 w150 h20 , Encryption.
Gui, Add, Edit, vme ReadOnly x6 y106 w150 h50 , 
Gui, Add, Button, gEncrypt x26 y166 w110 h30 , Encrypt
Gui, Add, Text, x176 y6 w150 h20 , Code to decrypt.
Gui, Add, Edit, vdk x276 y6 h15 w50, 123
Gui, Add, Edit, vcte x176 y26 w150 h50 , 5ntMot53FomedG.De5,4.kA5s-4Vje;8QoNDA5q Cgp7xKPtlPcjCV&q&m8,i:9I!j5XttN|
Gui, Add, Text, x176 y86 w150 h20 , Decrypted message.
Gui, Add, Edit, vce ReadOnly x176 y106 w150 h50 , 
Gui, Add, Button, gDecrypt x196 y166 w110 h30 , Decrypt
Gui, ADd, Progress, Verticle x165 y0 w2 h205
Gui, Show, w335 h205, Encrypt/Decrypt
Return

Encrypt:
Gui, Submit, Nohide
me:=Code(mte,1,ek)
GuiControl,, me, %me%
Return

Decrypt:
Gui, Submit, Nohide
GuiControl,, ce, % Code(cte,0,dk)
Return

GuiClose:
ExitApp

Code(x,y,Seed=12345) { ;x is string y=1 is code y=0 is decode Seed is the decode key
String=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 -=.,?':;/\!@#$`%^&*()_+|
Random, ,Seed
Loop, Parse, x
{
Pos:=InStr(String,A_LoopField,1)
Random, Offset , 1, 86
Coded.=SubStr(String, y ? (Pos+Offset)>86 ? Pos+Offset-86 : Pos+Offset : (Pos-Offset)<1 ? Pos-Offset+86 : Pos-Offset,1)
}
Return Coded
}


Laszlo's:
Code:
Gui, Add, Edit, vmte x6 y26 w150 h50 , 
Gui, Add, Text, x6 y6 w150 h20 , Message to encrypt.
Gui, Add, Edit, vek x106 y6 h15 w50, Key
Gui, Add, Text, x6 y86 w150 h20 , Encryption.
Gui, Add, Edit, vme ReadOnly x6 y106 w150 h50 , 
Gui, Add, Button, gEncrypt x26 y166 w110 h30 , Encrypt
Gui, Add, Text, x176 y6 w150 h20 , Code to decrypt.
Gui, Add, Edit, vdk x276 y6 h15 w50, 123
Gui, Add, Edit, vcte x176 y26 w150 h50 , Q7yEzQZ!erZi#9w4)NZi,1Vo<$;01f& zVSIp)">;)_|D.Sx
Gui, Add, Text, x176 y86 w150 h20 , Decrypted message.
Gui, Add, Edit, vce ReadOnly x176 y106 w150 h50 , 
Gui, Add, Button, gDecrypt x196 y166 w110 h30 , Decrypt
Gui, ADd, Progress, Verticle x165 y0 w2 h205
Gui, Show, w335 h205, Encrypt/Decrypt
Return

Encrypt:
Gui, Submit, Nohide
me:=Code(mte,1,ek)
GuiControl,, me, %me%
Return

Decrypt:
Gui, Submit, Nohide
GuiControl,, ce, % Code(cte,-1,dk)
Return

GuiClose:
ExitApp

Code(x,E,K1=0,K2=1,K3=2,K4=3) { ; x: data string, E=1|-1: encode|decode, K1..4: 32 bit unsigned keys
   Static S:="abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 -=~!@#$%^&*()_+[\]{|};:'"",./<?>"
   Loop % StrLen(x)
      D%A_Index% := 0
   Loop 4 {
      Random,,K%A_Index%
      Loop % StrLen(x) {
         Random D, 0, 92
         D%A_Index% := mod(D+D%A_Index%,93)
      }
   }     
   Loop Parse, x
      C .= SubStr(S, mod(InStr(S,A_LoopField,1)+92+E*D%A_Index%,93)+1, 1)
   Return C
}

_________________
www.AutoHotkey.net/~Eedis
I love my wife and daughter so much.
Image


Last edited by Eedis on September 12th, 2010, 8:23 pm, edited 1 time in total.

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

Joined: November 28th, 2009, 4:45 am
Posts: 3089
The "v" was messed up because I accidentaly left it out of the string. :oops: (please correct in your post also)
When I made it I was thinking more about passwords stored in ini files not long passages. so I did not include, enter, tab, or ".
You can add them if you want.
Edit: can't seem to get enter to work :?
Edit again: This should work for long strings Including enters :)
Code:
Code(x,y,Seed=12345) { ;x is string y=1 is code y=0 is decode Seed is the decode key
AutoTrim, Off
String.=Chr(9)Chr(10)Chr(32)Chr(33)Chr(34)Chr(35)Chr(36)
Loop 58 ;removed ` and % from the list of characters they were too much trouble
 String.=Chr(A_Index+37)
Loop 186
 String.=Chr(A_Index+96)
Random, ,Seed
Loop, Parse, x
{
Pos:=InStr(String,A_LoopField,1)
Random, Offset , 1, 224
Coded.=SubStr(String, (y) ? Mod(Pos+Offset,224) : Mod(Pos-Offset,224),1)
}
Return Coded
}

Edit: removed ` and % from the list they caused errors :(


Last edited by None on September 21st, 2010, 12:01 am, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2010, 6:08 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
One way to handle special characters, like the new line or accented letters, is leaving them unencrypted:
Code:
Code(x,E,K1=0,K2=1,K3=2,K4=3) { ; x: data string, E=1|-1: encode|decode, K1..4: 32 bit unsigned keys
   Static S:="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 -=~!@#$%^&*()_+[\]{|};:'"",./<?>"
   Loop % StrLen(x)
      D%A_Index% := 0
   Loop 4 {
      Random,,K%A_Index%
      Loop % StrLen(x) {
         Random D, 0, 93
         D%A_Index% := mod(D+D%A_Index%,94)
      }
   }
   Loop Parse, x
      C .= (I:=InStr(S,A_LoopField,1)) ? SubStr(S,mod(I+93+E*D%A_Index%,94)+1,1) : A_LoopField
   Return C
}
We can handle more characters, and speed up the function (if the same keys are used repeatedly) by keeping the last used tables, when we restrict the maximum input length to 224 characters
Code:
Code(x,E,K1=0,K2=1,K3=2,K4=3) { ; x: data string, E=1|-1: encode|decode, K1..4: 32 bit unsigned keys
   Static
   If (C1!=K1 || C2!=K2 || C3!=K3 || C4!=K4) {
      L := 224, C1 := K1 , C2 := K2 , C3 := K3 , C4 := K4
      Loop %L%
         D%A_Index% := 0, S .= Chr(A_Index+31)
      Loop 4 {
         Random,,K%A_Index%
         Loop %L% {
            Random D, 0, L-1
            D%A_Index% := mod(D+D%A_Index%,L)
         }
      }
   }
   C =
   Loop Parse, x
      C .= " "<=(A:=A_LoopField) ? SubStr(S,mod(InStr(S,A,1)+L-1+E*D%A_Index%,L)+1,1) : A
   Return C
}
Be careful with the ciphertext. Dependent on your application where you handle them, some funny characters may be dropped, others may be changed, altering the corresponding character at decryption.

Edit 2011-03-12: The second function only works with short strings, less than 225 characters! (Thanks Encoder for noticing)


Last edited by Laszlo on March 12th, 2011, 9:48 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 12th, 2010, 11:57 pm 
Offline

Joined: January 2nd, 2008, 4:47 am
Posts: 150
Location: Freenode IRC
ProTip: Avoid using the word Encryption and you wont get into trouble. Just call it a cipher, since it contains a hidden secret.

Calling it Encryption will get people citing "security through obscurity!"

Also: Are we sure the pseudo random code only uses a 32bit seed?

_________________
Image

Need help right away? Get live support on IRC.
Already have an IRC client installed? /join #ahk


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 13th, 2010, 12:11 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
No obscurity is involved here. The algorithm is public, the secrecy is ensured by secret keys. It is encryption. A cipher is an algorithm for performing encryption or decryption, so these are just synonyms.

And the pseudorandom number generator is documented, it uses 32 bit integer seeds.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 13th, 2010, 12:43 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
Raccoon wrote:
Also: Are we sure the pseudo random code only uses a 32bit seed?

Random wrote:
NewSeed should be an integer between 0 and 4294967295 (0xFFFFFFFF)...
Known limitations for floating point: 1) only about 4,294,967,296 distinct numbers can be generated for any particular range,

Yes pretty sure, but if you use Laszlo's method with four different seeds you effectivly have 2**256 (1.16*10^77)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 21st, 2010, 12:09 am 
Offline

Joined: November 28th, 2009, 4:45 am
Posts: 3089
If you want extra security you can recode the coded output as many times as you want[code]coded=+Äa6W„þõ©ÿ­¶š:7’»î‘ù


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 11th, 2010, 8:17 am 
Offline

Joined: October 21st, 2010, 12:52 pm
Posts: 490
Quote:
Be careful with the ciphertext. Dependent on your application where you handle them, some funny characters may be dropped, others may be changed, altering the corresponding character at decryption.


I like your version, it can encrypt/decrypt non-english text, though some text might drop, but already cool.

I made the GUI bigger, cos it is pain in the a$$ to see ant-like text inside bug-like interface ;P

Code:
;....GUI by Eedis
Gui, Font, S7 Bold, Tahoma
Gui, Color, CCDCDC0
Gui, Add, Text,     x6    y6 w250 h22, Enter the Seed to encode:
Gui, Add, Text,     x278  y6 w250 h22, Enter the Seed to decode:
Gui, Add, Edit, vek x176  y6 w70 h22, default
Gui, Add, Edit, vdk x450  y6 w70 h22, default
;..
Gui, Add, Edit, vmte x6   y36 w250 h140,
Gui, Add, Edit, vcte x276 y36 w250 h140, 5ntMot53FomedG.De5,4.kA5s-4Vje;8QoNDA5q Cgp7xKPtlPcjCV&q&m8,i:9I!j5XttN|
Gui, Font, S10 Bold, Tahoma
Gui, Add, Text, x6   y196 w180 h22 cRed, ENCRYPTION
Gui, Add, Text, x276 y196 w180 h22 cBlue, DECRYPTION
;..
Gui, Add, Edit, vme ReadOnly x6   y224 w250 h140,
Gui, Add, Edit, vce ReadOnly x276 y224 w250 h140,
;..
Gui, Add, Button, gEncrypt x6   y366 w160 h36, Run..Encrypt
Gui, Add, Button, gDecrypt x276 y366 w160 h36, Run..Decrypt
Gui, Add, Button, gReload  x458 y366 w80 h36,  Reload
Gui, Show, w532 h400, Encrypt/Decrypt
Return

Encrypt:
Gui, Submit, Nohide
me:=CodeG(mte,1,ek)
GuiControl,, me, %me%
Return

Decrypt:
Gui, Submit, Nohide
GuiControl,, ce, % CodeG(cte,-1,dk)
Return

Reload:
Reload

GuiClose:
ExitApp

CodeG(x,E,K1=0,K2=1,K3=2,K4=3) { ; x: data string, E=1|-1: encode|decode, K1..4: 32 bit unsigned keys
   Static
   If (C1!=K1 || C2!=K2 || C3!=K3 || C4!=K4) {
      L := 224, C1 := K1 , C2 := K2 , C3 := K3 , C4 := K4
      Loop %L%
         D%A_Index% := 0, S .= Chr(A_Index+31)
      Loop 4 {
         Random,,K%A_Index%
         Loop %L% {
            Random D, 0, L-1
            D%A_Index% := mod(D+D%A_Index%,L)
         }
      }
   }
   C =
   Loop Parse, x
      C .= " "<=(A:=A_LoopField) ? SubStr(S,mod(InStr(S,A,1)+L-1+E*D%A_Index%,L)+1,1) : A
   Return C
}

_________________

~C'est La Vie~
I like AHK_L and Unicode ;-)
Joined : Oct 11, 2010


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 1st, 2011, 3:57 am 
Offline

Joined: April 8th, 2008, 1:08 am
Posts: 100
Location: Minnesota, USA
Here's the code translated into php (Laszlo's version).
Code:
function Code($x,$E,$K0=0,$K1=1,$K2=2,$K3=3) {
    static $S = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 -=~!@#$%^&*()_+[\\]{|};:'\"\",./<?>";
   
     for ($A_Index = 0; $A_Index <= strlen($x); $A_Index++)               // Loop % StrLen(x)
       {${'D' . $A_Index} = 0;}                                           //    D%A_Index% := 0
      
    for ($A_Index = 0; $A_Index < 4; $A_Index++) {                        // Loop 4 {
            mt_srand(${'K' . $A_Index});                                  //    Random,,K%A_Index%
            for ($A_Index = 0; $A_Index <= strlen($x); $A_Index++) {      //    Loop % StrLen(x) {
                  $D = mt_rand(0, 93);                                    //        Random D, 0, 93           
                  ${'D' . $A_Index} = fmod($D+${'D' . $A_Index}, 94);     //        D%A_Index% := mod(D+D%A_Index%,94)
            }
      }
      
   foreach(str_split($x, 1) as $A_Index => $A_LoopField)                  // Loop Parse, x
      {$C .= ($I = strpos($S,$A_LoopField)) ? substr($S,fmod($I+93+$E*${'D' . $A_Index},94)+1,1) : $A_LoopField;}
     
   return $C;
}

_________________
-trueski-


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 1st, 2011, 5:05 am 
Offline

Joined: December 26th, 2010, 7:40 pm
Posts: 4172
Location: Awesometown, USA
reminds me of my TI-Basic days... for highscore security, I used the rand as a "hash" - store the highscore in list(1 and into rand, then store rand into list(2. Stops the casual hacker.

_________________
Autofire, AutoClick, Toggle, SpamWindow Control Tools
Recommended: AutoHotkey_L


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 14 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: rrhuffy and 59 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