CryptoSys API Library Manual

AES128_Bytes

Encrypts or decrypts an array of Bytes in one step in Electronic Codebook (EBC) mode.

@deprecated use AES128_BytesMode with strMode="ECB".

VBA/VB6 Syntax

Public Declare Function AES128_Bytes Lib "diCryptoSys.dll" (ByRef lpOutput As Byte, ByRef lpData As Byte, ByVal nDataLen As Long, ByRef lpKey As Byte, ByVal bEncrypt As Boolean) As Long

nRet = AES128_Bytes(lpOutput(0), abData(0), nDataLen, abKey(0), bEncrypt) ' Note the "(0)" after the byte array parameters

C/C++ Syntax

long __stdcall AES128_Bytes(unsigned char *lpOutput, const unsigned char *lpInput, long nBytes, const unsigned char *lpKey, int fEncrypt);

Parameters

lpOutput
[out] array of sufficient length to receive the output.
lpInput
[in] array containing the input data.
nBytes
[in] equal to length of the input data in bytes.
lpKey
[in] array containing the key.
fEncrypt
[in] direction flag: set as ENCRYPT (True) to encrypt or DECRYPT (False) to decrypt.

Returns (VBA/C)

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[])
with mode=Mode.ECB.

Remarks

The input array lpData 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 output array lpOutput must be at least as long as the input array. lpOutput and lpData may be the same.

This function is equivalent to

nRet = AES128_BytesMode(lpOutput(0), abData(0), nDataLen, abKey(0), bEncrypt, "ECB", 0)

Example

    Dim nRet As Long
    Dim abBlock() As Byte
    Dim abKey() As Byte
    Dim abPlain() As Byte
    Dim abCipher() As Byte
    Dim nBytes As Long
    
    'FIPS-197
    'C.1 AES-128 (Nk=4, Nr=10)
    'PLAINTEXT: 00112233445566778899aabbccddeeff
    'KEY: 000102030405060708090a0b0c0d0e0f
    
    ' Convert input to bytes
    abKey = cnvBytesFromHexStr("000102030405060708090a0b0c0d0e0f")
    abPlain = cnvBytesFromHexStr("00112233445566778899aabbccddeeff")
    abCipher = cnvBytesFromHexStr("69c4e0d86a7b0430d8cdb78070b4c55a")
    
    abBlock = abPlain
    nBytes = UBound(abBlock) - LBound(abBlock) + 1
    Debug.Print "KY=" & cnvHexStrFromBytes(abKey)
    Debug.Print "PT=" & cnvHexStrFromBytes(abBlock)
    ' Encrypt in one-off process
    nRet = AES128_Bytes(abBlock(0), abBlock(0), nBytes, abKey(0), ENCRYPT)
    Debug.Print "CT=" & cnvHexStrFromBytes(abBlock)
    Debug.Print "OK=" & cnvHexStrFromBytes(abCipher)
    Debug.Assert (StrConv(abBlock, vbUnicode) = StrConv(abCipher, vbUnicode))
    
    ' Now decrypt back to plain text
    nRet = AES128_Bytes(abBlock(0), abBlock(0), nBytes, abKey(0), DECRYPT)
    Debug.Print "P'=" & cnvHexStrFromBytes(abBlock)
    Debug.Assert (StrConv(abBlock, vbUnicode) = StrConv(abPlain, vbUnicode))

This should result in output as follows:

KY=000102030405060708090A0B0C0D0E0F
PT=00112233445566778899AABBCCDDEEFF
CT=69C4E0D86A7B0430D8CDB78070B4C55A
OK=69C4E0D86A7B0430D8CDB78070B4C55A
P'=00112233445566778899AABBCCDDEEFF

See Also

AES128_BytesMode

[Contents] [Index]

[PREV: AES128_B64Mode...]   [Contents]   [Index]   
   [NEXT: AES128_BytesMode...]

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