BLF_BytesMode encrypts or decrypts an array of Bytes
using a specified mode.
Public Declare Function BLF_BytesMode Lib "diCryptoSys.dll"
(ByRef abOutput As Byte, ByRef abData As Byte, ByVal nDataLen As Long,
ByRef abKey As Byte, ByVal nKeyLen As Long, ByVal bEncrypt As Boolean,
ByVal strMode As String, ByRef abInitV As Byte) As Long
nRet = BLF_BytesMode(abOutput(0), abData(0), nDataLen, abKey(0),
nKeyLen, bEncrypt, strMode, abInitV(0))
Byte array of sufficient length to receive the output.Byte array containing the input data.Long equal to length of the input data in bytes.Byte array containing the key.Long equal to length of the key in bytes.Boolean direction flag:
set as True to encrypt or False
to decrypt.String specifying the confidentiality mode:Byte array containing the
initialization vector (IV), or zero (0) for ECB mode.
long _stdcall BLF_BytesMode(unsigned char *output, const unsigned char *input,
long nbytes, const unsigned char *key, long keyBytes,
int bEncrypt, const char *strMode, const unsigned char *iv);
Long: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Blowfish.Encrypt Method (Byte[], Byte[], Mode, Byte[])
Blowfish.Decrypt Method (Byte[], Byte[], Mode, Byte[])
The input data abData must be an exact multiple of 8 bytes long. If not, an error code will be returned. The key abKey can be any length between 1 and 56 bytes. 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.
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 abOutput() 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 abOutput(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(abOutput(0), abData(0), nDataLen, abKey(0), _
nKeyLen, ENCRYPT, "CBC", abInitV(0))
Debug.Print "CT="; cnvHexStrFromBytes(abOutput), nRet
Debug.Print "OK="; sCorrect
' Now decrypt back
nRet = BLF_BytesMode(abData(0), abOutput(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