Jump to content

CryptoNG UDF - Cryptography API: Next Gen


Recommended Posts

#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d

#include <Constants.au3>
#include <CryptoNG.au3>

__ExampleA()
Func __ExampleA()
    Local $xEncryptedMessage = 'eiUj3S9BgJDcRylhHExLMRRjmsxXj1nwB1HzaN8fp9nrduDyGriduqbjh452imKr'
    Local $Key = '93wj660t8fok9jws'
    Local $IV = 'r0yy7e67p49ee4d7'

    $xEncryptedMessage = _CryptoNG_CryptStringToBinary($xEncryptedMessage, $CNG_CRYPT_STRING_BASE64)
    ConsoleWrite("$xEncryptedMessage: "& @tab & $xEncryptedMessage & @CRLF)

    $Key = StringToBinary($Key)
    ConsoleWrite("$Key: " & @tab & @tab & @tab & $Key & @CRLF)

    $IV = StringToBinary($IV)
    ConsoleWrite("$IV: " & @tab & @tab & @tab & $IV & @CRLF)

    Local $sDecryptedMessage = _CryptoNG_AES_CBC_DecryptData($xEncryptedMessage, $Key, $IV)
    If @error Then Exit ConsoleWrite("DECRYPT ERROR: " & _CryptoNG_LastErrorMessage())
    ConsoleWrite("$sDecryptedMessage: " & @tab & $sDecryptedMessage & @CRLF)
EndFunc

@TheXman thanks :)

Edited by jugador
Link to post
Share on other sites
  • Replies 102
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Popular Posts

Encryption / Decryption / Hashing Purpose Cryptography API: Next Generation (CNG) is Microsoft's long-term replacement for their CryptoAPI.  Microsoft's CNG is designed to be extensible at m

A new version (v1.3.0) was just published to the Downloads section of the forum. Added _CryptoNG_GenerateRandom and supporting internal function. Added an example of how to implement the

What's New in Version v1.8.0 v1.8.0 (2021-04-12) Added asymmetric (public/private key) RSA encryption/decryption functions _CryptoNG_RSA_EncryptData

Posted Images

Posted (edited)

My test script was very similar to yours. 🙂  As you can see below, the key & IV can be any data type as long as it has the correct length and value.  That is why the variables, in the UDF definition, are prefixed with "$v", meaning variable.

Function definition:

; Name ..........: _CryptoNG_AES_CBC_DecryptData
; Description ...: Decrypt data using AES with CBC block chaining.
; Syntax ........: _CryptoNG_DecryptData($xData, $vEncryptionKey, $vIV = Default, $sProvider = Default)
; Parameters ....: $xData               Binary data to be decrypted.
;                  $vEncryptionKey      Secret/Password used to encrypt text. Must be correct size for encryption algorithm.
;                  $vIV                 [optional] A 128 bit (16 byte) initialization vector. Default is 0x000102030405060708090A0B0C0D0E0F
;                  $sProvider           [optional] A string specifying the desired algorithm provider. Default is Microsoft Primitive Provider.

 

Test script:

#AutoIt3Wrapper_AU3Check_Parameters=-w 3 -w 4 -w 5 -w 6 -d

#include <Constants.au3>
#include <CryptoNG.au3>

aes_cbc_decrypt_with_iv_example()

Func aes_cbc_decrypt_with_iv_example()

    Const $ALG_ID      = "AES CBC"
    Const $BASE64_MSG  = "eiUj3S9BgJDcRylhHExLMRRjmsxXj1nwB1HzaN8fp9nrduDyGriduqbjh452imKr"
    Const $KEY         = "93wj660t8fok9jws"
    Const $IV          = "r0yy7e67p49ee4d7"

    Local $xEncryptedMessage = Binary("")
    Local $sDecryptedMessage = ""


    ;Convert Base64 string to binary
    $xEncryptedMessage = _CryptoNG_CryptStringToBinary($BASE64_MSG, $CNG_CRYPT_STRING_BASE64)
    If @error Then Exit ConsoleWrite("StringToBinary Error: " & _CryptoNG_LastErrorMessage() & @CRLF)

    ;Decrypt encrypted message
    $sDecryptedMessage = _CryptoNG_AES_CBC_DecryptData($xEncryptedMessage, $KEY, $IV)
    If @error Then Exit ConsoleWrite("DecryptData Error: " & _CryptoNG_LastErrorMessage() & @CRLF)

    ;Display results
    ConsoleWrite(StringFormat("%s Encrypted Message (Base64)  = %s", $ALG_ID, $BASE64_MSG) & @CRLF)
    ConsoleWrite(StringFormat("%s Encrypted Message (Binary)  = %s", $ALG_ID, $xEncryptedMessage) & @CRLF)
    ConsoleWrite(StringFormat("%s Encryption Key              = %s", $ALG_ID, $KEY) & @CRLF)
    ConsoleWrite(StringFormat("%s Initialization Vector       = %s", $ALG_ID, $IV) & @CRLF)
    ConsoleWrite(StringFormat("%s Decrypted Message           = %s", $ALG_ID, $sDecryptedMessage) & @CRLF)

EndFunc

Output

AES CBC Encrypted Message (Base64)  = eiUj3S9BgJDcRylhHExLMRRjmsxXj1nwB1HzaN8fp9nrduDyGriduqbjh452imKr
AES CBC Encrypted Message (Binary)  = 0x7A2523DD2F418090DC4729611C4C4B3114639ACC578F59F00751F368DF1FA7D9EB76E0F21AB89DBAA6E3878E768A62AB
AES CBC Encryption Key              = 93wj660t8fok9jws
AES CBC Initialization Vector       = r0yy7e67p49ee4d7
AES CBC Decrypted Message           = {"Color": "Blue", "Type": "Deep Blue"}

 

Edited by TheXman
Link to post
Share on other sites

What's New in Version v1.9.5 (and v1.9.6)

  • - Added 2 new algorithm-specific functions.

    • _CryptoNG_AES_GCM_EncryptData

    • _CryptoNG_AES_GCM_DecryptData

  • Added an AES GCM example to the examples file.

  • Added AES GCM functions to the Help File.

  • Optimized some internal functions

  • Updated the supplied calltips and userudfs files.

  • Misc function header corrections/modifications
     

  • v1.9.6

    • _CryptoNG_AES_GCM_DecryptData

      • Added an explicit Authorization Tag length validation.  ( @error = 8 )

      • Updated the function's help file entry to reflect the new @error (8).

    • Slightly modified the AES GCM encrypt/decrypt example to make the return values from the encryption, which is an array, more explicit and easier to understand.

Edited by TheXman
Link to post
Share on other sites

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...