AutoHotkey Community

It is currently May 27th, 2012, 2:57 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: January 29th, 2008, 11:07 am 
Offline

Joined: April 22nd, 2007, 6:33 pm
Posts: 1833
An encryption/decryption method, that appears secure when 2 unique keys are chosen and a unique order for the characters to be used. It is only meant for short strings to be encoded, as each character takes progressively longer to encrypt. I created this for communication with php on a server, so php version will follow shortly.


May be used as:

Code:
Prime_Crypt(EncDec, String, 1Key, 2Key, Chars)


EncDec must be the literal word Encrypt or Decrypt
String is the string you wish to enc/decrypt
1Key is a unique key you must specify (may be any length)
2Key is a unique key you must specify (may be any length)
Chars is an optional parameter, but if you want it to be secure then you should change it. It is a list of all the characters you plan on using. ie:

0123456789abcdefghijklmnopqrstuvwxyzABCDE$&*)...........

and so on, although the strength in the encryption is that this should be in a random order. I have made this to give you a random order if you want one:

Code:
StringCaseSense, On

Chars := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!£$^&*()_-+={[}]:;@'~#<>.| "
StringSplit, Chars, Chars

Loop
{
   Random, Rand, 1, % StrLen(Chars)
   NewString .= SubStr(Chars, Rand, 1)
   StringReplace, Chars, Chars, % SubStr(Chars, Rand, 1),, All
   
   If !Chars
   Break
}


MsgBox, Finished:`n`n%NewString%


And the actual encryption. Just copy and paste and it will run an example:

When decrypting and ecrypting, the 2 keys and the character order used must be the same or you will not be able to decrypt


Code:
#SingleInstance, Force

String = This Is A String That Will Be Tested For Encryption!
1Key = 12345678
2Key = 87654321

NewString := Prime_Crypt("Encrypt", String, 1Key, 2Key)
MsgBox, The string:`n`n%String%`n`nHas been encrypted to:`n`n%NewString%
String := Prime_Crypt("Decrypt", NewString, 1Key, 2Key)
MsgBox, The encrypted string:`n`n%NewString%`n`nHas been decrypted back to:`n`n%String%
ExitApp
Return

Prime_Crypt(EncDec, String, 1Key, 2Key, Chars="")
{
   If !Chars
   Chars := "U[5.vy:m~]s3lXAjVKhFP0^6ofbQk7e""`%?udY><qE{=GrSN_B+}nO$M()9cZtg-H£ 8R!az'i1wx#D4|p;W@&ILC2*JT"

   StringSplit, 1Key, 1Key
   StringSplit, 2Key, 2Key
   StringSplit, String, String
   StringSplit, Chars, Chars

   LoopNum := 0, Prime := 3
   Loop, % StrLen(String)   ;%
   {   
      LoopNum := (LoopNum >= StrLen(1Key)) ? 1 : LoopNum+1
      
      Num := 2
      Loop
      {
         Mod(Prime, Num) = 0 ? Prime++ : Num++      
         If (Prime = Num)
         Break
      }
      
      ChosenPrime := 0
      Loop, %Prime%
      ChosenPrime := (ChosenPrime = StrLen(2Key)) ? 1 : ChosenPrime+1
   
      Char := String%A_Index%
      Loop, % StrLen(Chars)   ;%
      {
         If (Char == Chars%A_Index%)
         CryptAscNum := A_Index, Break
      }

      Loop, % 1Key%LoopNum%+2Key%ChosenPrime%   
      {
         If (EncDec = "Encrypt")
         CryptAscNum := (CryptAscNum >= StrLen(Chars)) ? 0 : CryptAscNum+1
         Else If (EncDec = "Decrypt")
         CryptAscNum := (CryptAscNum <= 0) ? StrLen(Chars) : CryptAscNum-1
         Else
         Return, "Only Encrypt or Decrypt may be used as parameters for EncDec with literal quotes"
      }
      NewString .= Chars%CryptAscNum%
      Prime++
   }
   Return, NewString
}
Esc::ExitApp


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2008, 1:41 pm 
Offline

Joined: July 15th, 2007, 1:43 am
Posts: 1320
Now, just to wait for Laszlo...

_________________
Religion is false. >_>


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2008, 1:54 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Substitution ciphers like this are far from secure but they have practical uses. I currently have a client (ahk) server (php) scripts that obfuscate strings with hex conversion and simple rotation. It's fast and can be done in minimal amount of code. Statistically no encryption is absolute, so what you employ should depend on the nature of your working environment. tic's method looks interesting, I'd like to see his finished php version :)

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2008, 2:54 pm 
Offline

Joined: April 22nd, 2007, 6:33 pm
Posts: 1833
strictly speaking it isnt neccesary to use prime numbers, and this is greatly slowing the code down for longer strings.

i just need a way to generate a constantly changing number. it doesnt matter that a "hacker" will know this routine, so in the case above 3,5,7,11,13.....
as the hacker does not know the length of the keys or what they contain, but this number must still be variable. unfortuantely it takes time to work out large prime numbers. i need a formula that will generate a constantly changing result, and will not be reciprocal, for example using sine or cosine. so for example prime numbers will never be the same as the last one and the result has a varying degree of increment, however it is purely divergent, so will continue to increase. it would be ideal to have an oscilating function that does not give the same results every cycle (if that makes sense :))

any ideas?

Edit:

This is close but no cigar. it repeats after nearly 10000:

Code:
pi := 4*ATan(1)

Loop, 10000
y := Round(Sqrt(Mod(100*(Cos(A_Index*(pi/180)+A_Index)), 100*(Sin(A_Index*(pi/180)+A_Index)))**2))


anyone can do better?

Edit2:

This does not repeat until about 994000:

Code:
y := Round(Sqrt(Mod(100*(Cos(A_Index*(pi/180)+A_Index)), 100*(Sin(A_Index*(pi/180)+Cos(A_Index))))**2))


but is quite slow. anyone got a quicker one?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2008, 6:01 pm 
Offline

Joined: April 22nd, 2007, 6:33 pm
Posts: 1833
I will write a php version of this Titan, but am first just trying to perfect the ahk version (i had already started on the php version).

Once finished I will fully show how to copy protect an exe using this method and still allow your app to be portable!

I can see a weakness, but it relates to what I am currently working on now. As said earlier I need a non repeating array of numbers, of infinite length that can be recreated (ie not random), and the first number in the array must be greater than the length of the key.

I would be interested in what Laszlo has to say and whether he can suggest a way to generate these numbers.

If anyone needs an explanation of how it works, I will explain (dont read that as me not thinking youre smart enough lol ;) just that you may not want to bumble through my code as I know I often dont want to with other peoples scripts)


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

Joined: January 2nd, 2010, 6:13 pm
Posts: 105
tic wrote:
Chars is an optional parameter, but if you want it to be secure then you should change it. It is a list of all the characters you plan on using. ie:

0123456789abcdefghijklmnopqrstuvwxyzABCDE$&*)...........


I liked the fact that we could choose the characters we plan on using. But suppose if I chose to use only alpha-numeric characters, ie, [a-zA-Z0-9] in the "Chars" variable, then the characters in the original string which are not present in the "Chars" variable won't be decrypted. They will stay as they were in the encrypted text even after decryption.


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

Joined: April 22nd, 2007, 6:33 pm
Posts: 1833
Heh....I was just mucking around....I dont think it would be a good idea to use this rereading this. Phew that was a long time ago...


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

Joined: January 2nd, 2010, 6:13 pm
Posts: 105
Well, I was just studying whatever I could find about encryption in this forum, even if they are old.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: Kirtman and 13 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