CryptoSys API Library Manual

AES128_BytesMode

AES128_BytesMode encrypts or decrypts an array of Bytes using a specified mode. The key and IV data are in byte arrays.

VB6/VBA Syntax

Public Declare Function AES128_BytesMode Lib "diCryptoSys.dll" (ByRef abOutput As Byte, ByRef abData As Byte, ByVal nDataLen As Long, ByRef abKey As Byte, ByVal bEncrypt As Boolean, ByVal strMode As String, ByRef abInitV As Byte) As Long

nRet = AES128_Bytes(abOutput(0), abData(0), nDataLen, abKey(0), bEncrypt, strMode, abInitV(0))

Parameters

abOutput
[out] Byte array of sufficient length to receive the output.
abData
[in] Byte array containing the input data.
nDataLen
[in] Long equal to length of the input data in bytes.
abKey
[in] Byte array containing the key.
bEncrypt
[in] Boolean direction flag: set as True to encrypt or False to decrypt.
strMode
[in] String 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.
abInitV
[in] Byte containing the initialization vector (IV), or zero (0) for ECB mode.

C/C++ Syntax

long _stdcall AES128_BytesMode(unsigned char *output, const unsigned char *input, long nbytes, const unsigned char *key, int bEncrypt, const char *lpszMode, const unsigned char *iv);

Returns (VB6/C)

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

.NET Equivalent

Aes128.Encrypt Method (Byte[], Byte[], Mode, Byte[])
Aes128.Decrypt Method (Byte[], Byte[], Mode, Byte[])

Remarks

The input array abData must be an exact multiple of 16 bytes long. If not, an error code will be returned. The key array abKey() must be exactly 16 bytes long. The initialization vector abInitV() must be exactly the block size of 16 bytes long, except for ECB mode, where it is ignored (use 0). The output array abOutput must be at least as long as the input array. abOutput and abData may be the same. Note the (0) after the byte array variables.

Example

    Dim nRet As Long
    Dim strOutput As String
    Dim strInput As String
    Dim sCorrect As String
    Dim abKey()  As Byte
    Dim abInitV() As Byte
    Dim abResult() As Byte
    Dim abData() As Byte
    Dim nDataLen As Long

    ' Set up input in byte arrays
    strInput = "Now is the time for all good men"
    sCorrect = "C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177"
    abKey = cnvBytesFromHexStr("0123456789ABCDEFF0E1D2C3B4A59687")
    abInitV = cnvBytesFromHexStr("FEDCBA9876543210FEDCBA9876543210")
    abData = StrConv(strInput, vbFromUnicode)
    nDataLen = UBound(abData) - LBound(abData) + 1
    
    ' Pre-dimension output array
    ReDim abResult(nDataLen - 1)
    
    Debug.Print "KY="; cnvHexStrFromBytes(abKey)
    Debug.Print "IV="; cnvHexStrFromBytes(abInitV)
    Debug.Print "PT="; cnvHexStrFromBytes(abData)
    ' Encrypt in one-off process
    nRet = AES128_BytesMode(abResult(0), abData(0), nDataLen, abKey(0), _
        ENCRYPT, "CBC", abInitV(0))
    Debug.Print "CT="; cnvHexStrFromBytes(abResult)
    Debug.Print "OK="; sCorrect

    ' Now decrypt back
    nRet = AES128_BytesMode(abData(0), abResult(0), nDataLen, abKey(0), _
        DECRYPT, "CBC", abInitV(0))
    strOutput = StrConv(abData(), vbUnicode)
    Debug.Print "P'="; strOutput
    
    ' Check
    Debug.Assert (strOutput = strInput)

This should result in output as follows:

KY=0123456789ABCDEFF0E1D2C3B4A59687
IV=FEDCBA9876543210FEDCBA9876543210
PT=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E
CT=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177
OK=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177
P'=Now is the time for all good men

See Also

AES128_Bytes

[Contents] [Index]

[HOME]   [NEXT: AES128_File...]

Copyright © 2001-9 D.I. Management Services Pty Ltd. All rights reserved.