Encrypts or decrypts an array of Bytes using a specified mode.
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
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);
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[])
static Blowfish.encrypt_block(data, key, modestr="ECB", iv=None)
static Blowfish.decrypt_block(data, key, modestr="ECB", iv=None)
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.
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