CryptoSys API Library Manual

TDEA_BytesMode

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

VB6/VBA Syntax

Public Declare Function TDEA_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 = TDEA_BytesMode(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 64-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 TDEA_BytesMode(unsigned char *output, const unsigned char *input, long nbytes, const unsigned char *key, int bEncrypt, const char *pStrMode, 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

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

Remarks

The input data abData must be an exact multiple of 8 bytes long. If not, an error code will be returned. The key abKey must be exactly 24 bytes long and the IV, if required, exactly 8 bytes long. The output array abOutput must be at least nDataLen bytes long. 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 strKey As String
Dim strHexIV As String
Dim sCorrect As String
Dim nDataLen As Long
Dim abKey() As Byte
Dim abOutput() As Byte
Dim abData() As Byte
Dim abInitV() As Byte

strKey = "0123456789abcdeffedcba987654321089abcdef01234567"
strHexIV = "1234567890abcdef"
strInput = "Now is the time for all "
sCorrect = "204011f986e35647199e47af391620c5bb9a5bcfc86db0bb"

' Convert to byte arrays and compute lengths
abKey = cnvBytesFromHexStr(strKey)
abInitV = cnvBytesFromHexStr(strHexIV)
abData = StrConv(strInput, vbFromUnicode)
nDataLen = Len(strInput)

' Dimension array for output
ReDim abOutput(nDataLen - 1)

Debug.Print "KY=" & cnvHexStrFromBytes(abKey)
Debug.Print "IV=" & cnvHexStrFromBytes(abInitV)
Debug.Print "PT=" & cnvHexStrFromBytes(abData)

' Encrypt in one-off process
nRet = TDEA_BytesMode(abOutput(0), abData(0), nDataLen, _
    abKey(0), True, "CBC", abInitV(0))
Debug.Print "CT=" & cnvHexStrFromBytes(abOutput)
Debug.Print "OK=" & sCorrect
Debug.Assert (sCorrect = cnvHexStrFromBytes(abOutput))

' Now decrypt back
nRet = TDEA_BytesMode(abData(0), abOutput(0), nDataLen, _
    abKey(0), DECRYPT, "CBC", abInitV(0))
strOutput = StrConv(abData(), vbUnicode)
Debug.Print "P'=" & "[" & strOutput & "]"
Debug.Assert (strOutput = strInput)

This should result in output as follows:

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

See Also

TDEA_Bytes

[Contents] [Index]

[HOME]   [NEXT: TDEA_File...]

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