Jump to content

Recommended Posts

Posted

yes, I'm want to:

Quote

store the public key blob in a variable or constant within you script and use it to encrypt data

 

I'm tryed to find internal function by this keyword __CryptoNG_BcryptImportKeyPair

but can't find...

Posted
16 minutes ago, kroman82 said:

sorry, I'm found that...

...
Func __CryptoNG_BcryptImportKeyPair($hAlgorithmProvider, $sKeyBlobFile, $sKeyBlobType, $xKeyBlob = Binary(""))

    If $__gbDebugging Then _DebugOut(@CRLF & "Function: __CryptoNG_BcryptImportKeyPair()")

    Local $aResult[0]

    Local $iError = 0, _
            $iStatusCode = 0

    Local $tBuffer = ""

    Local $hFile = -1, _
            $hKey = -1

;~  Local $xKeyBlob = Binary("")
    If $xKeyBlob = Binary("") Then


        ;Make sure file exists
        If Not FileExists($sKeyBlobFile) Then Return SetError(3, 0, "")


        ;Read binary blob file
        $hFile = FileOpen($sKeyBlobFile, $FO_BINARY)
        If $hFile = -1 Then Return SetError(4, 0, "")

        $xKeyBlob = FileRead($hFile)
        FileClose($hFile)

    EndIf
...

Placing $xKeyBlob as a parameter I think would work nicely :)

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

..then again:

...
Func __CryptoNG_BcryptImportKeyPair($hAlgorithmProvider, $sKeyBlobFile, $sKeyBlobType)

    If $__gbDebugging Then _DebugOut(@CRLF & "Function: __CryptoNG_BcryptImportKeyPair()")

    Local $aResult[0]

    Local $iError = 0, _
            $iStatusCode = 0

    Local $tBuffer = ""

    Local $hFile = -1, _
            $hKey = -1


    Local $xKeyBlob = Binary("")
    If StringLeft($sKeyBlobFile, 3) = "ECS" Or StringLeft($sKeyBlobFile, 3) = "RSA" Then
        $xKeyBlob = $sKeyBlobFile
    Else

        ;Make sure file exists
        If Not FileExists($sKeyBlobFile) Then Return SetError(3, 0, "")


        ;Read binary blob file
        $hFile = FileOpen($sKeyBlobFile, $FO_BINARY)
        If $hFile = -1 Then Return SetError(4, 0, "")

        $xKeyBlob = FileRead($hFile)
        FileClose($hFile)

    EndIf
...

these blob files start with a header that is ECS or RSA and no filename will start as such. This idea will be easier to implement script wide.

Am just looking at it. Never used the UDF yet. Just sharing ideas.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted (edited)

ok, here is my running code/idea:  <removed>

I will remove this file in a few days to save in forum file space. I hope this is all you are after @kroman82
( I worked on it because I've got things to do but I don't wanna do 'em but am anxious 🤪 )

Edit: removed zip.
New v2.4.0 can now accept the public/private key blob as either a string containing the file path to the key blob file or as binary data containing the key blob itself.

Edited by argumentum
removed zip file

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

I use both Crypt.au3 and CryptoNG.au3 and often operate conversion and creation and non-file usage. I would like to share some codes that I have improved or generated, hoping to be helpful to you.

 

#include "..\CryptoNG.au3"

#include <bignum.au3>
#include <string.au3>
#include <array.au3>

 

    Local $c_data = FileOpenDialog("Select a license data", @ScriptDir & "\", "c data (*.*)", 1)
    If @error Then
        MsgBox(4096,"","No File(s) choose")
        Exit
    EndIf

     Local $hFileOpen = FileOpen($c_data, 0)
     Local $c = FileRead($hFileOpen)

$Private_c =_WinAPI_Base64Decode($c)

$n_binary = Binary("0xput in data")

$Public_key = Create_to_Blob(@ScriptDir&"\Test.blob","public", 2048, 65537, $n_binary, 0x0, 0x0, 0, 0 )

$m = _CryptoNG_RSA_EncryptData($Private_c , $Public_key, $CNG_BCRYPT_PAD_NONE, Default, "binary" )

 

 

;Read_to_Blob(@ScriptDir&"\example_legacy_privatekey.blob", "Private", "Legacy")
;Read_to_Blob(@ScriptDir&"\example_legacy_publickey.blob", "Public", "Legacy")

;Read_to_Blob(@ScriptDir&"\example_rsa_privatekey.blob","Private")
;Read_to_Blob(@ScriptDir&"\example_rsa_publickey.blob","public")

 

;Create_to_Blob(@ScriptDir&"\Test20250227.blob","public",2048,65537,Binary("0x" & _BigHex_Swap_endian("092E57E9A31BB37F89222D15A2449594A4ED5705BF395A4469218881CA8C8A27437F7CE2B1ADEF8D93AE19BF381489DD7D06ED674FF945BF932F3FD81CEDB875F8A677CD4A4468A5DF249B9C0611F5E31DE60DBA86BCB69C38D0EED3931A61E379136E52FB7D39B30B5FA29CA5205D8B6A8101E666779EF3D99B196B4F54AA43F682B53FA5BE8986A7086D4C75975DCB22640DD8ACFAEBA7C8D72058AC997F4EB72F824FD45C55C29C0834AB0D629B56F191A4BB16CDA0694141DC64DAACEF8F923FDC8F3734FA5CE59E7034C2D9237358084259D10134C0A13C2098847630B656179B1F7DB61674D1B705A10E41A7F215D0D865A46E6BC2850642BA413C55C5")),Binary("0x" & _BigHex_Swap_endian("C7C6000BB7C3E10D0440AB2BE592D5F0A7D7899A721A256C0554CF3989D77C1AC1BCC27F9CF4884033A16D0CA8B478A9B2DCB92B6A5993AD3E303D971F1539C8AD306697BA08B340EEAD98D4C462B5E77DF9615F0DA5ED5DFA5B6167AD19D6C6A06AAD1ED71E8E1B7A99F9F3E3838DE7F51F0B2C5AE3D2B784C55A29AAB8A8CF")),Binary("0x" & _BigHex_Swap_endian("AF54BA9A2567F5AD9BC29158786F9E7264DDB487E8750B1E2455121383CE487EA2F1E43842D42864830063FCD906B521F6DE05CACBD8CC5886486DFA51DFB6D404F5168F26B5508F6F71D937C27A0EF90300A60B9293AB00B3D2917C65D2B7AFDC5635F0AC721622307DF3D846F501CA1125D567D4178929CFF93281202345F3")),0,1,"Legacy")
;Create_to_Blob(@ScriptDir&"\Test20250227.blob","PRIVATE",2048,65537,Binary("0x" & _BigHex_Swap_endian("092E57E9A31BB37F89222D15A2449594A4ED5705BF395A4469218881CA8C8A27437F7CE2B1ADEF8D93AE19BF381489DD7D06ED674FF945BF932F3FD81CEDB875F8A677CD4A4468A5DF249B9C0611F5E31DE60DBA86BCB69C38D0EED3931A61E379136E52FB7D39B30B5FA29CA5205D8B6A8101E666779EF3D99B196B4F54AA43F682B53FA5BE8986A7086D4C75975DCB22640DD8ACFAEBA7C8D72058AC997F4EB72F824FD45C55C29C0834AB0D629B56F191A4BB16CDA0694141DC64DAACEF8F923FDC8F3734FA5CE59E7034C2D9237358084259D10134C0A13C2098847630B656179B1F7DB61674D1B705A10E41A7F215D0D865A46E6BC2850642BA413C55C5")),Binary("0x" & _BigHex_Swap_endian("C7C6000BB7C3E10D0440AB2BE592D5F0A7D7899A721A256C0554CF3989D77C1AC1BCC27F9CF4884033A16D0CA8B478A9B2DCB92B6A5993AD3E303D971F1539C8AD306697BA08B340EEAD98D4C462B5E77DF9615F0DA5ED5DFA5B6167AD19D6C6A06AAD1ED71E8E1B7A99F9F3E3838DE7F51F0B2C5AE3D2B784C55A29AAB8A8CF")),Binary("0x" & _BigHex_Swap_endian("AF54BA9A2567F5AD9BC29158786F9E7264DDB487E8750B1E2455121383CE487EA2F1E43842D42864830063FCD906B521F6DE05CACBD8CC5886486DFA51DFB6D404F5168F26B5508F6F71D937C27A0EF90300A60B9293AB00B3D2917C65D2B7AFDC5635F0AC721622307DF3D846F501CA1125D567D4178929CFF93281202345F3")),0,1,"Legacy")
 

Global Const $tagRSAPUBKEY = _
                "align 1;" & _
                "char magic[4];" & _
                "uint bitlen;" & _
                "uint PubExpLength;" & _
                "uint ModulusLength;" & _
                "uint PLength;" & _
                "uint QLength;" & _
                "byte PubExp[3];" & _
                "byte Modulus[1];"

Global Const $tagRSAPRIVATEKEY = _
                "align 1;" & _
                "char magic[4];" & _
                "uint bitlen;" & _
                "uint PubExpLength;" & _
                "uint ModulusLength;" & _
                "uint PLength;" & _
                "uint QLength;" & _
                "byte PubExp[3];" & _
                "byte Modulus[1];" & _
                "byte P[1];" & _
                "byte q[1];"

Global Const $tagRSAPRIVATEKEY_Legacy = _
                "byte bType[1];" & _    
                "byte bVersion[1];" & _ 
                "word reserved[1];" & _
                "byte aiKeyAlg[4];" & _ 
                "char magic[4];" & _
                "uint bitlen;" & _
                "byte PubExp[4];" & _
                "byte Modulus[1];" & _
                "byte p[1];" & _
                "byte q[1];" & _
                "byte exponent1[1];" & _ 
                "byte exponent2[1];" & _ 
                "byte coefficient[1];" & _ 
                "byte privateExponent[1];" 

Global Const $tagRSAPUBKEY_Legacy = _
                "byte bType[1];" & _    
                "byte bVersion[1];" & _
                "word reserved[1];" & _
                "byte aiKeyAlg[4];" & _ 
                "char magic[4];" & _
                "uint bitlen;" & _
                "byte PubExp[4];" & _
                "byte Modulus[1];"

Func _BigNum_inverse($a, $b)
        $result = 0
    Local $array[0][2], $cc = 1

    While 1
        If Mod($cc, 2) = 1 Then
            _ArrayAdd($array, $b & "|" & $a)
        Else
            _ArrayAdd($array, $a & "|" & $b)
        EndIf
        $cc += 1

        $result = _BigNum_Mod($a, $b)
                If $result <> 0 Then
            $a = $b
            $b = $result
        Else            
            ExitLoop
        EndIf

    WEnd

    $cc = 1
    Local $d = "", $k = ""
    For $i = UBound($array) - 1 To 0 Step -1

        If $cc = 1 Then
            If $array[$i][0] = 1 Then
                $d = 1
            Else
                $d = 1
                $k = $array[$i][0] - 1
            EndIf
        EndIf

        If $cc <> 1 Then
            If $k = "" Then
                $k = _BigNum_Div((_BigNum_Sub(_BigNum_Mul($array[$i][0], $d), 1)), $array[$i][1])
            Else
                $d = _BigNum_Div((_BigNum_Add(_BigNum_Mul($array[$i][1], $k), 1)), $array[$i][0])
                $k = ""
            EndIf
        EndIf

        $cc += 1

    Next

    Return $d
EndFunc  

Func _BigHex_Swap_endian($data)
        $result = ""

        if IsBinary($data) Then
        $data = Hex($data,BinaryLen($data))
        EndIf

        if Mod(StringLen($data),2) <> 0 Then
        $data = "0" & $data
        EndIf

        for $i = 1 to StringLen($data) step 2
        $result = StringMid($data,$i,2) & $result
        Next

        Return $result

EndFunc


Func _BigNum_ToBase2($iDec, $base = 16 , $checkfirstzero = 0)
    $base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;base64
    ;Sexagesimal
    $base60_2 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwx" ;sexagesimal
    $base32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" ;base32
    $base24 = "0123456789ABCDEFGHJKLMNP" ;base24
    $base16 = "0123456789ABCDEF" ;hex
    ;Duodecimal system or dozenal
    $base12 = "0123456789AB" ;duodecimal
    $base10 = "0123456789" ;base10
    $base8 = "01234567" ;oct
    $base2 = "01" ;binary

    If $base = 2 Then
        $Symbol = $base2
    ElseIf $base = 8 Then
        $Symbol = $base8
    ElseIf $base = 10 Then
        $Symbol = $base10
    ElseIf $base = 16 Then
        $Symbol = $base16
    ElseIf $base = 60 Then
        $Symbol = $base60_2
    ElseIf $base = 64 Then
        $Symbol = $base64
    EndIf


    Local $Out, $ost
    $Symbol = StringSplit($Symbol, '')
    If @error Or $Symbol[0] < 2 Then Return SetError(1, 0, $iDec)
    Do
        $ost = _BigNum_Mod($iDec, $Symbol[0])
        $iDec = _BigNum_Div(_BigNum_Sub($iDec, $ost), $Symbol[0])
        $Out = $Symbol[$ost + 1] & $Out
    Until Not Number($iDec)
    if $checkfirstzero = 1 Then
        if Mod(StringLen($Out),2) <> 0 Then
            $Out = "0" & $Out
        EndIf
    EndIf
    Return SetError(0, $Symbol[0], $Out)
EndFunc


Func _BigHex_ToBase2($sDecimal, $iBase)
    Local $vDigits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!""#$%&'()*+,-./:;<=>?@[\]^_`{|}~" ; 96 ASCII symbols, use Unicode for more  94個字元
    If $iBase > StringLen($vDigits) Then SetError(1, 0, 0)
    $vDigits = StringSplit($vDigits, '', 2)
    Local $sResult = 0
    For $i = 1 To StringLen($sDecimal)
        $nn = _ArraySearch($vDigits, StringMid($sDecimal, $i, 1))
        $sResult = _BigNum_Add(_BigNum_Mul($iBase, $sResult), $nn)
    Next
    Return $sResult
EndFunc





Func Read_to_Blob($key_path ,$tag = "Public", $tag2 = "new" , $type = "File" , $showdata = 1)
Local $returndata[2]

if $type <> "File" Then
$blob_b = $key_path
Else
Local $hFile = FileOpen($key_path,$FO_BINARY)
Local $blob_b = FileRead($hFile)
FileClose($hFile)
EndIf

Local $blob_handle = DllStructCreate("byte a["&BinaryLen($blob_b)&"];")
$blob_handle.a = $blob_b
if $tag2 = "new" Then
    if $tag = "Public" Then
        Local $blob_data = DllStructCreate($tagRSAPUBKEY,DllStructGetPtr($blob_handle))
        Local $tagRSAPUBKEY2 = StringReplace($tagRSAPUBKEY,"Modulus[1]","Modulus["&$blob_data.bitlen/8&"]")
        $blob_data = DllStructCreate($tagRSAPUBKEY2,DllStructGetPtr($blob_handle))
        

        $returndata[1] = $tagRSAPUBKEY2

        if $showdata = 1 Then
        _WinAPI_DisplayStruct($blob_data,$tagRSAPUBKEY2)
        EndIf
    Else
        Local $blob_data = DllStructCreate($tagRSAPRIVATEKEY,DllStructGetPtr($blob_handle))
        Local $tagRSAPRIVATEKEY2 = StringReplace($tagRSAPRIVATEKEY,"Modulus[1]","Modulus["&$blob_data.bitlen/8&"]")
        $tagRSAPRIVATEKEY2 = StringReplace($tagRSAPRIVATEKEY2,"p[1]","p["&$blob_data.bitlen/8/2&"]")
        $tagRSAPRIVATEKEY2 = StringReplace($tagRSAPRIVATEKEY2,"q[1]","q["&$blob_data.bitlen/8/2&"]")
        $blob_data = DllStructCreate($tagRSAPRIVATEKEY2,DllStructGetPtr($blob_handle))
        

        $returndata[1] = $tagRSAPRIVATEKEY2

        if $showdata = 1 Then
        _WinAPI_DisplayStruct($blob_data,$tagRSAPRIVATEKEY2)
        EndIf
    EndIf
Else
    if $tag = "Public" Then
        Local $blob_data = DllStructCreate($tagRSAPUBKEY_Legacy,DllStructGetPtr($blob_handle))
        Local $tagRSAPUBKEY2_Legacy = StringReplace($tagRSAPUBKEY_Legacy,"Modulus[1]","Modulus["&$blob_data.bitlen/8&"]")
        $blob_data = DllStructCreate($tagRSAPUBKEY2_Legacy,DllStructGetPtr($blob_handle))
        

        $returndata[1] = $tagRSAPUBKEY2_Legacy

        if $showdata = 1 Then
        _WinAPI_DisplayStruct($blob_data,$tagRSAPUBKEY2_Legacy)
        EndIf
    Else
        Local $blob_data = DllStructCreate($tagRSAPRIVATEKEY_Legacy,DllStructGetPtr($blob_handle))
        Local $tagRSAPRIVATEKEY2_Legacy = StringReplace($tagRSAPRIVATEKEY_Legacy,"Modulus[1]","Modulus["&$blob_data.bitlen/8&"]")
        $tagRSAPRIVATEKEY2_Legacy = StringReplace($tagRSAPRIVATEKEY2_Legacy,"p[1]","p["&$blob_data.bitlen/8/2&"]")
        $tagRSAPRIVATEKEY2_Legacy = StringReplace($tagRSAPRIVATEKEY2_Legacy,"q[1]","q["&$blob_data.bitlen/8/2&"]")
        $tagRSAPRIVATEKEY2_Legacy = StringReplace($tagRSAPRIVATEKEY2_Legacy,"exponent1[1]","exponent1["&$blob_data.bitlen/8/2&"]")
        $tagRSAPRIVATEKEY2_Legacy = StringReplace($tagRSAPRIVATEKEY2_Legacy,"exponent2[1]","exponent2["&$blob_data.bitlen/8/2&"]")
        $tagRSAPRIVATEKEY2_Legacy = StringReplace($tagRSAPRIVATEKEY2_Legacy,"coefficient[1]","coefficient["&$blob_data.bitlen/8/2&"]")
        $tagRSAPRIVATEKEY2_Legacy = StringReplace($tagRSAPRIVATEKEY2_Legacy,"privateExponent[1]","privateExponent["&$blob_data.bitlen/8&"]")
        $blob_data = DllStructCreate($tagRSAPRIVATEKEY2_Legacy,DllStructGetPtr($blob_handle))
        

        $returndata[1] = $tagRSAPRIVATEKEY2_Legacy

        if $showdata = 1 Then
        _WinAPI_DisplayStruct($blob_data,$tagRSAPRIVATEKEY2_Legacy)
        EndIf

        
    EndIf
EndIf

$returndata[0] = $blob_data
Return $returndata

EndFunc


Func Create_to_Blob($key_path,$tag = "Public" , $bitlen = 2048, $e = 65537, $Modulus = Binary("0x00"), $p = Binary("0x00"), $q = Binary("0x00") ,$write_or_binary = 1, $show = 0, $tag2 = "new")

if $tag2 = "new" Then
    if $tag = "Public" Then
        Local $tagRSAPUBKEY2 = StringReplace($tagRSAPUBKEY,"Modulus[1]","Modulus["&$bitlen/8&"]")
        Local $blob_data = DllStructCreate($tagRSAPUBKEY2)
        $blob_data.magic = "RSA1" 
        $blob_data.bitlen = $bitlen 
        $blob_data.PubExpLength = 3 
        $blob_data.ModulusLength = $blob_data.bitlen/8 
        $blob_data.PLength = 0 
        $blob_data.QLength = 0 
        $blob_data.PubExp = $e 
        $blob_data.Modulus = $Modulus 

        if $show = 1 Then
            _WinAPI_DisplayStruct($blob_data,$tagRSAPUBKEY2)
        EndIf

    Else
        Local $tagRSAPRIVATEKEY2 = StringReplace($tagRSAPRIVATEKEY,"Modulus[1]","Modulus["&$bitlen/8&"]")
        $tagRSAPRIVATEKEY2 = StringReplace($tagRSAPRIVATEKEY2,"p[1]","p["&$bitlen/8/2&"]")
        $tagRSAPRIVATEKEY2 = StringReplace($tagRSAPRIVATEKEY2,"q[1]","q["&$bitlen/8/2&"]")
        Local $blob_data = DllStructCreate($tagRSAPRIVATEKEY2)
        $blob_data.magic = "RSA2" 
        $blob_data.bitlen = $bitlen 
        $blob_data.PubExpLength = 3 
        $blob_data.ModulusLength = $blob_data.bitlen/8 
        $blob_data.PLength = $blob_data.bitlen/8/2 
        $blob_data.QLength = $blob_data.bitlen/8/2 
        $blob_data.PubExp = $e 
        $blob_data.Modulus = $Modulus 
        $blob_data.p = $p 
        $blob_data.q = $q 

        if $show = 1 then
            _WinAPI_DisplayStruct($blob_data,$tagRSAPRIVATEKEY2)
        EndIf

    EndIf
Else
    if $tag = "Public" Then
        Local $tagRSAPUBKEY_Legacy2 = StringReplace($tagRSAPUBKEY_Legacy,"Modulus[1]","Modulus["&$bitlen/8&"]")
        Local $blob_data = DllStructCreate($tagRSAPUBKEY_Legacy2)
        $blob_data.bType = 0x06 
        $blob_data.bVersion = 0x02
        $blob_data.aiKeyAlg = Binary("0x00A40000")
        $blob_data.magic = "RSA1"
        $blob_data.bitlen = $bitlen 
        $blob_data.PubExp =  Binary("0x" & _BigHex_Swap_endian(_BigNum_ToBase2($e,16,1))) 
        $blob_data.Modulus = Binary("0x" & _BigHex_Swap_endian($Modulus)) 

        if $show = 1 Then
            _WinAPI_DisplayStruct($blob_data,$tagRSAPUBKEY_Legacy2)
        EndIf

    Else

        Local $tagRSAPRIVATEKEY_Legacy2 = StringReplace($tagRSAPRIVATEKEY_Legacy,"Modulus[1]","Modulus["&$bitlen/8&"]")
        $tagRSAPRIVATEKEY_Legacy2 = StringReplace($tagRSAPRIVATEKEY_Legacy2,"p[1]","p["&$bitlen/8/2&"]")
        $tagRSAPRIVATEKEY_Legacy2 = StringReplace($tagRSAPRIVATEKEY_Legacy2,"q[1]","q["&$bitlen/8/2&"]")
        $tagRSAPRIVATEKEY_Legacy2 = StringReplace($tagRSAPRIVATEKEY_Legacy2,"exponent1[1]","exponent1["&$bitlen/8/2&"]")
        $tagRSAPRIVATEKEY_Legacy2 = StringReplace($tagRSAPRIVATEKEY_Legacy2,"exponent2[1]","exponent2["&$bitlen/8/2&"]")
        $tagRSAPRIVATEKEY_Legacy2 = StringReplace($tagRSAPRIVATEKEY_Legacy2,"coefficient[1]","coefficient["&$bitlen/8/2&"]")
        $tagRSAPRIVATEKEY_Legacy2 = StringReplace($tagRSAPRIVATEKEY_Legacy2,"privateExponent[1]","privateExponent["&$bitlen/8&"]")
        Local $blob_data = DllStructCreate($tagRSAPRIVATEKEY_Legacy2)
        $blob_data.bType = 0x07 
        $blob_data.bVersion = 0x02
        $blob_data.aiKeyAlg = Binary("0x00A40000")
        $blob_data.magic = "RSA2" 
        $blob_data.bitlen = $bitlen 
        $blob_data.PubExp = Binary("0x" & _BigHex_Swap_endian(_BigNum_ToBase2($e,16,1)))
        $blob_data.Modulus = Binary("0x" & _BigHex_Swap_endian($Modulus)) 
        $blob_data.p = Binary("0x" & _BigHex_Swap_endian($p)) 
        $blob_data.q = Binary("0x" & _BigHex_Swap_endian($q)) 
        Local $D = _BigNum_inverse(_BigNum_Mul(_BigNum_Sub(_BigHex_ToBase2(Hex($p), 16),1),_BigNum_Sub(_BigHex_ToBase2(Hex($q), 16),1)),$e)
        $blob_data.exponent1 = Binary("0x" &_BigHex_Swap_endian(_BigNum_ToBase2(_BigNum_mod($D,_BigNum_Sub(_BigHex_ToBase2(Hex($p), 16),1)),16,1)))
        $blob_data.exponent2 = Binary("0x" &_BigHex_Swap_endian(_BigNum_ToBase2(_BigNum_mod($D,_BigNum_Sub(_BigHex_ToBase2(Hex($q), 16),1)),16,1))) 
        $blob_data.coefficient =  Binary("0x" &_BigHex_Swap_endian(_BigNum_ToBase2(_BigNum_inverse(_BigHex_ToBase2(Hex($p), 16) , _BigHex_ToBase2(Hex($q), 16)),16,1)))
        $blob_data.privateExponent = Binary("0x" &_BigHex_Swap_endian(_BigNum_ToBase2($D,16,1))) 


        if $show = 1 then
            _WinAPI_DisplayStruct($blob_data,$tagRSAPRIVATEKEY_Legacy2)
        EndIf

    EndIf

EndIf


Local $blob_binary = DllStructCreate("byte a["&DllStructGetSize($blob_data)&"];",DllStructGetPtr($blob_data))

if $write_or_binary = 1 Then
$hFile = FileOpen($key_path,$FO_BINARY+$FO_OVERWRITE)
FileWrite($hFile,$blob_binary.a)
FileClose($hFile)
Else
Return $blob_binary.a
EndIf



EndFunc

Func __CryptoNG_BcryptImportKeyPair($hAlgorithmProvider, $sKeyBlobFile, $sKeyBlobType, $Key_type = "file" )

    If $__gbDebugging Then _DebugOut(@CRLF & "Function: __CryptoNG_BcryptImportKeyPair()")

    Local $aResult[0]

    Local $iError          = 0, _
          $iStatusCode     = 0

    Local $tBuffer = ""

    Local $hFile = -1, _
          $hKey  = -1

    Local $xKeyBlob = Binary("")


    ;Make sure file exists
    if $Key_type = "file" Then
    If Not FileExists($sKeyBlobFile) Then Return SetError(3, 0, "")


    ;Read binary blob file
    $hFile = FileOpen($sKeyBlobFile, $FO_BINARY)
    If $hFile = -1 Then Return SetError(4, 0, "")

    $xKeyBlob = FileRead($hFile)
    FileClose($hFile)
    Else
    $xKeyBlob = $sKeyBlobFile
    EndIf

    ;Create a binary buffer, containing key blob, to pass to function
    $tBuffer      = DllStructCreate(StringFormat("byte data[%i]", BinaryLen($xKeyBlob)))
    $tBuffer.data = $xKeyBlob

    If $__gbDebugging Then _DebugReportVar("Public/Private Key Blob", $tBuffer.data)


    ;Import key pair
    $aResult = DllCall(__CryptoNG_GetBcryptDllHandle(), "int", "BCryptImportKeyPair", _
                       "handle",   $hAlgorithmProvider, _
                       "handle",   Null, _
                       "wstr",     $sKeyBlobType, _
                       "handle*",  Null, _
                       "struct*",  $tBuffer, _
                       "ulong",    DllStructGetSize($tBuffer), _
                       "ulong",    $CNG_BCRYPT_NO_KEY_VALIDATION _
                       )
    If @error Then
        $iError = @error
        $__gsLastErrorMessage = __CryptoNG_DllCallErrorMessage($iError)
        Return SetError(1, $iError, "")
    EndIf

    If $__gbDebugging Then _DebugReportVar("BCryptImportKeyPair $aResult", $aResult)


    ;Get returned values from dllcall
    $iStatusCode = $aResult[0]
    $hKey        = $aResult[4]


    ;Check status code from dllcall
    If $iStatusCode <> $CNG_STATUS_SUCCESS Then
        $__gsLastErrorMessage = __CryptoNG_StatusMessage($iStatusCode)
        Return SetError(2, $iStatusCode, "")
    EndIf


    ;All is good, return handle pointer
    Return $hKey

EndFunc

Func _CryptoNG_RSA_EncryptData($sText, $sPublicKeyBlobFile, $iPadding = Default, $sProvider = Default ,$Key_type = "file")

    If $__gbDebugging Then _DebugOut(@CRLF & "Function: _CryptoNG_RSA_EncryptData()")

    Local $hAlgorithmProvider = -1, _
          $hEncryptionKey     = -1

    Local $xEncryptedText = ""


    ;Resolve defaults
    If $sProvider = Default Then $sProvider = "Microsoft Primitive Provider"
    If $iPadding  = Default Then $iPadding  = $CNG_BCRYPT_PAD_PKCS1

    ;Make sure that text is not blank
    If $sText = "" Then
        $__gsLastErrorMessage = "Invalid parameter - Text cannot be blank."
        Return SetError(-1, 0, "")
    EndIf


    ;Make sure that key blob file exists
    if $Key_type = "file" Then
    If Not FileExists($sPublicKeyBlobFile) Then
        $__gsLastErrorMessage = "Public key blob file does not exist."
        Return SetError(-2, 0, "")
    EndIf
    EndIf

    ;Open algorithm provider
    $hAlgorithmProvider = __CryptoNG_BCryptOpenEncryptionAlgorithmProvider($CNG_BCRYPT_RSA_ALGORITHM, $sProvider)
    If @error Then Return SetError(2, 0, "")

    ;Import rsa public key
    $hEncryptionKey = __CryptoNG_BcryptImportKeyPair($hAlgorithmProvider, $sPublicKeyBlobFile, $CNG_BCRYPT_RSAPUBLIC_BLOB, $Key_type)
    If @error Then
        If $hAlgorithmProvider <> -1 Then __CryptoNG_BcryptCloseAlgorithmProvider($hAlgorithmProvider)
        Return SetError(3, 0, "")
    EndIf

    ;Encrypt data
    $xEncryptedText = __CryptoNG_BCryptEncrypt_RSA($CNG_BCRYPT_RSA_ALGORITHM, $sText, $hEncryptionKey, $iPadding)
    If @error Then
        If $hAlgorithmProvider <> -1 Then __CryptoNG_BcryptCloseAlgorithmProvider($hAlgorithmProvider)
        If $hEncryptionKey     <> -1 Then __CryptoNG_BcryptDestroyKey($hEncryptionKey)
        Return SetError(4, 0, "")
    EndIf

    ;Clean up
    If $hAlgorithmProvider <> -1 Then __CryptoNG_BcryptCloseAlgorithmProvider($hAlgorithmProvider)
    If $hEncryptionKey     <> -1 Then __CryptoNG_BcryptDestroyKey($hEncryptionKey)

    Return $xEncryptedText

EndFunc

 

Posted
7 hours ago, konya said:

...hoping to be helpful to you.

As is, it is not.
You could take all you posted here and put it in the examples section, and in the place of this post, a link to the examples area post.

Also add a link in the code for users to find the includes

#include <bignum.au3> ; https://get it from here

and add comments for the reader of the post, to have context as otherwise, ...what is this and what does it solve.

Thanks

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...