CryptoSys API Library Manual

BLF_BytesMode

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

VBA/VB6 Syntax

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

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

C/C++ Syntax

long __stdcall BLF_BytesMode(unsigned char *lpOutput, const unsigned char *lpInput, long nBytes, const unsigned char *lpKey, long keyBytes, 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.
keyBytes
[in] equal to length of the key in bytes.
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 0; otherwise it returns a non-zero error code.

.NET Equivalent

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

Python Equivalent

static Blowfish.encrypt_block(data, key, modestr="ECB", iv=None)
static Blowfish.decrypt_block(data, key, modestr="ECB", iv=None)

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 can be any length between 1 and 56 bytes (448 bits). 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 nKeyLen As Long
    Dim abKey() As Byte
    Dim lpOutput() As Byte
    Dim abData() As Byte
    Dim nDataLen As Long
    Dim abInitV() As Byte
    Dim nIVLen As Long
    
    strKey = "0123456789ABCDEFF0E1D2C3B4A59687"
    strHexIV = "FEDCBA9876543210"
    strInput = _
"37363534333231204E6F77206973207468652074696D6520666F722000000000"
    sCorrect = _
"6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC"
    
    ' Convert to byte arrays and compute lengths
    nKeyLen = Len(strKey) \ 2
    nDataLen = Len(strInput) \ 2
    nIVLen = Len(strHexIV) \ 2
    abKey = cnvBytesFromHexStr(strKey)
    abData = cnvBytesFromHexStr(strInput)
    abInitV = cnvBytesFromHexStr(strHexIV)
    
    ' 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 = BLF_BytesMode(lpOutput(0), abData(0), nDataLen, abKey(0), _
        nKeyLen, ENCRYPT, "CBC", abInitV(0))
    Debug.Print "CT=" & cnvHexStrFromBytes(lpOutput), nRet
    Debug.Print "OK=" & sCorrect
    
    ' Now decrypt back
    nRet = BLF_BytesMode(abData(0), lpOutput(0), nDataLen, abKey(0), _
        nKeyLen, DECRYPT, "cbc", abInitV(0))
    Debug.Print "P'=" & cnvHexStrFromBytes(abData), nRet

This should result in output as follows:

KY=0123456789ABCDEFF0E1D2C3B4A59687
IV=FEDCBA9876543210
PT=37363534333231204E6F77206973207468652074696D6520666F722000000000
CT=6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC    0 
OK=6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CC
P'=37363534333231204E6F77206973207468652074696D6520666F722000000000    0 

See Also

BLF_Bytes

[Contents] [Index]

[PREV: BLF_Bytes...]   [Contents]   [Index]   
   [NEXT: BLF_File...]

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