AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Built In Encryption/Decryption

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
purloinedheart



Joined: 04 Apr 2008
Posts: 537
Location: Canada

PostPosted: Wed Aug 26, 2009 3:11 am    Post subject: Built In Encryption/Decryption Reply with quote


Click the image for the EXE.

Quote:


Encrypt.ahk
Code:
SetBatchLines -1
#NoTrayIcon
#SingleInstance Off
Loop %0%
 File := %A_Index%

RegRead Setting, HKEY_CURRENT_USER, Software\Simcrypt
if (Setting = "Unique")
 {
 if (A_OSVersion = "WIN_VISTA")
  RegRead Key, HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows NT\CurrentVersion, ProductID
 else
  RegRead Key, HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows\CurrentVersion, ProductID
 goto Encrypt
 }
else if (Setting = "User")
 RegRead Key, HKEY_CURRENT_USER, Software\Simcrypt, User
else
 {
  Gui Add, Edit, vKey w125
  Gui Add, Button, x43 default, Encrypt
  Gui Show,, Key
  return
 }

ButtonEncrypt:
Gui Submit


Encrypt:
BinRead(File, Text, 0, 0)
FileGetSize Size, %File%, M
if Size > 0
 FileCopy %File%, %File%.bak
FileDelete %File%
FileAppend % VxEncrypt(Key, Text), %File%
FileDelete %File%.bak
GuiClose:
ExitApp

VxEncrypt(key, message, CryptEvent = "")
{
   LowerBound = 34
   UpperBound = 126
   StringReplace, Message, Message, `r`n, `n, all
   StringLeft, CryptEvent, CryptEvent, 1
   If !CryptEvent
      CryptEvent = e
   Loop, parse, key
      key .= "`n" Asc(A_LoopField)
   key := SubStr(key, InStr(Key, "`n",0,1)+1)
   NewMessage =
   Loop, Parse, Message
   {
      code := Asc(A_LoopField)
      If ( code >= LowerBound ) && ( code <= UpperBound )
      {
         mco := code
         prevc := A_Index
         newkey := ""
         Loop, Parse, key, `n
            code := Clamp(( code - (A_LoopField * ( Mod(A_LoopField, 2)
            * 2 - 1 )) * ((CryptEvent = "e") * 2 - 1) ), LowerBound, UpperBound )
         If CryptEvent = e
            mco := code
         Loop, Parse, key, `n
            newkey .= "`n" (prevc := Clamp((A_LoopField + mco * (Mod((mco
            +Prevc), 2) * 2 - 1) + Prevc), 1, 255))
         key := SubStr( newkey, 2 )
      }
      NewMessage .= Chr(code)
   }
return % NewMessage
}

clamp( number, low, high )
{
   span := (high - low + (high > low)*2 - 1)
   return number + span * Round(((low-number)/2 + (high-number)/2)/span)
}

BinRead(file, ByRef data, n=0, offset=0)
{
   h := DllCall("CreateFile","Str",file,"Uint",0x80000000,"Uint",3,"UInt",0,"UInt",3,"Uint",0,"UInt",0)
   IfEqual h,-1, SetEnv, ErrorLevel, -1
   IfNotEqual ErrorLevel,0,Return,0

   m = 0
   IfLess offset,0, SetEnv,m,2
   r := DllCall("SetFilePointerEx","Uint",h,"Int64",offset,"UInt *",p,"Int",m)
   IfEqual r,0, SetEnv, ErrorLevel, -3
   IfNotEqual ErrorLevel,0, {
      t = %ErrorLevel%
      DllCall("CloseHandle", "Uint", h)
      ErrorLevel = %t%
      Return 0
   }

   TotalRead = 0
   data =
   IfEqual n,0, SetEnv n,0xffffffff

   format = %A_FormatInteger%
   SetFormat Integer, Hex

   Loop %n%
   {
      result := DllCall("ReadFile","UInt",h,"UChar *",c,"UInt",1,"UInt *",Read,"UInt",0)
      if (!result or Read < 1 or ErrorLevel)
         break
      TotalRead += Read
      c += 0
      StringTrimLeft c, c, 2
      c = 0%c%
      StringRight c, c, 2
      data = %data%%c%
   }

   IfNotEqual ErrorLevel,0, SetEnv,t,%ErrorLevel%

   h := DllCall("CloseHandle", "Uint", h)
   IfEqual h,-1, SetEnv, ErrorLevel, -2
   IfNotEqual t,,SetEnv, ErrorLevel, %t%

   SetFormat Integer, %format%
   Totalread += 0
   Return TotalRead
}


Decrypt.ahk
Code:

SetBatchLines -1
#NoTrayIcon
#SingleInstance Off
Loop %0%
 File := %A_Index%
FileRead Text, %File%

RegRead Setting, HKEY_CURRENT_USER, Software\Simcrypt
if (Setting = "Unique")
 {
 if (A_OSVersion = "WIN_VISTA")
  RegRead Key, HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows NT\CurrentVersion, ProductID
 else
  RegRead Key, HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows\CurrentVersion, ProductID
 goto Decrypt
 }
else if (Setting = "User")
 RegRead Key, HKEY_CURRENT_USER, Software\Simcrypt, User
else
 {
  Gui Add, Edit, vKey w125
  Gui Add, Button, x43 default, Decrypt
  Gui Show,, Key
  return
 }

ButtonDecrypt:
Gui Submit

Decrypt:
BinText := VxEncrypt(Key, Text,1)
FileDelete %File%
BinWrite(File, BinText, 0, 0)
GuiClose:
ExitApp

VxEncrypt(key, message, CryptEvent = "")
{
   LowerBound = 34
   UpperBound = 126
   StringReplace, Message, Message, `r`n, `n, all
   StringLeft, CryptEvent, CryptEvent, 1
   If !CryptEvent
      CryptEvent = e
   Loop, parse, key
      key .= "`n" Asc(A_LoopField)
   key := SubStr(key, InStr(Key, "`n",0,1)+1)
   NewMessage =
   Loop, Parse, Message
   {
      code := Asc(A_LoopField)
      If ( code >= LowerBound ) && ( code <= UpperBound )
      {
         mco := code
         prevc := A_Index
         newkey := ""
         Loop, Parse, key, `n
            code := Clamp(( code - (A_LoopField * ( Mod(A_LoopField, 2)
            * 2 - 1 )) * ((CryptEvent = "e") * 2 - 1) ), LowerBound, UpperBound )
         If CryptEvent = e
            mco := code
         Loop, Parse, key, `n
            newkey .= "`n" (prevc := Clamp((A_LoopField + mco * (Mod((mco
            +Prevc), 2) * 2 - 1) + Prevc), 1, 255))
         key := SubStr( newkey, 2 )
      }
      NewMessage .= Chr(code)
   }
return % NewMessage
}


clamp( number, low, high )
{
   span := (high - low + (high > low)*2 - 1)
   return number + span * Round(((low-number)/2 + (high-number)/2)/span)
}


BinWrite(file, data, n=0, offset=0)
{

   h := DllCall("CreateFile","str",file,"Uint",0x40000000,"Uint",0,"UInt",0,"UInt",4,"Uint",0,"UInt",0)
   IfEqual h,-1, SetEnv, ErrorLevel, -1
   IfNotEqual ErrorLevel,0,Return,0

   m = 0
   IfLess offset,0, SetEnv,m,2
   r := DllCall("SetFilePointerEx","Uint",h,"Int64",offset,"UInt *",p,"Int",m)
   IfEqual r,0, SetEnv, ErrorLevel, -3
   IfNotEqual ErrorLevel,0, {
      t = %ErrorLevel%
      DllCall("CloseHandle", "Uint", h)
      ErrorLevel = %t%
      Return 0
   }

   TotalWritten = 0
   m := Ceil(StrLen(data)/2)
   If (n <= 0 or n > m)
       n := m
   Loop %n%
   {
      StringLeft c, data, 2
      StringTrimLeft data, data, 2
      c = 0x%c%
      result := DllCall("WriteFile","UInt",h,"UChar *",c,"UInt",1,"UInt *",Written,"UInt",0)
      TotalWritten += Written
      if (!result or Written < 1 or ErrorLevel)
         break
   }

   IfNotEqual ErrorLevel,0, SetEnv,t,%ErrorLevel%

   h := DllCall("CloseHandle", "Uint", h)
   IfEqual h,-1, SetEnv, ErrorLevel, -2
   IfNotEqual t,,SetEnv, ErrorLevel, %t%

   Return TotalWritten
}

Install.ahk
Code:
#NoTrayIcon
SetBatchLines -1
Gui -Caption
Gui Color, 0x80B2D3
Gui Add, Edit, r10 ReadOnly,
(
SYMCRYPT v. 1.4
------------------------------
V 1.4
  Context Menu support for all file types
  Backup files larger than 1MB during encryption
V 1.3
  New encryption method
  Read/Write binary
v 1.2
 Encrypt .txt files
  - Drag-n-Drop to the EXE or
  - Right click the .txt file and choose Encrypt.
  - Computer Specific Key
  - Set default key

About Simcrypt
-----------------
Simcrypt is a simple encryption program.

Currently Simcrypt can only encrypt simple text files.

This program should not be used for any serious
purposes, the encryption level is very weak and
errors may occur.

Any feedback or questions can be sent to
  abhishek.regmi@gmail.com

Use at your own risk.
)
Gui Font, s15
Gui Add, Button, w100 x30 y150,Install
Gui Add, Button, w100 x145 y150, Exit
Gui Show, w280 h200
return

ButtonInstall:
IfExist %A_ProgramFiles%\Simpcrypt\
 FileRemoveDir, %A_ProgramFiles%\Simpcrypt\, 1
FileCreateDir %A_ProgramFiles%\Simpcrypt\
FileInstall Encrypt.exe, %A_ProgramFiles%\Simpcrypt\Encrypt.exe
FileInstall Decrypt.exe, %A_ProgramFiles%\Simpcrypt\Decrypt.exe
FileInstall Options.exe, %A_ProgramFiles%\Simpcrypt\Options.exe
FileInstall Uninstall.exe, %A_ProgramFiles%\Simpcrypt\Uninstall.exe
FileCreateDir %A_StartMenuCommon%\Simpcrypt\
FileCreateShortcut %A_ProgramFiles%\Simpcrypt\Options.exe, %A_StartMenuCommon%\Simpcrypt\Options.lnk
FileCreateShortcut %A_ProgramFiles%\Simpcrypt\Uninstall.exe, %A_StartMenuCommon%\Simpcrypt\Uninstall.lnk


RegWrite REG_SZ, HKEY_CLASSES_ROOT, *\shell\Encrypt\Command, , "%A_ProgramFiles%\Simpcrypt\Encrypt.exe" "`%0"
RegWrite REG_SZ, HKEY_CLASSES_ROOT, *\shell\Decrypt\Command, , "%A_ProgramFiles%\Simpcrypt\Decrypt.exe" "`%0"
Msgbox Symcrypt has successfully been installed!
ButtonExit:
GuiClose:
ExitApp

Uninstall.ahk
Code:
FileRemoveDir %A_ProgramFiles%\Simpcrypt\, 1
FileRemoveDir %A_StartMenuCommon%\Simpcrypt, 1
RegDelete HKEY_CLASSES_ROOT, *\shell\Encrypt\
RegDelete HKEY_CLASSES_ROOT, *\shell\Decrypt\
RegDelete HKEY_CURRENT_USER, Software\Simcrypt
Msgbox Symcrypt has successfully been uninstalled!

Options.ahk
Code:

#SingleInstance Force
SetBatchLines -1
RegRead Setting, HKEY_CURRENT_USER, Software\Simcrypt
Gui Color, 0x80B2D3
Gui Add, Text, , OPTIONS
Gui Add, CheckBox, x15 y30 vUnique, Auto-Gen Key
Gui Add, CheckBox, x15 y50 vDefault, User Defined Key
Gui Add, Edit, x25 y72 vUserKey, Pur|_01n3d
Gui Add, CheckBox, x15 y95 vSelect, Manual Key
Gui Add, Button, default, Submit

if (Setting = Unique)
 GuiControl, , Unique, 1
else if (Setting = User)
 {
  RegRead UserKey, HKEY_CURRENT_USER, Software\Simcrypt, User
  GuiControl, , Default, 1
  GuiControl, , UserKey, %UserKey%
 }
else
 GuiControl, , Select, 1
Gui Show, w250 h200, Encrypt Options
return



ButtonSubmit:
Gui Submit
if (Unique = 1)
 RegWrite REG_SZ, HKEY_CURRENT_USER, Software\Simcrypt, , Unique
else if (Default = 1)
 {
  RegWrite REG_SZ, HKEY_CURRENT_USER, Software\Simcrypt, , User
  RegWrite REG_SZ, HKEY_CURRENT_USER, Software\Simcrypt, User, %UserKey%
 }
else if (Select = 1)
 RegWrite REG_SZ, HKEY_CURRENT_USER, Software\Simcrypt, , Manual
Msgbox Changes have been made!
GuiClose:
ExitApp

Credits
VxE
Laszlo


Last edited by purloinedheart on Wed Aug 26, 2009 4:30 pm; edited 5 times in total
Back to top
View user's profile Send private message
purloinedheart



Joined: 04 Apr 2008
Posts: 537
Location: Canada

PostPosted: Wed Aug 26, 2009 3:11 am    Post subject: Reply with quote

Known Bugs as of 1.3
Code:

Slow speed on large files


Coming soon
Code:

Selection of different encryption methods in options
Number of attempts before file deletion


Last edited by purloinedheart on Wed Aug 26, 2009 4:26 pm; edited 1 time in total
Back to top
View user's profile Send private message
SoggyDog



Joined: 02 May 2006
Posts: 783
Location: Greeley, CO

PostPosted: Wed Aug 26, 2009 2:43 pm    Post subject: Reply with quote

PurloinedHeart wrote:
Coming soon
Code:
...
Create a backup copy of the file before encryption
...

Good idea because: If/when someone tries to decrypt the file with wrong password and data is forever fried

Bad idea because: You still have a plain text version of the file on your computer, so what's the point of encrypting at all
_________________

SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
purloinedheart



Joined: 04 Apr 2008
Posts: 537
Location: Canada

PostPosted: Wed Aug 26, 2009 3:41 pm    Post subject: Reply with quote

The backup was only meant to be used in case someone tries to encrypt a large file, and there's an error before it's done. The backup will be deleted as soon as the file is encrypted.
Back to top
View user's profile Send private message
SoggyDog



Joined: 02 May 2006
Posts: 783
Location: Greeley, CO

PostPosted: Wed Aug 26, 2009 4:08 pm    Post subject: Reply with quote

I see...
That makes a bit of sense and eliminates my "Bad idea" comment, but leaves us with the problem of someone trying to decrypt the file with the wrong passcode and forever destroying the data.
_________________

SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
purloinedheart



Joined: 04 Apr 2008
Posts: 537
Location: Canada

PostPosted: Wed Aug 26, 2009 4:21 pm    Post subject: Reply with quote

It's not very difficult to allow multiple tries, but the problem is that this would not be desired by everyone, I'll think about adding "Number of attempts before deletion" to options soon, thanks Razz

Edit: New V 1.4
Back to top
View user's profile Send private message
SoggyDog



Joined: 02 May 2006
Posts: 783
Location: Greeley, CO

PostPosted: Wed Aug 26, 2009 4:51 pm    Post subject: Reply with quote

PurloinedHeart wrote:
I'll think about adding "Number of attempts before deletion"

I'm not sure that's the way to go either;

Example:
I'm a mean guy and I want to see your data, but then I see that it's been encrypted;
This upsets me so I decide to just keep entering the wrong password until it's deleted.
Hey, if I can't have it, neither can you.
_________________

SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
purloinedheart



Joined: 04 Apr 2008
Posts: 537
Location: Canada

PostPosted: Wed Aug 26, 2009 4:54 pm    Post subject: Reply with quote

They could easily just press 'Del' Wink
Back to top
View user's profile Send private message
SoggyDog



Joined: 02 May 2006
Posts: 783
Location: Greeley, CO

PostPosted: Wed Aug 26, 2009 5:09 pm    Post subject: Reply with quote

PurloinedHeart wrote:
They could easily just press 'Del' Wink

a) I know that and you know that, but
b) You have know idea how stupid the people I support are.

[edit] That might be a great "General Chat" thread (if it hasn't already been done)... Stupid user stories!
_________________

SoggyDog
Dwarf Fortress:
"The most intriguing game I've ever played."
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
purloinedheart



Joined: 04 Apr 2008
Posts: 537
Location: Canada

PostPosted: Wed Aug 26, 2009 5:33 pm    Post subject: Reply with quote

Having some problems while encrypting/decrypting some .exes atm
Shocked
(Tested on a 5MB EXE, took some time, exe ended up being corrupted)
Back to top
View user's profile Send private message
purloinedheart



Joined: 04 Apr 2008
Posts: 537
Location: Canada

PostPosted: Thu Sep 17, 2009 2:43 am    Post subject: Reply with quote

Bump -- Anyone have any methods to improve speed? The binary conversion seems slow/inefficient

Edit: On my new laptop, took about 30 seconds to encrypt a 1.5mb exe file
(core2 T6500 @ 2.1, 4mb ram) and script only runs on one core Sad

Edit 2: Computer seem sot be dying when I try to decrypt it
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group