[Function] - UnicodeCrypter() - Encrypt\Decrypt Unicode (Support All Characters from 1 to 1114111)

Post your working scripts, libraries and tools for AHK v1.1 and older
User
Posts: 407
Joined: 26 Jun 2017, 08:12

[Function] - UnicodeCrypter() - Encrypt\Decrypt Unicode (Support All Characters from 1 to 1114111)

22 Oct 2018, 00:23

A very simplistic function that Encrypt\Decrypt Unicode characters from 1 to 1114111 (Tested encryption\decryption with all the characters at once and no issues found)!

This function is very simple and clearly aimed for non-advanced users who just don't want to save their plain text data into files without any kind of encryption!

Image

Code: Select all

gui, add, text, , Unicode:
gui, add, edit, w400 h300 +HScroll gEncrypt +HwndEditIdU,
gui, add, text, , Encrypt Key =
gui, add, edit, x+5 w200 vEK gEncrypt, 0|A|6|5|8|E|2|C|1|F|B|D|4|3|9|7		;"0|A|6|5|8|E|2|C|1|F|B|D|4|3|9|7" is the function default key
gui, add, button, x+5 gEK, Generate

gui, add, text, ys, Encrypted:
gui, add, edit, w400 h300 +HScroll gDecrypt +HwndEditIdE,
gui, add, text, , Decrypt Key =
gui, add, edit, x+5 w200 vDK gDecrypt, 0|A|6|5|8|E|2|C|1|F|B|D|4|3|9|7		;"0|A|6|5|8|E|2|C|1|F|B|D|4|3|9|7" is the function default key
gui, add, button, x+5 gDK, Generate

gui, add, text, xm, Since Autohotkey does not support "null" characters, "0" must be the first Hex symbol in "key" in order to prevent issues!

gui, show


msgbox, % ""
. "∞abcd  =  " UnicodeCrypter("∞abcd")   "`r`n"
. ""
. "AutoHotKey  =  " UnicodeCrypter("AutoHotKey")   "`r`n"
. ""
. chr(65535) "  =  " UnicodeCrypter(chr(65535))       "`r`n"
. chr(65536) "  =  " UnicodeCrypter(chr(65536))       "`r`n"	;Unicode characters from 65536 to 1114111 are represented by pairs of 2 Unicode Surrogate Characters
. chr(70000) "  =  " UnicodeCrypter(chr(70000))       "`r`n"	;Unicode Surrogate characters starts from 55296 to 57343
. chr(1114111) "  =  " UnicodeCrypter(chr(1114111))   "`r`n"
. "`r`n"
. "-------------------- New Crypt key in use ------------------ `r`n"
. "`r`n"
. "∞abcd  =  " UnicodeCrypter("∞abcd", "Key(0|8|3|6|5|1|B|9|E|2|7|F|4|A|C|D)")   "`r`n"
. ""
. "AutoHotKey  =  " UnicodeCrypter("AutoHotKey", "Key(0|8|3|6|5|1|B|9|E|2|7|F|4|A|C|D)")   "`r`n"
. ""
. chr(65535) "  =  " UnicodeCrypter(chr(65535), "Key(0|8|3|6|5|1|B|9|E|2|7|F|4|A|C|D)")       "`r`n"
. chr(65536) "  =  " UnicodeCrypter(chr(65536), "Key(0|8|3|6|5|1|B|9|E|2|7|F|4|A|C|D)")       "`r`n"	;Unicode characters from 65536 to 1114111 are represented by pairs of 2 Unicode Surrogate Characters
. chr(70000) "  =  " UnicodeCrypter(chr(70000), "Key(0|8|3|6|5|1|B|9|E|2|7|F|4|A|C|D)")       "`r`n"	;Unicode Surrogate characters starts from 55296 to 57343
. chr(1114111) "  =  " UnicodeCrypter(chr(1114111), "Key(0|8|3|6|5|1|B|9|E|2|7|F|4|A|C|D)")   "`r`n"

return

Encrypt:	;_____________ Encrypt _______________

guicontrol, -g, % EditIdE
ControlSetText , , Please Wait!, % "ahk_id" EditIdE

ControlGetText, InputString, , % "ahk_id" EditIdU

guicontrolget, EK

InputString := UnicodeCrypter(InputString, "Key(" EK ")")

ControlSetText , , % InputString, % "ahk_id" EditIdE
guicontrol, +gDecrypt, % EditIdE

return

Decrypt:	;_____________ Decrypt _______________

guicontrol, -g, % EditIdU
ControlSetText , , Please Wait!, % "ahk_id" EditIdU

ControlGetText, InputString, , % "ahk_id" EditIdE

guicontrolget, DK

InputString := UnicodeCrypter(InputString, "Decrypt Key(" DK ")")

ControlSetText , , % InputString, % "ahk_id" EditIdU
guicontrol, +gEncrypt, % EditIdU

return

EK:	;_________________ Generate New Encryption Key ______________________

guicontrol, , EK, % UnicodeCrypter(,"NewKey")

return

DK:	;_________________ Generate New Decryption Key ______________________

guicontrol, , DK, % UnicodeCrypter(,"NewKey")

return

guiclose:	;_____________ gui close _____________
exitapp


UnicodeCrypter(String := "", Options := "")	;__________________ UnicodeCrypter(Function) - v1.0 ________________
{
Static Defaultkey := "0|A|6|5|8|E|2|C|1|F|B|D|4|3|9|7"		;Default Encrypt\Decrypt Key
;since Autohotkey does not support "null" characters, "0" must be placed first to prevent problems!

	if (Options = "NewKey")
	{
	NewKey := RegExReplace(Defaultkey, "^..")	;remove "0|"

	Sort, NewKey, D| Random		;"D|" means Delimiter = | , "Random" for random sort! 

	return, "0|" NewKey		;since Autohotkey does not support "null" characters, "0" must be placed first to prevent problems!
	}

if RegExMatch(Options, "i)Key\((.*?)\)", Match)		;"i)" enable regex case insensitive
Key := Match1
else
key := Defaultkey

k := StrSplit(Key, "|")		;"k[]" array is created

if !RegExMatch(Options, "i)Decrypt")
c := {0:k[1], 1:k[2], 2:k[3], 3:k[4], 4:k[5], 5:k[6], 6:k[7], 7:k[8], 8:k[9], 9:k[10], A:k[11], B:k[12], C:k[13], D:k[14], E:k[15], F:k[16]}
else
c := {k[1]:0, k[2]:1, k[3]:2, k[4]:3, k[5]:4, k[6]:5, k[7]:6, k[8]:7, k[9]:8, k[10]:9, k[11]:"A", k[12]:"B", k[13]:"C", k[14]:"D", k[15]:"E", k[16]:"F"}

	loop
	{
	Char := SubStr(String, a_index, 1)	;"a_index" = pos, "1" = Length

		if (Char == "")		;"==" is case sensitive
		return, Crypted
		else
		{
		CharHex := Format("{1:X}", ord(Char))
		;x or X, Unsigned hexadecimal integer; "x" for "abcdef" format or "X" for "ABCDEF" format.
		;The "0x" prefix is not included unless the "#" flag is used, example: {1:#x}

			CharHexC := ""
			loop
			{
			HexS := SubStr(CharHex, a_index, 1)	;"a_index" = pos, "1" = Length

			if (HexS == "")		;"==" is case sensitive
			break
			else
			CharHexC .= c[HexS]
			}

		Crypted .= chr("0x" CharHexC)
		}

	}


}

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Epoch and 56 guests