CNG (Cryptography API: Next Generation)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: CNG (Cryptography API: Next Generation)

05 Oct 2016, 05:37

Update:
Added class for all hash functions
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: CNG (Cryptography API: Next Generation)

30 Nov 2016, 15:28

Any progress on encryption?
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: CNG (Cryptography API: Next Generation)

16 Dec 2016, 02:33

Hey Masonjar13...

just AES with CBC is finsihed atm.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: CNG (Cryptography API: Next Generation)

16 Dec 2016, 03:09

Great! No pressure or anything, it had just been a while, curious if you were still working on it. I'm very much looking forward to using this in place of Crypt. I appreciate the update! :thumbup:
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: CNG (Cryptography API: Next Generation)

16 Dec 2016, 08:34

why are there separate implementations for win7/win10 ?

User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: CNG (Cryptography API: Next Generation)

16 Dec 2016, 12:14

guest3456 wrote:why are there separate implementations for win7/win10 ?
There is a function only present in Windows 10 that encapsulates several functions. That is, there is no difference, just a convenience.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
User avatar
runie
Posts: 304
Joined: 03 May 2014, 14:50
Contact:

Re: CNG (Cryptography API: Next Generation)

08 Mar 2017, 05:17

Have you quit on this project?

Would love to see data encryption! :D
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: CNG (Cryptography API: Next Generation)

08 Mar 2017, 05:32

Since I got no time to finish this project atm I uploaded my first encryption function AES + CBC
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: CNG (Cryptography API: Next Generation)

11 Apr 2017, 05:45

Update:
- fixed files with bom (thx jeeswg for reporting and wolf_II for the hint)
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
TheHacker
Posts: 29
Joined: 24 Apr 2017, 07:53

Re: CNG (Cryptography API: Next Generation)

17 Feb 2018, 12:08

Hi,
AHK 1.1.28.00 x64 Unicode, Windows 10 x64 v.1709.
The Windows 10 versions of the file hash functions give incorrect results if the file size is 262145 bytes or larger.
The Windows 7 versions of the file hash functions seem to give correct results regardless of file size.
Tested for MD5, SHA1, SHA256, SHA384, SHA512.
TheHacker
Posts: 29
Joined: 24 Apr 2017, 07:53

Re: CNG (Cryptography API: Next Generation)

17 Feb 2018, 12:46

Hi again,
AHK 1.1.28.00 x64 Unicode, Windows 10 x64 v.1709, CNG Win7 version.
I would like to create a hash of a part of a binary file. However, if said part contains characters (bytes) >= 0x80 I get incorrect results.

Code: Select all

File := FileOpen("TestFile", "rw")
File.WriteUChar(0x80)
File.Seek(0)
File.RawRead(Data, 1)
File.Close()
MsgBox,
( Join`s LTrim Comments
%
bcrypt_sha256(Data) . "`n" . 						; ea0add9a514e94de9abd0ba721eed15b15aa0b3a0f09ebee5e54a9dfae943153 <- Not OK
bcrypt_sha256(StrGet(&Data, "CP1252")) . "`n" .		; c4cc90ed3d26f12d4b08a75140970a7904035c31cbb4515a83f19b9003c00d1d <- Not OK
bcrypt_sha256(StrGet(&Data, "CP1250")) . "`n" .		; c4cc90ed3d26f12d4b08a75140970a7904035c31cbb4515a83f19b9003c00d1d <- Not OK 
bcrypt_sha256(StrGet(&Data, "CP0")) . "`n`n" .		; c4cc90ed3d26f12d4b08a75140970a7904035c31cbb4515a83f19b9003c00d1d <- Not OK

bcrypt_sha256_file("TestFile")						; 76be8b528d0075f7aae98d6fa57a6d3c83ae480a8469e668d7b0af968995ac71 <- OK, but uses the whole file instead of just a part
)
Any pointers on what I am doing wrong and how to fix it would be appreciated.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: CNG (Cryptography API: Next Generation)

17 Feb 2018, 14:56

You have to learn about character encoding then. 0x80 has a special meaning for unicode strings.
This library only seems to be able to handle strings unless it is a file.
Recommends AHK Studio
TheHacker
Posts: 29
Joined: 24 Apr 2017, 07:53

Re: CNG (Cryptography API: Next Generation)

17 Feb 2018, 15:45

Thanks for bringing me on the right track. It seems replacing

Code: Select all

VarSetCapacity(pbInput, StrPut(string, "UTF-8"), 0) && cbInput := StrPut(string, &pbInput, "UTF-8") - 1
with

Code: Select all

VarSetCapacity(pbInput, StrPut(string, "CP0"), 0) && cbInput := StrPut(string, &pbInput, "CP0") - 1
helps. Need to do some testing though.
Thanks again.
TheHacker
Posts: 29
Joined: 24 Apr 2017, 07:53

Re: CNG (Cryptography API: Next Generation)

18 Feb 2018, 20:31

OK, no luck. Sometimes it works, sometimes it does not. Don't know how to pinpoint it.
TheHacker
Posts: 29
Joined: 24 Apr 2017, 07:53

Re: CNG (Cryptography API: Next Generation)

27 Sep 2018, 07:35

The Windows 10 versions of the file hash functions give incorrect results if the file size is 262145 bytes or larger.
Just to give an example to anyone who'd like to confirm / reproduce:

Code: Select all

#Include class_bcrypt.ahk
#Include bcrypt_sha256_file.ahk

SetWorkingDir, %A_ScriptDir%

Loop, 262145
	TestString .= "a"

FileAppend, %TestString%, test.txt
MsgBox, % bcrypt.file("test.txt", "SHA256")		; ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb - INCORRECT, matches SHA256 of "a"
MsgBox, % bcrypt_sha256_file("test.txt")		; ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb - INCORRECT, matches SHA256 of "a"

; Correct SHA256:
; c592f4a6b099700b5050ce8bc67367f0c8f44810203124e86405c3e7b6f1a2ba

FileDelete, test.txt
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: CNG (Cryptography API: Next Generation)

12 Feb 2019, 03:53

After fixing the bug (win10 functions), I decided to remove the win10 functions from the lib.
Since BCryptHash performs a single hash computation, it will consume the full memory for string and file hashes.
So if the file is 4 GB, the ram usage will be also 4 gb for the hash calculation. Its also slower than the other one and not compatible on windows 7.

So please use the win7 function.
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
TheHacker
Posts: 29
Joined: 24 Apr 2017, 07:53

Re: CNG (Cryptography API: Next Generation)

12 Feb 2019, 05:18

Thank you very much for having looked into the bug! I will use the Windows 7 functions, then.
Any suggestions on how to create a hash of a part of a binary file, please?

Thank you.
just me
Posts: 9424
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: CNG (Cryptography API: Next Generation)

16 Feb 2019, 06:08

?

Code: Select all

#NoEnv
MsgBox % bcrypt_sha256_filepart("C:\Windows\notepad.exe")
ExitApp

bcrypt_sha256_filepart(filename, offset := 0, length := -1)
{
    static BCRYPT_SHA256_ALGORITHM := "SHA256"
    static BCRYPT_OBJECT_LENGTH    := "ObjectLength"
    static BCRYPT_HASH_LENGTH      := "HashDigestLength"

    if !(hBCRYPT := DllCall("LoadLibrary", "str", "bcrypt.dll", "ptr"))
        throw Exception("Failed to load bcrypt.dll", -1)
    if (NT_STATUS := DllCall("bcrypt\BCryptOpenAlgorithmProvider", "ptr*", hAlgo, "ptr", &BCRYPT_SHA256_ALGORITHM, "ptr", 0, "uint", 0) != 0)
        throw Exception("BCryptOpenAlgorithmProvider: " NT_STATUS, -1)
    if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlgo, "ptr", &BCRYPT_OBJECT_LENGTH, "uint*", cbHashObject, "uint", 4, "uint*", cbResult, "uint", 0) != 0)
        throw Exception("BCryptGetProperty: " NT_STATUS, -1)
    if (NT_STATUS := DllCall("bcrypt\BCryptGetProperty", "ptr", hAlgo, "ptr", &BCRYPT_HASH_LENGTH, "uint*", cbHash, "uint", 4, "uint*", cbResult, "uint", 0) != 0)
        throw Exception("BCryptGetProperty: " NT_STATUS, -1)
    VarSetCapacity(pbHashObject, cbHashObject, 0)
    if (NT_STATUS := DllCall("bcrypt\BCryptCreateHash", "ptr", hAlgo, "ptr*", hHash, "ptr", &pbHashObject, "uint", cbHashObject, "ptr", 0, "uint", 0, "uint", 0) != 0)
        throw Exception("BCryptCreateHash: " NT_STATUS, -1)

    if !(f := FileOpen(filename, "r", "UTF-8"))
        throw Exception("Failed to open file: " filename, -1)
    length := length < 0 ? f.length - offset : length
    if ((offset + length) > f.length)
        throw Exception("Invalid parameters offset / length!", -1)
    f.Pos(offset)
    while (length > 262144) && (dataread := f.RawRead(data, 262144))
    {
        if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0)
            throw Exception("BCryptHashData: " NT_STATUS, -1)
        length -= dataread
    }
    if (length > 0)
    {
        if (dataread := f.RawRead(data, length))
            if (NT_STATUS := DllCall("bcrypt\BCryptHashData", "ptr", hHash, "ptr", &data, "uint", dataread, "uint", 0) != 0)
                throw Exception("BCryptHashData: " NT_STATUS, -1)

    }
    f.Close()

    VarSetCapacity(pbHash, cbHash, 0)
    if (NT_STATUS := DllCall("bcrypt\BCryptFinishHash", "ptr", hHash, "ptr", &pbHash, "uint", cbHash, "uint", 0) != 0)
        throw Exception("BCryptFinishHash: " NT_STATUS, -1)
    loop % cbHash
        hash .= Format("{:02x}", NumGet(pbHash, A_Index - 1, "uchar"))
    DllCall("bcrypt\BCryptDestroyHash", "ptr", hHash)
    DllCall("bcrypt\BCryptCloseAlgorithmProvider", "ptr", hAlgo, "uint", 0)
    DllCall("FreeLibrary", "ptr", hBCRYPT)
    return hash
}
*not tested*
TheHacker
Posts: 29
Joined: 24 Apr 2017, 07:53

Re: CNG (Cryptography API: Next Generation)

20 Feb 2019, 14:26

Will try, thank you!
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: CNG (Cryptography API: Next Generation)

20 Feb 2019, 17:37

@just me: works fine :+1:
bcrypt.GIF
bcrypt.GIF (23.44 KiB) Viewed 6111 times

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 110 guests