CryptoSys PKI Pro Manual

TDEA_BytesMode

Encrypts or decrypts an array of Bytes in one step using a specified mode.

VBA/VB6 Syntax

Public Declare Function TDEA_BytesMode Lib "diCrPKI.dll" (ByRef lpOutput As Byte, ByRef lpInput As Byte, ByVal nDataLen As Long, ByRef lpKey As Byte, ByVal fEncrypt As Long, ByVal strMode As String, ByRef lpIV As Byte) As Long

nRet = TDEA_BytesMode(lpOutput(0), lpInput(0), nDataLen, lpKey(0), fEncrypt, strMode, lpIV(0)) ' Note the "(0)" after the byte array parameters

C/C++ Syntax

long __stdcall TDEA_BytesMode(unsigned char *lpOutput, const unsigned char *lpData, long nDataLen, const unsigned char *lpKey, long fEncrypt, const char *szMode, const unsigned char *lpIV);

Parameters

lpOutput
[out] array of sufficient length to receive the output.
lpData
[in] array containing the input data.
nDataLen
[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.
szMode
[in] specifying the confidentiality mode:
"ECB" for Electronic Codebook mode,
"CBC" for Cipher Block Chaining mode,
"CFB" for 64-bit Cipher Feedback mode,
"OFB" for Output Feedback mode, or
"CTR" for Counter mode.
lpIV
[in] array containing the initialization vector (IV), or zero (0) for ECB mode.

Returns (VBA/C)

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

.NET Equivalent

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

Remarks

For ECB and CBC modes, the input data lpInput must be a multiple of 8 bytes long otherwise a "Data not a valid length" error will result. The key lpKey must be exactly 24 bytes long and the IV, if required, exactly 8 bytes long. The output array lpOutput must be at least nDataLen bytes long. lpOutput and lpInput may be the same.

Example

Dim nRet As Long
Dim sOutput As String
Dim sInput As String
Dim sKey As String
Dim sHexIV As String
Dim sCorrect As String
Dim nKeyLen As Long
Dim nDataLen As Long
Dim nIVLen As Long
Dim aKey() As Byte
Dim aResult() As Byte
Dim aData() As Byte
Dim aInitV() As Byte

sKey = "0123456789abcdeffedcba987654321089abcdef01234567"
sHexIV = "1234567890abcdef"
sInput = "Now is the time for all "
sCorrect = "204011f986e35647199e47af391620c5bb9a5bcfc86db0bb"

' Convert hex strings to byte arrays
nKeyLen = Len(sKey) \ 2
ReDim aKey(nKeyLen)
Call CNV_BytesFromHexStr(aKey(0), nKeyLen, sKey)
nIVLen = Len(sHexIV) \ 2
ReDim aInitV(nIVLen)
Call CNV_BytesFromHexStr(aInitV(0), nIVLen, sHexIV)

' Convert string to byte array and dimension aResult array
aData() = StrConv(sInput, vbFromUnicode)
nDataLen = UBound(aData) + 1
ReDim aResult(nDataLen - 1)

Debug.Print "KY=" & cnvHexStrFromBytes(aKey)
Debug.Print "IV=" & cnvHexStrFromBytes(aInitV)
Debug.Print "PT=" & "[" & sInput & "]"
' Encrypt in one-off process
nRet = TDEA_BytesMode(aResult(0), aData(0), nDataLen, _
    aKey(0), ENCRYPT, "CBC", aInitV(0))
Debug.Print "CT=" & cnvHexStrFromBytes(aResult) & nRet
Debug.Print "OK=" & sCorrect

' Now decrypt back
nRet = TDEA_BytesMode(aData(0), aResult(0), nDataLen, _
    aKey(0), DECRYPT, "cbc", aInitV(0))
sOutput = StrConv(aData(), vbUnicode)
Debug.Print "P'=" & "[" & sOutput & "]" & nRet

This should result in output as follows:

KY=0123456789ABCDEFFEDCBA987654321089ABCDEF01234567
IV=1234567890ABCDEF
PT=[Now is the time for all ]
CT=204011F986E35647199E47AF391620C5BB9A5BCFC86DB0BB 0
OK=204011f986e35647199e47af391620c5bb9a5bcfc86db0bb
P'=[Now is the time for all ] 0

See Also

TDEA_HexMode TDEA_B64Mode TDEA_File

[Contents] [Index]

[PREV: TDEA_B64Mode...]   [Contents]   [Index]   
   [NEXT: TDEA_File...]

Copyright © 2004-24 D.I. Management Services Pty Ltd. All rights reserved. Generated 2024-09-23T07:52:09Z.