Encrypts or decrypts an array of Bytes in one step using a specified mode.
Public Declare Function TDEA_BytesMode Lib "diCrPKI.dll"
(ByRef lpOutput As Byte, ByRef lpInput As Byte, ByVal nDataLen As Long, ByRef lpKey As Byte,
ByVal fEncrypt As Long, ByVal strMode As String, ByRef lpIV As Byte) As Long
nRet = TDEA_BytesMode(lpOutput(0), lpInput(0), nDataLen,
lpKey(0), fEncrypt, strMode, lpIV(0)) ' Note the "(0)" after the byte array parameters
long __stdcall TDEA_BytesMode(unsigned char *lpOutput, const unsigned char *lpData, long nDataLen, const unsigned char *lpKey, long fEncrypt, const char *szMode, const unsigned char *lpIV);
If successful, the return value is zero; otherwise it returns a nonzero error code.
Tdea.Encrypt Method (Byte[], Byte[], Mode, Byte[])
Tdea.Decrypt Method (Byte[], Byte[], Mode, Byte[])
For ECB and CBC modes, the input data lpInput must be a multiple of 8 bytes long otherwise a "Data not a valid length" error will result. The key lpKey must be exactly 24 bytes long and the IV, if required, exactly 8 bytes long. The output array lpOutput must be at least nDataLen bytes long. lpOutput and lpInput may be the same.
Dim nRet As Long Dim sOutput As String Dim sInput As String Dim sKey As String Dim sHexIV As String Dim sCorrect As String Dim nKeyLen As Long Dim nDataLen As Long Dim nIVLen As Long Dim aKey() As Byte Dim aResult() As Byte Dim aData() As Byte Dim aInitV() As Byte sKey = "0123456789abcdeffedcba987654321089abcdef01234567" sHexIV = "1234567890abcdef" sInput = "Now is the time for all " sCorrect = "204011f986e35647199e47af391620c5bb9a5bcfc86db0bb" ' Convert hex strings to byte arrays nKeyLen = Len(sKey) \ 2 ReDim aKey(nKeyLen) Call CNV_BytesFromHexStr(aKey(0), nKeyLen, sKey) nIVLen = Len(sHexIV) \ 2 ReDim aInitV(nIVLen) Call CNV_BytesFromHexStr(aInitV(0), nIVLen, sHexIV) ' Convert string to byte array and dimension aResult array aData() = StrConv(sInput, vbFromUnicode) nDataLen = UBound(aData) + 1 ReDim aResult(nDataLen - 1) Debug.Print "KY=" & cnvHexStrFromBytes(aKey) Debug.Print "IV=" & cnvHexStrFromBytes(aInitV) Debug.Print "PT=" & "[" & sInput & "]" ' Encrypt in one-off process nRet = TDEA_BytesMode(aResult(0), aData(0), nDataLen, _ aKey(0), ENCRYPT, "CBC", aInitV(0)) Debug.Print "CT=" & cnvHexStrFromBytes(aResult) & nRet Debug.Print "OK=" & sCorrect ' Now decrypt back nRet = TDEA_BytesMode(aData(0), aResult(0), nDataLen, _ aKey(0), DECRYPT, "cbc", aInitV(0)) sOutput = StrConv(aData(), vbUnicode) Debug.Print "P'=" & "[" & sOutput & "]" & nRet
This should result in output as follows:
KY=0123456789ABCDEFFEDCBA987654321089ABCDEF01234567 IV=1234567890ABCDEF PT=[Now is the time for all ] CT=204011F986E35647199E47AF391620C5BB9A5BCFC86DB0BB 0 OK=204011f986e35647199e47af391620c5bb9a5bcfc86db0bb P'=[Now is the time for all ] 0
TDEA_HexMode TDEA_B64Mode TDEA_File