CryptoSys API Library Manual

DES_BytesMode

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

VBA/VB6 Syntax

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

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

C/C++ Syntax

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

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.
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] containing the initialization vector (IV), or zero (0) for ECB mode.

Returns (VBA/C)

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

.NET Equivalent

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

Remarks

The input data lpData must be an exact multiple of 8 bytes long. If not, an error code will be returned. The key lpKey must be exactly 8 bytes long. The output array lpOutput must be at least nDataLen bytes long. lpOutput and lpData may be the same.

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 lpOutput() As Byte
    Dim abData() As Byte
    Dim abInitV() As Byte
    
    strKey = "0123456789abcdef"
    strHexIV = "1234567890abcdef"
    strInput = "Now is the time for all "
    sCorrect = "e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6"
    
    ' 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 lpOutput(nDataLen - 1)
    
    Debug.Print "KY=" & cnvHexStrFromBytes(abKey)
    Debug.Print "IV=" & cnvHexStrFromBytes(abInitV)
    Debug.Print "PT=" & cnvHexStrFromBytes(abData)
    
    ' Encrypt in one-off process
    nRet = DES_BytesMode(lpOutput(0), abData(0), nDataLen, _
        abKey(0), True, "CBC", abInitV(0))
    Debug.Print "CT=" & cnvHexStrFromBytes(lpOutput), nRet
    Debug.Print "OK=" & sCorrect
    Debug.Assert (sCorrect = cnvHexStrFromBytes(lpOutput))
    
    ' Now decrypt back
    nRet = DES_BytesMode(abData(0), lpOutput(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=0123456789ABCDEF
IV=1234567890ABCDEF
PT=4E6F77206973207468652074696D6520666F7220616C6C20
CT=E5C7CDDE872BF27C43E934008C389C0F683788499A7C05F6      0 
OK=e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6
P'=[Now is the time for all ]

See Also

DES_Bytes

[Contents] [Index]

[PREV: DES_Bytes...]   [Contents]   [Index]   
   [NEXT: DES_CheckKey...]

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