nnnik's Encrypt/Decrypt V2.1.0

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

nnnik's Encrypt/Decrypt V2.1.0

22 Dec 2013, 15:12

Here are some functions to encrypt and decrypt strings.
I'll add support for other things later (binaries and files)

Here is the code.

Code: Select all

encryptStr(str="",pass="")
{
If !(enclen:=(strput(str,"utf-16")*2))
    return "Error: Nothing to Encrypt"
If !(passlen:=strput(pass,"utf-8")-1)
    return "Error: No Pass"
enclen:=mod(enclen,4) ? (enclen) : (enclen-2)
Varsetcapacity(encbin,enclen,0)
strput(str,&encbin,enclen/2,"utf-16")
Varsetcapacity(passbin,passlen+=mod((4-mod(passlen,4)),4),0)
strput(pass,&passbin,strlen(pass),"utf-8")
_encryptbin(&encbin,enclen,&passbin,passlen)
return _crypttobase64(&encbin,enclen)
}

decryptStr(str="",pass="")
{
If !((strput(str,"utf-16")*2))
    return "Error: Nothing to Decrypt"
If !((passlen:=strput(pass,"utf-8")-1))
    return "Error: No Pass"
Varsetcapacity(passbin,passlen+=mod((4-mod(passlen,4)),4),0)
strput(pass,&passbin,strlen(pass),"utf-8")
enclen:=_cryptfrombase64(str,encbin)
_decryptbin(&encbin,enclen,&passbin,passlen)
return strget(&encbin,"utf-16")
}

_MCode(mcode)
{
  static e := {1:4, 2:1}, c := (A_PtrSize=8) ? "x64" : "x86"
  if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
    return
  if (!DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", 0, "uint*", s, "ptr", 0, "ptr", 0))
    return
  p := DllCall("GlobalAlloc", "uint", 0, "ptr", s, "ptr")
  if (c="x64")
    DllCall("VirtualProtect", "ptr", p, "ptr", s, "uint", 0x40, "uint*", op)
  if (DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", p, "uint*", s, "ptr", 0, "ptr", 0))
    return p
  DllCall("GlobalFree", "ptr", p)
}

_encryptbin(bin1pointer,bin1len,bin2pointer,bin2len){
  static encrypt := _MCode("2,x86:U1VWV4t0JBCLTCQUuAAAAAABzoPuBIsWAcKJFinCAdAPr8KD6QR164tsJByLfCQYi3QkEItMJBSLH7gAAAAAixYBwjHaiRYx2inCAdAPr8KDxgSD6QR154PHBIPtBHXQuAAAAABfXl1bww==,x64:U1ZJicpJidNMidZMidlIAc64AAAAAEiD7gSLFgHCiRYpwgHQD6/CSIPpBHXpuAAAAABBixhMidZMidmLFgHCMdqJFjHaKcIB0A+vwkiDxgRIg+kEdeVJg8AESYPpBHXbuAAAAABeW8M=") ;reserved
b:=0
Loop % bin1len/4
{
a:=numget(bin1pointer+0,bin1len-A_Index*4,"uint")
numput(a+b,bin1pointer+0,bin1len-A_Index*4,"uint")
b:=(a+b)*a
}
Loop % bin2len/4
{
c:=numget(bin2pointer+0,(A_Index-1)*4,"uint")
b:=0
Loop % bin1len/4
{
a:=numget(bin1pointer+0,(A_Index-1)*4,"uint")
numput((a+b)^c,bin1pointer+0,(A_Index-1)*4,"uint")
b:=(a+b)*a
}
}
}

_decryptbin(bin1pointer,bin1len,bin2pointer,bin2len){
  static decrypt := _MCode("2,x86:U1VWV4tsJByLfCQYAe+D7wSLH7gAAAAAi3QkEItMJBSLFjHaKcKJFgHQD6/Cg8YEg+kEdeuD7QR11LgAAAAAi3QkEItMJBQBzoPuBIsWKcKJFgHQD6/Cg+kEde24AAAAAF9eXVvD,x64:U1ZJicpJidNNAchJg+gEuAAAAABBixhMidZMidmLFjHaKcKJFgHQD6/CSIPGBEiD6QR16UmD6QR140yJ1kyJ2UgBzrgAAAAASIPuBIsWKcKJFgHQD6/CSIPpBHXruAAAAABeW8M=") ;reserved

Loop % bin2len/4
{
c:=numget(bin2pointer+0,bin2len-A_Index*4,"uint")
b:=0
Loop % bin1len/4
{
a:=numget(bin1pointer+0,(A_Index-1)*4,"uint")
numput(a:=(a^c)-b,bin1pointer+0,(A_Index-1)*4,"uint")
b:=(a+b)*a
}
}
b:=0
Loop % bin1len/4
{
a:=numget(bin1pointer+0,bin1len-A_Index*4,"uint")
numput(a:=a-b,bin1pointer+0,bin1len-A_Index*4,"uint")
b:=(a+b)*a
}
}

_crypttobase64(binpointer,binlen)
{
    s:=0
    DllCall("crypt32\CryptBinaryToStringW","ptr",binpointer,"uint",binlen,"uint",1,"ptr",   0,"uint*",s)
    VarSetCapacity(out,s*2,0)
    DllCall("crypt32\CryptBinaryToStringW","ptr",binpointer,"uint",binlen,"uint",1,"ptr",&out,"uint*",s)
    return strget(&out,"utf-16")
}

_cryptfrombase64(string,byref bin)
{
    DllCall("crypt32\CryptStringToBinaryW", "wstr",string,"uint",0,"uint",1,"ptr",0,"uint*",s,"ptr",0,"ptr",0)
    VarSetCapacity(bin,s,0)
    DllCall("crypt32\CryptStringToBinaryW", "wstr",string,"uint",0,"uint",1,"ptr",&bin,"uint*",s,"ptr",0,"ptr",0)
    return s
}
Here is a Test-Script:

Code: Select all

c:=encryptstr("Hallo Welt!","ffff")
Msgbox % decryptstr(c,"ffff")
I'm gonna give the sourcecode of the MCode functions to anybody that contacts me.
(It's not like you can decrypt it easily without pass if you know the code, I'm just still working on the right license.)

I'd like to have some feedback. :D
Log:
Spoiler
Recommends AHK Studio
HotKeyIt
Posts: 2364
Joined: 29 Sep 2013, 18:35
Contact:

Re: nnnik's Encrypt/Decrypt V2

22 Dec 2013, 15:30

You can init static vars so no need to check them and function will run faster.

Code: Select all

_encryptbin(bin1pointer,bin1len,bin2pointer,bin2len){
  static encrypt := MCode("2,x86:VVZXi3QkEItMJBS4AAAAAAHOg+4EixYBwokWKcIB0A+vwoPpBHXri2wkHIt8JBiLdCQQi0wkFIsfuAAAAACLFlIBwjHaiRZaAdAPr8KDxgSD6QR16YPHBIPtBHXSuAAAAABfXl3D")
  return DllCall(encrypt,"ptr",bin1pointer,"uint",bin1len,"ptr",bin2pointer,"uint",bin2len,"cdecl") ;returns 0 always
}

_decryptbin(bin1pointer,bin1len,bin2pointer,bin2len){
  static decrypt := MCode("2,x86:VVZXi2wkHIt8JBgB74PvBIsfuAAAAACLdCQQi0wkFIsWMdopwokWAdAPr8KDxgSD6QR164PtBHXUuAAAAACLdCQQi0wkFAHOg+4EixYpwokWAdAPr8KD6QR17bgAAAAAX15dww==") 
  return DllCall(decrypt,"ptr",bin1pointer,"uint",bin1len,"ptr",bin2pointer,"uint",bin2len,"cdecl") ;returns 0 always
}
You can PM me the code, I am working on encryption for compiling in AutoHotkey_H, probably it will be useful.
User avatar
oldbrother
Posts: 275
Joined: 23 Oct 2013, 05:08

Re: nnnik's Encrypt/Decrypt V2

25 Dec 2013, 07:09

Please send me the M-code. "Mail deleted to prevent Spam"
Thank you!
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: nnnik's Encrypt/Decrypt V2.0.2

26 Dec 2013, 07:34

V 2.0.2
-Minor Bug Fix that can occur in other versions of AHK
-added _MCode function so no further includes are needed

V2.0.3
-enhance the size estimation of StrEncrypt/Decrypt.

V2.1
-work on the real 64 bit Code

Todo:


V2.2
-add functions to encrypt/decrypt files,bynaries.
Recommends AHK Studio
User avatar
cyruz
Posts: 348
Joined: 30 Sep 2013, 13:31

Re: nnnik's Encrypt/Decrypt V2.1.0

27 Dec 2013, 09:38

Hello nnnik, may you send me the source on pvt? I'm just really curious :D
ABCza on the old forum.
My GitHub.
Joe
Posts: 29
Joined: 02 Oct 2013, 04:21

Re: nnnik's Encrypt/Decrypt V2.1.0

28 Dec 2013, 05:36

It seems as if the encryption isn't affected by the password.

Code: Select all

Msgbox % (encryptstr("Hallo Welt!","ffff") == encryptstr("Hallo Welt!","xyz"))  ; 1
Decryption works with any password here.

Apart from that it looks interesting.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: nnnik's Encrypt/Decrypt V2.1.0

28 Dec 2013, 06:17

Maybe I have mixed up some code versions. :(
But fck sh1t Ill try to do it later.
For now i have simply translated it to AHK Code.
Recommends AHK Studio

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 246 guests