CryptoSys API Library Manual

AES128_HexMode

Encrypts or decrypts data represented as a hexadecimal string using a specified mode. The key and initialization vector are represented as a hexadecimal string.

VBA/VB6 Syntax

Public Declare Function AES128_HexMode Lib "diCryptoSys.dll" (ByVal strOutput As String, ByVal strInput As String, ByVal strHexKey As String, ByVal bEncrypt As Boolean, ByVal strMode As String, ByVal strHexIV As String) As Long

nRet = AES128_HexMode(strOutput, strInput, strHexKey, bEncrypt, strMode, strHexIV)

C/C++ Syntax

long __stdcall AES128_HexMode(char *szOutput, const char *szInput, const char *szKey, int fEncrypt, const char *szMode, const char *szIV);

Parameters

szOutput
[out] of sufficient length to receive the output.
szInput
[in] containing the input data in hexadecimal.
szKey
[in] containing the key in hexadecimal.
fEncrypt
[in] direction flag: set as ENCRYPT (True) to encrypt or DECRYPT (False) to decrypt.
szMode
[in] specifying the confidentiality mode:
"ECB" for Electronic Codebook mode,
"CBC" for Cipher Block Chaining mode,
"CFB" for 128-bit Cipher Feedback mode,
"OFB" for Output Feedback mode, or
"CTR" for Counter mode.
szIV
[in] containing the initialization vector (IV) in hexadecimal.

Returns (VBA/C)

If successful, the return value is 0; otherwise it returns a non-zero error code.

.NET Equivalent

Aes128.Encrypt Method (String, String, Mode, String)
Aes128.Decrypt Method (String, String, Mode, String)

COM/ASP Equivalent

aes128.HexMode
Public Function HexMode(ByVal strInput As String, ByVal strHexKey As String, ByVal bEncrypt As Boolean, ByVal strMode As String, ByVal strHexIV As String) As String

See aes128.HexMode.

Remarks

The length of the input string szInput should be a multiple of 32 hex characters long (i.e. representing a multiple of 16 bytes). The key string szHexKey must be exactly 32 hex characters long (i.e. representing exactly 16 bytes/128 bits). The initialization vector szHexIV must be exactly 32 hex characters long (i.e. representing exactly the block size of 16 bytes), except for ECB mode, where it is ignored (use ""). The output string szOutput must be set up with at least the same number of characters as the input string before calling. The variables szOutput and szInput should be different. Valid hexadecimal characters are [0-9A-Fa-f].

Example

The examples are from RFC 3602 The AES-CBC Cipher Algorithm and Its Use with IPsec

    Dim nRet As Long
    Dim strOutput As String
    Dim strInput As String
    Dim strHexKey As String
    Dim sPlain As String
    Dim sCipher As String
    Dim strHexIV As String
'Case #1: Encrypting 16 bytes (1 block) using AES-CBC with 128-bit key
'Key       : 0x06a9214036b8a15b512e03d534120006
'IV        : 0x3dafba429d9eb430b422da802c9fac41
'Plaintext : "Single block msg"
'Ciphertext: 0xe353779c1079aeb82708942dbe77181a

    strHexKey = "06a9214036b8a15b512e03d534120006"
    strHexIV = "3dafba429d9eb430b422da802c9fac41"
    sPlain = cnvHexStrFromString("Single block msg")
    sCipher = "e353779c1079aeb82708942dbe77181a"
    
    strInput = sPlain
    ' Set strOutput to be same length as strInput
    strOutput = String(Len(strInput), " ")
    
    Debug.Print "KY=" & strHexKey
    Debug.Print "IV=" & strHexIV
    Debug.Print "PT=" & strInput
    ' Encrypt in one-off process
    nRet = AES128_HexMode(strOutput, strInput, strHexKey, ENCRYPT, "CBC", strHexIV)
    Debug.Print "CT=" & strOutput; nRet
    Debug.Print "OK=" & sCipher
    Debug.Assert (strOutput = sCipher)
    
    ' Decrypt to check
    strInput = strOutput
    nRet = AES128_HexMode(strOutput, strInput, strHexKey, DECRYPT, "CBC", strHexIV)
    Debug.Print "P'=" & strOutput; nRet
    Debug.Print "OK=" & sPlain
    Debug.Assert (strOutput = sPlain)
    
' Case #4: Encrypting 64 bytes (4 blocks) using AES-CBC with 128-bit key
' Key       : 0x56e47a38c5598974bc46903dba290349
' IV        : 0x8ce82eefbea0da3c44699ed7db51b7d9
' Plaintext : 0xa0a1a2a3a4a5a6a7a8a9aaabacadaeaf
'               b0b1b2b3b4b5b6b7b8b9babbbcbdbebf
'               c0c1c2c3c4c5c6c7c8c9cacbcccdcecf
'               d0d1d2d3d4d5d6d7d8d9dadbdcdddedf
' Ciphertext: 0xc30e32ffedc0774e6aff6af0869f71aa
'               0f3af07a9a31a9c684db207eb0ef8e4e
'               35907aa632c3ffdf868bb7b29d3d46ad
'               83ce9f9a102ee99d49a53e87f4c3da55
    
    strHexKey = "56e47a38c5598974bc46903dba290349"
    strHexIV = "8ce82eefbea0da3c44699ed7db51b7d9"
    sPlain = "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf" _
        & "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" _
        & "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" _
        & "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
    sCipher = "c30e32ffedc0774e6aff6af0869f71aa" _
        & "0f3af07a9a31a9c684db207eb0ef8e4e" _
        & "35907aa632c3ffdf868bb7b29d3d46ad" _
        & "83ce9f9a102ee99d49a53e87f4c3da55"
        
    strInput = sPlain
    ' Set strOutput to be same length as strInput
    strOutput = String(Len(strInput), " ")
    
    Debug.Print "KY=" & strHexKey
    Debug.Print "IV=" & strHexIV
    Debug.Print "PT=" & strInput
    ' Encrypt in one-off process
    nRet = AES128_HexMode(strOutput, strInput, strHexKey, ENCRYPT, "CBC", strHexIV)
    Debug.Print "CT=" & strOutput; nRet
    Debug.Print "OK=" & sCipher
    Debug.Assert (strOutput = sCipher)

    ' Decrypt to check
    strInput = strOutput
    nRet = AES128_HexMode(strOutput, strInput, strHexKey, DECRYPT, "CBC", strHexIV)
    Debug.Print "P'=" & strOutput; nRet
    Debug.Print "OK=" & sPlain
    Debug.Assert (strOutput = sPlain)

This should result in output as follows:

IV=8ce82eefbea0da3c44699ed7db51b7d9
PT=a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf
c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf
CT=C30E32FFEDC0774E6AFF6AF0869F71AA0F3AF07A9A31A9C684DB207EB0EF8E4E
35907AA632C3FFDF868BB7B29D3D46AD83CE9F9A102EE99D49A53E87F4C3DA55 0 
OK=c30e32ffedc0774e6aff6af0869f71aa0f3af07a9a31a9c684db207eb0ef8e4e
35907aa632c3ffdf868bb7b29d3d46ad83ce9f9a102ee99d49a53e87f4c3da55
P'=A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF
C0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF 0 
OK=a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebf
c0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf

See Also

AES128_Hex

[Contents] [Index]

[PREV: AES128_Hex...]   [Contents]   [Index]   
   [NEXT: AES128_Init...]

Copyright © 2001-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-01-07T07:42:00Z.