CryptoSys PKI Toolkit Manual

CIPHER_Bytes

CIPHER_Bytes uses the specified block cipher algorithm and mode to encrypt or decrypt data in a bytes array. The key and initialization vector are given as bytes arrays.

VB6/VBA Syntax

Public Declare Function CIPHER_Bytes Lib "diCrPKI.dll" (ByVal fEncrypt As Long, ByRef abOutput As Byte, ByRef abData As Byte, ByVal nDataLen As Long, ByRef abKey As Byte, ByRef abIV As Byte, ByVal strAlgAndMode As String, ByVal nOptions As Long) As Long

nRet = CIPHER_Bytes(fEncrypt, abOutput(0), abInput(0), nDataLen, abKey(0), abIV(0), strAlgAndMode, nOptions)

Parameters

fEncrypt
[in] Boolean direction flag: set as ENCRYPT (True) to encrypt or DECRYPT (False) to decrypt.
abOutput
[out] Byte array of sufficient length to receive the output.
abData
[in] Byte array containing the input data.
nDataLen
[in] Long specifying the length of the input data in bytes.
abKey
[in] Byte array containing the key.
abIV
[in] Byte containing the initialization vector (IV), or zero (0) for ECB mode.
strAlgAndMode
[in] String containing the block cipher algorithm and mode (see Specifying the algorithm and mode for generic block cipher functions).
nOptions
[in] Long option flags.

C/C++ Syntax

long _stdcall CIPHER_Bytes(long fEncrypt, unsigned char *lpOutput, const unsigned char *lpData, long nDataLen, const unsigned char *lpKey, const unsigned char *lpIV, const char *szAlgAndMode, long nOptions);

Returns (VB6/C)

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

.NET Equivalent

Cipher.Encrypt Method (Byte[], Byte[], Byte[], CipherAlgorithm, Mode)
Cipher.Decrypt Method (Byte[], Byte[], Byte[], CipherAlgorithm, Mode)

Remarks

The algorithm and mode must be specified using either the strAlgAndMode or nOptions parameter (see Specifying the algorithm and mode for generic block cipher functions). The length of key abKey must be exactly the required key size, and the length of the IV, if required, exactly the block size. See Valid key and block sizes. Important: The output array abOutput must be at least nDataLen bytes long. abOutput and abData may be the same. VB6 users should note the (0) after the byte array parameters.

For ECB and CBC modes, the length of the input data abInput must be an exact multiple of the block size long otherwise a BAD_LENGTH_ERROR error will result. It is the responsibility of the user to provide suitable padding before encrypting in those modes and to remove any padding after decrypting.

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 abCheck() 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=" & strInput
Debug.Print "PT=" & cnvHexStrFromBytes(abData)
' Encrypt in one-off process (abResult <-- abData)
nRet = CIPHER_Bytes(ENCRYPT, abResult(0), abData(0), nDataLen, _
    abKey(0), abInitV(0), "aes128-cbc", 0)
Debug.Print "CIPHER_Bytes(ENCRYPT) returns " & nRet
Debug.Print "CT=" & cnvHexStrFromBytes(abResult)
Debug.Print "OK=" & sCorrect

' Now decrypt back (abCheck <-- abResult)
ReDim abCheck(nDataLen - 1)
nRet = CIPHER_Bytes(DECRYPT, abCheck(0), abResult(0), nDataLen, _
    abKey(0), abInitV(0), "", PKI_BC_AES128 + PKI_MODE_CBC)
Debug.Print "CIPHER_Bytes(DECRYPT) returns " & nRet
' And decode back from a byte array into a string
Debug.Print "P'=" & cnvHexStrFromBytes(abCheck)
strOutput = StrConv(abCheck(), vbUnicode)
Debug.Print "P'=" & strOutput

This should result in output as follows:

KY=0123456789ABCDEFF0E1D2C3B4A59687
IV=FEDCBA9876543210FEDCBA9876543210
PT=Now is the time for all good men
PT=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E
CIPHER_Bytes(ENCRYPT) returns 0
CT=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177
OK=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177
CIPHER_Bytes(DECRYPT) returns 0
P'=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E
P'=Now is the time for all good men

See Also

CIPHER_Hex CIPHER_File

[Contents] [Index]

[HOME]   [NEXT: CIPHER_File...]

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