Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

[AHK_L] Crypt - ahk cryptography class (Encryption, Hashing)


  • Please log in to reply
83 replies to this topic
Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
not sure where the problem is, but it definetly on your side, because after unzipping and running test.ahk works fine for me with last AHK_L

Elderon
  • Members
  • 36 posts
  • Last active: Apr 08 2014 04:16 PM
  • Joined: 17 Aug 2012
Perhaps I'm running it wrong? I right click the test.ahk and open with> AHK_L unicode32...
I use AutoHotKey_l!
Windows 7 Ultimate x64
I don't like DropBox, try SugarSync instead.
while(!(succeed = try()));

Uberi
  • Moderators
  • 1119 posts
  • Last active: May 02 2015 06:05 PM
  • Joined: 23 Aug 2010
Might be a working directory issue. Try using #Include %A_ScriptDir% at the top of the script (I recommend doing this for any script that uses this directive anyways).

Elderon
  • Members
  • 36 posts
  • Last active: Apr 08 2014 04:16 PM
  • Joined: 17 Aug 2012
That did it! Thanks Uberi!!
I use AutoHotKey_l!
Windows 7 Ultimate x64
I don't like DropBox, try SugarSync instead.
while(!(succeed = try()));

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006
These are very well written set of functions.

I've seen many examples of var to file to hash/encrypt to decrypt,
but no examples of var to hash/encrypt to decrypt.
I'm working with SHA2 family (4-6) and have tested many variations of:
string := "Hello"
MsgBox % hash := Crypt.Hash.StrHash( string, 6, "pw" ) [color=#008000]; have also tried StrEncrypt()[/color]
MsgBox % [color=#BF0000]Crypt.Hash.StrDecrypt( hash, "pw", 0, 6 )[/color]
Im able to encrypt and return Hash,
But not able to decrypt, it always returns blank.
Am I doing something wrong or to understand I write/read to/from file?
I tend to stay far away from crypt in general so pardon my lack of knowledge..

Thanks for any guidance.

Posted Image

don't duplicate, iterate!


Uberi
  • Moderators
  • 1119 posts
  • Last active: May 02 2015 06:05 PM
  • Joined: 23 Aug 2010
Hashes are, by definition, one way functions. They are essentially designed to be impossible to decrypt, except through brute force. Even through brute force, there may exist multiple possible results for any given hash value.

Wikipedia has a nice explanation: <!-- m -->http://en.wikipedia....i/Hash_function<!-- m -->

As a sort of demonstration, you can hash a random string, which has a high Kolmogorov complexity, and obtain a short result. Since the result is shorter than the minimum length of its description in a universal description language, it contains less information. Since decryption is a function of the input, decryption resulting in the original input is impossible.

TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006
ahh I see, that makes total sense thanks as always Uberi.
So "pw" is redundant then.. ok got it..

Posted Image

don't duplicate, iterate!


Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010

So "pw" is redundant then..

not exactly, if you use password, HMAC hashing algorithm will be used, and the next parameter after pw is it's algo
StrHash(string,HashAlg = 1,pwd = "",hmac_alg = 1)
			;password hashing algorithms
			HASH_ALG := HashAlg==1?c.CALG_MD5
						:HashAlg==2?c.CALG_MD2
						:HashAlg==3?c.CALG_SHA
						:HashAlg==4?c.CALG_SHA_256	;Vista+ only
						:HashAlg==5?c.CALG_SHA_384	;Vista+ only
						:HashAlg==6?c.CALG_SHA_512	;Vista+ only
						:0
			;encryption algorithms
			HMAC_KEY_ALG 	:= hmac_alg==1?c.CALG_RC4
								:hmac_alg==2?c.CALG_RC2
								:hmac_alg==3?c.CALG_3DES
								:hmac_alg==4?c.CALG_3DES_112
								:hmac_alg==5?c.CALG_AES_128 ;not supported for win 2000
								:hmac_alg==6?c.CALG_AES_192	;not supported for win 2000
								:hmac_alg==7?c.CALG_AES_256	;not supported for win 2000
								:0

to use StrDecrypt, you must pass it a hash returned from StrEncrypt. It is not a real hash, but just human-readable representation of binary data.
string := "Hello"
MsgBox % hash := Crypt.Encrypt.StrEncrypt( string, "pw", 5, 1 ) ; encrypts string using AES_128 encryption and MD5 hash
MsgBox % Crypt.Encrypt.StrDecrypt( hash, "pw", 5, 1 ) ;not a Crypt.Hash


jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
deo... can you take a look about this script/func

#Warn
#SingleInstance, Force

Gui, Margin, 10, 10
Gui, Font, s10
Gui, Add, GroupBox, x10 y10 w650 h55, Text / Text+Salt
Gui, Add, Edit, x20 y30 w300 vStr gUseStr, AutoHotkey
Gui, Add, Edit, x350 y30 w300 vStr2 gUseStr, Salt
Gui, Add, GroupBox, x10 y85 w650 h55, MD5 / MD5+Salt:
Gui, Add, Edit, x20 y105 w300 vMD5 ReadOnly Lowercase
Gui, Add, Edit, x350 y105 w300 vMD5Salt ReadOnly
Gui, Add, GroupBox, x10 y150 w650 h55, SHA1 / SHA1+Salt:
Gui, Add, Edit, x20 y170 w300 vSHA1	ReadOnly
Gui, Add, Edit, x350 y170 w300 vSHA1Salt ReadOnly
Gui, Add, GroupBox, x10 y215 w650 h55, SHA256 / SHA256+Salt:
Gui, Add, Edit, x20 y235 w300 vSHA256 ReadOnly
Gui, Add, Edit, x350 y235 w300 vSHA256Salt ReadOnly
Gui, Add, GroupBox, x10 y280 w650 h55, SHA384 / SHA384+Salt:
Gui, Add, Edit, x20 y300 w300 vSHA384 ReadOnly
Gui, Add, Edit, x350 y300 w300 vSHA384Salt ReadOnly
Gui, Add, GroupBox, x10 y345 w650 h55, SHA512 / SHA512+Salt:
Gui, Add, Edit, x20 y365 w300 vSHA512 ReadOnly
Gui, Add, Edit, x350 y365 w300 vSHA512Salt ReadOnly

UseStr:
	GuiControlGet, Str
	GuiControlGet, Str2
	Gui, Show
	GuiControl,,MD5, % MD5( Str )
	GuiControl,,MD5Salt, % SSMD5( Str, Str2)
	GuiControl,,SHA1, % SHA1( Str )
	GuiControl,,SHA1Salt, % SSSHA1( Str, Str2)
	GuiControl,,SHA256, % SHA256( Str )
	GuiControl,,SHA256Salt, % SSSHA256( Str, Str2)
	GuiControl,,SHA384, % SHA384( Str )
	GuiControl,,SHA384Salt, % SSSHA384( Str, Str2)
	GuiControl,,SHA512, % SHA512( Str )
	GuiControl,,SHA512Salt, % SSSHA512( Str, Str2)
Return

GuiClose:
GuiEscape:
ExitApp


;   ===============================================================================================
;   Function .......: MD5, SHA1
;   Description ....: Hash to determine strings and files
;   Author(s) ......: by Bentschi
;   Origin .........: http://de.autohotkey.com/forum/topic8295.html
;   ===============================================================================================
	MD5(string, encoding="utf-8") {
		return CalcStringHash(string, 0x8003, encoding)
	}
	SHA1(string, encoding="utf-8") {
		return CalcStringHash(string, 0x8004, encoding)
	}
	SHA256(string, encoding="utf-8") {
		return CalcStringHash(string, 0x800c, encoding)
	}
	SHA384(string, encoding="utf-8") {
		return CalcStringHash(string, 0x800d, encoding)
	}
	SHA512(string, encoding="utf-8") {
		return CalcStringHash(string, 0x800e, encoding)
	}

	CalcAddrHash(addr, length, algid, byref hash=0, byref hashlength=0) {
		hProv := size := hHash := hash := ""
		static h := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"]
		static b := h.minIndex()
		o := ""
		if (DllCall("advapi32\CryptAcquireContext", "ptr*", hProv, "ptr", 0, "ptr", 0, "uint", 1, "uint", 0xF0000000)) {
			if (DllCall("advapi32\CryptCreateHash", "ptr", hProv, "uint", algid, "uint", 0, "uint", 0, "ptr*", hHash)) {
				if (DllCall("advapi32\CryptHashData", "ptr", hHash, "ptr", addr, "uint", length, "uint", 0)) {
					if (DllCall("advapi32\CryptGetHashParam", "ptr", hHash, "uint", 2, "ptr", 0, "uint*", hashlength, "uint", 0)) {
						VarSetCapacity(hash, hashlength, 0)
						if (DllCall("advapi32\CryptGetHashParam", "ptr", hHash, "uint", 2, "ptr", &hash, "uint*", hashlength, "uint", 0)) {
							Loop, % hashlength
							{
								v := NumGet(hash, A_Index-1, "uchar")
								o .= h[(v>>4)+b] h[(v&0xf)+b]
							}
						}
					}
				}
				DllCall("advapi32\CryptDestroyHash", "ptr", hHash)
			}
			DllCall("advapi32\CryptReleaseContext", "ptr", hProv, "uint", 0)
		}
		return o
	}

	CalcStringHash(string, algid, encoding="utf-8", byref hash=0, byref hashlength=0) {
		chrlength := (encoding="cp1200" || encoding="utf-16") ? 2 : 1
		length := (StrPut(string, encoding)-1) * chrlength
		VarSetCapacity(data, length, 0)
		StrPut(string, &data, floor(length/chrlength), encoding)
		return CalcAddrHash(&data, length, algid, hash, hashlength)
	}

	CalcFileHash(filename, algid, continue=0, byref hash=0, byref hashlength=0) {
		if (!(f := FileOpen(filename, "r")))
			return
		f.pos := 0
		if (!continue && f.length>0x7fffffff)
			return
		if (!continue) {
			VarSetCapacity(data, f.length, 0)
			f.rawRead(&data, f.length)
			f.pos := oldpos
			return CalcAddrHash(&data, f.length, algid, hash, hashlength)
		}
		hashlength := 0
			while (f.pos<f.length) {
			readlength := (f.length-fpos>continue) ? continue : f.length-f.pos
			VarSetCapacity(data, hashlength+readlength, 0)
			DllCall("RtlMoveMemory", "ptr", &data, "ptr", &hash, "ptr", hashlength)
			f.rawRead(&data+hashlength, readlength)
			h := CalcAddrHash(&data, hashlength+readlength, algid, hash, hashlength)
		}
		return h
	}
;   ===============================================================================================
;   Function .......: Secure Salted MD5, SHA1, SHA256, SHA384 & SHA512
;   Description ....: Extension to protect it against hashtables
;   Author(s) ......: by IsNull
;   Origin .........: http://de.autohotkey.com/forum/topic9797-45.html#78997
;   ===============================================================================================
	SSMD5(data, salt) {
		hash := ""
		saltedHash := MD5(data . salt) 
		saltedHashR := MD5(salt . data)
		len := StrLen(saltedHash)
		Loop, % len / 2
		{
			byte1 := "0x" . SubStr(saltedHash, 2 * A_index - 1, 2)
			byte2 := "0x" . SubStr(saltedHashR, 2 * A_index - 1, 2)
			SetFormat, integer, hex
			hash .= StrLen(ns := SubStr(byte1 ^ byte2, 3)) < 2 ? "0" ns : ns
		}
		SetFormat, integer, dez
		return hash
	}

	SSSHA1(data, salt) {
		hash := ""
		saltedHash := SHA1(data . salt) 
		saltedHashR := SHA1(salt . data)
		len := StrLen(saltedHash)
		Loop, % len / 2
		{
			byte1 := "0x" . SubStr(saltedHash, 2 * A_index - 1, 2)
			byte2 := "0x" . SubStr(saltedHashR, 2 * A_index - 1, 2)
			SetFormat, integer, hex
			hash .= StrLen(ns := SubStr(byte1 ^ byte2, 3)) < 2 ? "0" ns : ns
		}
		SetFormat, integer, dez
		return hash
	}

	SSSHA256(data, salt) {
		hash := ""
		saltedHash := SHA256(data . salt) 
		saltedHashR := SHA256(salt . data)
		len := StrLen(saltedHash)
		Loop, % len / 2
		{
			byte1 := "0x" . SubStr(saltedHash, 2 * A_index - 1, 2)
			byte2 := "0x" . SubStr(saltedHashR, 2 * A_index - 1, 2)
			SetFormat, integer, hex
			hash .= StrLen(ns := SubStr(byte1 ^ byte2, 3)) < 2 ? "0" ns : ns
		}
		SetFormat, integer, dez
		return hash
	}

	SSSHA384(data, salt) {
		hash := ""
		saltedHash := SHA384(data . salt) 
		saltedHashR := SHA384(salt . data)
		len := StrLen(saltedHash)
		Loop, % len / 2
		{
			byte1 := "0x" . SubStr(saltedHash, 2 * A_index - 1, 2)
			byte2 := "0x" . SubStr(saltedHashR, 2 * A_index - 1, 2)
			SetFormat, integer, hex
			hash .= StrLen(ns := SubStr(byte1 ^ byte2, 3)) < 2 ? "0" ns : ns
		}
		SetFormat, integer, dez
		return hash
	}

	SSSHA512(data, salt) {
		hash := ""
		saltedHash := SHA512(data . salt) 
		saltedHashR := SHA512(salt . data)
		len := StrLen(saltedHash)
		Loop, % len / 2
		{
			byte1 := "0x" . SubStr(saltedHash, 2 * A_index - 1, 2)
			byte2 := "0x" . SubStr(saltedHashR, 2 * A_index - 1, 2)
			SetFormat, integer, hex
			hash .= StrLen(ns := SubStr(byte1 ^ byte2, 3)) < 2 ? "0" ns : ns
		}
		SetFormat, integer, dez
		return hash
	}
;   ===============================================================================================



edit:
And with your includes it works but I get many errors
#Include Crypt.ahk
#Include CryptConst.ahk
#Include CryptFoos.ahk
	
#Warn
#SingleInstance, Force

Gui, Margin, 10, 10
Gui, Font, s10
Gui, Add, GroupBox, x10 y10 w650 h55, Text / Text+Salt
Gui, Add, Edit, x20 y30 w300 vStr gUseStr, AutoHotkey
Gui, Add, Edit, x350 y30 w300 vStr2 gUseStr, Salt
Gui, Add, GroupBox, x10 y85 w650 h55, MD5 / MD5+Salt:
Gui, Add, Edit, x20 y105 w300 vMD5 ReadOnly Lowercase
Gui, Add, Edit, x350 y105 w300 vMD5Salt ReadOnly
Gui, Add, GroupBox, x10 y150 w650 h55, SHA1 / SHA1+Salt:
Gui, Add, Edit, x20 y170 w300 vSHA1	ReadOnly
Gui, Add, Edit, x350 y170 w300 vSHA1Salt ReadOnly
Gui, Add, GroupBox, x10 y215 w650 h55, SHA256 / SHA256+Salt:
Gui, Add, Edit, x20 y235 w300 vSHA256 ReadOnly
Gui, Add, Edit, x350 y235 w300 vSHA256Salt ReadOnly
Gui, Add, GroupBox, x10 y280 w650 h55, SHA384 / SHA384+Salt:
Gui, Add, Edit, x20 y300 w300 vSHA384 ReadOnly
Gui, Add, Edit, x350 y300 w300 vSHA384Salt ReadOnly
Gui, Add, GroupBox, x10 y345 w650 h55, SHA512 / SHA512+Salt:
Gui, Add, Edit, x20 y365 w300 vSHA512 ReadOnly
Gui, Add, Edit, x350 y365 w300 vSHA512Salt ReadOnly

UseStr:
	GuiControlGet, Str
	GuiControlGet, Str2
	Gui, Show
	GuiControl,,MD5, % Crypt.Hash.StrHash( Str, 1 )
	GuiControl,,MD5Salt, % Crypt.Hash.StrHash( Str, 1, Str2, 7 )
	GuiControl,,SHA1, % Crypt.Hash.StrHash( Str, 3 )
	GuiControl,,SHA1Salt, % Crypt.Hash.StrHash( Str, 3, Str2, 7 )
	GuiControl,,SHA256, % Crypt.Hash.StrHash( Str, 4 )
	GuiControl,,SHA256Salt, % Crypt.Hash.StrHash( Str, 4, Str2, 7 )
	GuiControl,,SHA384, % Crypt.Hash.StrHash( Str, 5 )
	GuiControl,,SHA384Salt, % Crypt.Hash.StrHash( Str, 5, Str2, 7 )
	GuiControl,,SHA512, % Crypt.Hash.StrHash( Str, 6 )
	GuiControl,,SHA512Salt, % Crypt.Hash.StrHash( Str, 6, Str2, 7 )
Return

GuiClose:
GuiEscape:
ExitApp
Errors:
01 <!-- m -->http://img5.fotos-ho... ... yr1ifk.jpg<!-- m -->
02 <!-- m -->http://img5.fotos-ho... ... i21uoy.jpg<!-- m -->
03 <!-- m -->http://img5.fotos-ho... ... x48jve.jpg<!-- m -->
04 <!-- m -->http://img5.fotos-ho... ... pv2tha.jpg<!-- m -->
05 <!-- m -->http://img5.fotos-ho... ... mbu2cg.jpg<!-- m -->
06 <!-- m -->http://img5.fotos-ho... ... 7yoam2.jpg<!-- m -->
07 <!-- m -->http://img5.fotos-ho... ... a1b8gz.jpg<!-- m -->
08 <!-- m -->http://img5.fotos-ho... ... unytov.jpg<!-- m -->
09 <!-- m -->http://img5.fotos-ho... ... h1zl82.jpg<!-- m -->
10 <!-- m -->http://img5.fotos-ho... ... 9vyl13.jpg<!-- m -->
11 <!-- m -->http://img5.fotos-ho... ... 3tupmw.jpg<!-- m -->
12 <!-- m -->http://img5.fotos-ho... ... zmifgl.jpg<!-- m -->
[...] 01-12, 01-12, ...
[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
it is because you use #Warn, you get messages about using unassigned variables which is not a problem actually
use this instead
#Warn All
#Warn UseUnsetLocal, Off


TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

..if you use password, HMAC hashing algorithm will be used

Yes this made sense from the point that Hash is a one way process :).
Thanks again Deo for your clarification and these pristine functions.
@jNizM, hope all is well now, and ty for helping me demystify crypto.

Posted Image

don't duplicate, iterate!


jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
Thx booth, TLM & Deo =)

need only one more help ;)

Gui, Add, Edit, [paste hash here] Verify
Gui, Add, Edit, [MD5 OK | SHA1 OK | SHA256 OK | ... ]
like this one from SKAN:
<!-- m -->http://dl.dropbox.co... ... ashish.ahk<!-- m -->
[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
so what the question is?

jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
how can i get this work for my script

#Include Crypt.ahk
#Include CryptConst.ahk
#Include CryptFoos.ahk
   
#SingleInstance, Force

Gui, Margin, 10, 10
Gui, Font, s10
Gui, Add, GroupBox, x10 y10 w650 h55, Text / Text+Salt
Gui, Add, Edit, x20 y30 w300 vStr gUseStr, AutoHotkey
Gui, Add, Edit, x350 y30 w300 vStr2 gUseStr, Salt
Gui, Add, GroupBox, x10 y85 w650 h55, MD5 / MD5+Salt:
Gui, Add, Edit, x20 y105 w300 vMD5 ReadOnly Lowercase
Gui, Add, Edit, x350 y105 w300 vMD5Salt ReadOnly
Gui, Add, GroupBox, x10 y150 w650 h55, SHA1 / SHA1+Salt:
Gui, Add, Edit, x20 y170 w300 vSHA1   ReadOnly
Gui, Add, Edit, x350 y170 w300 vSHA1Salt ReadOnly
Gui, Add, GroupBox, x10 y215 w650 h55, SHA256 / SHA256+Salt:
Gui, Add, Edit, x20 y235 w300 vSHA256 ReadOnly
Gui, Add, Edit, x350 y235 w300 vSHA256Salt ReadOnly
Gui, Add, GroupBox, x10 y280 w650 h55, SHA384 / SHA384+Salt:
Gui, Add, Edit, x20 y300 w300 vSHA384 ReadOnly
Gui, Add, Edit, x350 y300 w300 vSHA384Salt ReadOnly
Gui, Add, GroupBox, x10 y345 w650 h55, SHA512 / SHA512+Salt:
Gui, Add, Edit, x20 y365 w300 vSHA512 ReadOnly
Gui, Add, Edit, x350 y365 w300 vSHA512Salt ReadOnly

UseStr:
   GuiControlGet, Str
   GuiControlGet, Str2
   Gui, Show
   GuiControl,,MD5, % Crypt.Hash.StrHash( Str, 1 )
   GuiControl,,MD5Salt, % Crypt.Hash.StrHash( Str, 1, Str2, 7 )
   GuiControl,,SHA1, % Crypt.Hash.StrHash( Str, 3 )
   GuiControl,,SHA1Salt, % Crypt.Hash.StrHash( Str, 3, Str2, 7 )
   GuiControl,,SHA256, % Crypt.Hash.StrHash( Str, 4 )
   GuiControl,,SHA256Salt, % Crypt.Hash.StrHash( Str, 4, Str2, 7 )
   GuiControl,,SHA384, % Crypt.Hash.StrHash( Str, 5 )
   GuiControl,,SHA384Salt, % Crypt.Hash.StrHash( Str, 5, Str2, 7 )
   GuiControl,,SHA512, % Crypt.Hash.StrHash( Str, 6 )
   GuiControl,,SHA512Salt, % Crypt.Hash.StrHash( Str, 6, Str2, 7 )
Return

GuiClose:
GuiEscape:
ExitApp

[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

Deo
  • Members
  • 199 posts
  • Last active: Jan 31 2014 03:19 PM
  • Joined: 16 May 2010
you need a dinamic generation of the hashes based on entered string, am i right? or there something else?