Encrypt or decrypt data in a byte array. The key and initialization vector are given as byte arrays.
@deprecated
Use CIPHER_EncryptBytes or CIPHER_DecryptBytes instead.
Public Declare Function CIPHER_Bytes Lib "diCrPKI.dll"
(ByVal fEncrypt As Long, ByRef lpOutput As Byte, ByRef lpData As Byte, ByVal nDataLen As Long,
ByRef lpKey As Byte, ByRef lpIV As Byte,
ByVal strAlgAndMode As String, ByVal nOptions As Long) As Long
nRet = CIPHER_Bytes(fEncrypt, lpOutput(0), lpData(0), nDataLen, lpKey(0),
lpIV(0), strAlgAndMode, nOptions) ' Note the "(0)" after the byte array parameters
long __stdcall CIPHER_Bytes(long fEncrypt, unsigned char *lpOutput, const unsigned char *lpData, long nDataLen, const unsigned char *lpKey, const unsigned char *lpIV, const char *szAlgAndMode, long nOptions);
If successful, the return value is zero; otherwise it returns a nonzero error code.
Cipher.Encrypt Method (Byte[], Byte[], Byte[], CipherAlgorithm, Mode)
Cipher.Decrypt Method (Byte[], Byte[], Byte[], CipherAlgorithm, Mode)
The algorithm and mode must be specified using either the szAlgAndMode or nOptions parameter (see Specifying the algorithm and mode for generic block cipher functions). The length of key lpKey must be exactly the required key size, and the length of the IV, if required, exactly the block size. See Valid key and block sizes. Important: The output array lpOutput must be at least nDataLen bytes long. lpOutput and lpData may be the same.
For ECB and CBC modes, the length of the input data lpInput must be an exact multiple
of the block size otherwise a BAD_LENGTH_ERROR
error will result.
It is the responsibility of the user to provide suitable padding before encrypting in those modes
and to remove any padding after decrypting.
Note that we convert the input data from hex strings to byte arrays in this example just for convenience so we can quickly demonstrate how the function works. In practice, since you are using the "Bytes" function, we assume your input data would already be in byte array form. If all your data strings are already in hex string format and you want the output in hex, then use CIPHER_Hex instead.
Dim nRet As Long Dim strOutput As String Dim strInput As String Dim sCorrect As String Dim abKey() As Byte Dim abInitV() As Byte Dim abResult() As Byte Dim abData() As Byte Dim abCheck() As Byte Dim nDataLen As Long ' Set up input in byte arrays strInput = "Now is the time for all good men" sCorrect = "C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177" abKey = cnvBytesFromHexStr("0123456789ABCDEFF0E1D2C3B4A59687") abInitV = cnvBytesFromHexStr("FEDCBA9876543210FEDCBA9876543210") abData = StrConv(strInput, vbFromUnicode) nDataLen = UBound(abData) - LBound(abData) + 1 ' Pre-dimension output array ReDim abResult(nDataLen - 1) Debug.Print "KY=" & cnvHexStrFromBytes(abKey) Debug.Print "IV=" & cnvHexStrFromBytes(abInitV) Debug.Print "PT=" & strInput Debug.Print "PT=" & cnvHexStrFromBytes(abData) ' Encrypt in one-off process (abResult <-- abData) nRet = CIPHER_Bytes(ENCRYPT, abResult(0), abData(0), nDataLen, _ abKey(0), abInitV(0), "aes128-cbc", 0) Debug.Print "CIPHER_Bytes(ENCRYPT) returns " & nRet Debug.Print "CT=" & cnvHexStrFromBytes(abResult) Debug.Print "OK=" & sCorrect ' Now decrypt back (abCheck <-- abResult) ReDim abCheck(nDataLen - 1) nRet = CIPHER_Bytes(DECRYPT, abCheck(0), abResult(0), nDataLen, _ abKey(0), abInitV(0), "", PKI_BC_AES128 + PKI_MODE_CBC) Debug.Print "CIPHER_Bytes(DECRYPT) returns " & nRet ' And decode back from a byte array into a string Debug.Print "P'=" & cnvHexStrFromBytes(abCheck) strOutput = StrConv(abCheck(), vbUnicode) Debug.Print "P'=" & strOutput
This should result in output as follows:
KY=0123456789ABCDEFF0E1D2C3B4A59687 IV=FEDCBA9876543210FEDCBA9876543210 PT=Now is the time for all good men PT=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E CIPHER_Bytes(ENCRYPT) returns 0 CT=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177 OK=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177 CIPHER_Bytes(DECRYPT) returns 0 P'=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E P'=Now is the time for all good men