Page 2 of 6

Re: CNG (Cryptography API: Next Generation)

Posted: 05 Oct 2016, 05:37
by jNizM
Update:
Added class for all hash functions

Re: CNG (Cryptography API: Next Generation)

Posted: 30 Nov 2016, 15:28
by Masonjar13
Any progress on encryption?

Re: CNG (Cryptography API: Next Generation)

Posted: 16 Dec 2016, 02:33
by jNizM
Hey Masonjar13...

just AES with CBC is finsihed atm.

Re: CNG (Cryptography API: Next Generation)

Posted: 16 Dec 2016, 03:09
by Masonjar13
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:

Re: CNG (Cryptography API: Next Generation)

Posted: 16 Dec 2016, 08:34
by guest3456
why are there separate implementations for win7/win10 ?

Re: CNG (Cryptography API: Next Generation)

Posted: 16 Dec 2016, 12:14
by Masonjar13
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.

Re: CNG (Cryptography API: Next Generation)

Posted: 08 Mar 2017, 05:17
by runie
Have you quit on this project?

Would love to see data encryption! :D

Re: CNG (Cryptography API: Next Generation)

Posted: 08 Mar 2017, 05:32
by jNizM
Since I got no time to finish this project atm I uploaded my first encryption function AES + CBC

Re: CNG (Cryptography API: Next Generation)

Posted: 11 Apr 2017, 05:45
by jNizM
Update:
- fixed files with bom (thx jeeswg for reporting and wolf_II for the hint)

Re: CNG (Cryptography API: Next Generation)

Posted: 17 Feb 2018, 12:08
by TheHacker
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.

Re: CNG (Cryptography API: Next Generation)

Posted: 17 Feb 2018, 12:46
by TheHacker
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.

Re: CNG (Cryptography API: Next Generation)

Posted: 17 Feb 2018, 14:56
by nnnik
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.

Re: CNG (Cryptography API: Next Generation)

Posted: 17 Feb 2018, 15:45
by TheHacker
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.

Re: CNG (Cryptography API: Next Generation)

Posted: 18 Feb 2018, 20:31
by TheHacker
OK, no luck. Sometimes it works, sometimes it does not. Don't know how to pinpoint it.

Re: CNG (Cryptography API: Next Generation)

Posted: 27 Sep 2018, 07:35
by TheHacker
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

Re: CNG (Cryptography API: Next Generation)

Posted: 12 Feb 2019, 03:53
by jNizM
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.

Re: CNG (Cryptography API: Next Generation)

Posted: 12 Feb 2019, 05:18
by TheHacker
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.

Re: CNG (Cryptography API: Next Generation)

Posted: 16 Feb 2019, 06:08
by just me
?

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*

Re: CNG (Cryptography API: Next Generation)

Posted: 20 Feb 2019, 14:26
by TheHacker
Will try, thank you!

Re: CNG (Cryptography API: Next Generation)

Posted: 20 Feb 2019, 17:37
by burque505
@just me: works fine :+1:
bcrypt.GIF
bcrypt.GIF (23.44 KiB) Viewed 6148 times