Encrypts or decrypts a chunk of input (in incremental mode).
Public Declare Function CIPHER_Update Lib "diCryptoSys.dll" (ByVal hContext As Long, ByRef lpOutput As Byte, ByVal nOutBytes As Long, ByRef lpData As Byte, ByVal nDataLen As Long) As Long
nRet = CIPHER_Update(hContext, lpOutput(0), nOutBytes, lpData(0), nDataLen)
long __stdcall CIPHER_Update(long hContext, unsigned char *lpOutput, long nOutBytes, const unsigned char *lpData, long nDataLen);
If successful, the return value is 0; otherwise it returns a non-zero error code.
Public Function cipherUpdate
(hContext As Long, lpData() As Byte) As Byte()
Cipher.Update Method (Byte[])
crsysapi::bvec_t crsysapi::Cipher::Update (const bvec_t &data)
The handle hContext to the CIPHER context must have been set up with a
prior call to CIPHER_Init
or CIPHER_InitHex
.
The chunks of data to be en/decrypted must be passed to the Update function in sequential order and only in lengths which are multiples of the block size (16 bytes for AES, 8 for Triple DES). For ECB and CBC modes, the last block in the sequence must be suitably padded to a length a multiple of the block size. For other modes, e.g. CTR, the last block in the sequence may be shorter than a multiple of the block length. However, once this final, shorter block has been processed, the Update function can no longer be used (well it can, but the output is wrong).
' Using the "raw" functions Dim key() As Byte Dim iv() As Byte Dim pt() As Byte Dim ct() As Byte Dim ok() As Byte Dim ptlen As Long Dim ctlen As Long Dim keylen As Long Dim ivlen As Long Dim algstr As String Dim hContext As Long Dim nRet As Long key = cnvBytesFromHexStr("0123456789ABCDEFF0E1D2C3B4A59687") iv = cnvBytesFromHexStr("FEDCBA9876543210FEDCBA9876543210") keylen = cnvBytesLen(key) ivlen = cnvBytesLen(iv) Debug.Print "KY=" & cnvHexStrFromBytes(key) Debug.Print "IV=" & cnvHexStrFromBytes(iv) ' TEST 1 - CBC mode algstr = "Aes128/CBC" Debug.Print algstr ' Initialize context for repeated chunks hContext = CIPHER_Init(ENCRYPT, algstr, key(0), keylen, iv(0), ivlen, 0) Debug.Print "CIPHER_Init returns 0x" & Hex(hContext) & " (expected NON-zero)" Debug.Assert hContext <> 0 ' Pass data in chunks of 16 bytes (or multiples of) ' Part 1... pt = StrConv("Now is the time for all good men", vbFromUnicode) Debug.Print "PT=" & cnvHexStrFromBytes(pt) ptlen = cnvBytesLen(pt) ctlen = ptlen ReDim ct(ctlen - 1) nRet = CIPHER_Update(hContext, ct(0), ctlen, pt(0), ptlen) Debug.Print "CIPHER_Update returns " & nRet & " (expected 0)" Debug.Print "CT=" & cnvHexStrFromBytes(ct) ' Part 2... pt = StrConv(" to come to the aid of the party", vbFromUnicode) Debug.Print "PT=" & cnvHexStrFromBytes(pt) ptlen = cnvBytesLen(pt) ctlen = ptlen ReDim ct(ctlen - 1) nRet = CIPHER_Update(hContext, ct(0), ctlen, pt(0), ptlen) Debug.Print "CIPHER_Update returns " & nRet & " (expected 0)" Debug.Print "CT=" & cnvHexStrFromBytes(ct) ' We are done nRet = CIPHER_Final(hContext) Debug.Print "CIPHER_Final returns " & nRet & " (expected 0)"
' Using the wrapper functions Dim key() As Byte Dim iv() As Byte Dim pt() As Byte Dim ct() As Byte Dim algstr As String Dim hContext As Long Dim nRet As Long key = cnvBytesFromHexStr("0123456789ABCDEFF0E1D2C3B4A59687") iv = cnvBytesFromHexStr("FEDCBA9876543210FEDCBA9876543210") Debug.Print "KY=" & cnvHexStrFromBytes(key) Debug.Print "IV=" & cnvHexStrFromBytes(iv) ' TEST 1 - CBC mode algstr = "Aes128/CBC" Debug.Print algstr ' Initialize context for repeated chunks hContext = cipherInit(ENCRYPT, algstr, key, iv) Debug.Print "cipherInit returns 0x" & Hex(hContext) & " (expected NON-zero)" Debug.Assert hContext <> 0 ' Pass data in chunks of 16 bytes (or multiples of) ' Part 1... pt = StrConv("Now is the time for all good men", vbFromUnicode) Debug.Print "PT=" & cnvHexStrFromBytes(pt) ct = cipherUpdate(hContext, pt) Debug.Print "CT=" & cnvHexStrFromBytes(ct) ' Part 2... pt = StrConv(" to come to the aid of the party", vbFromUnicode) Debug.Print "PT=" & cnvHexStrFromBytes(pt) ct = cipherUpdate(hContext, pt) Debug.Print "CT=" & cnvHexStrFromBytes(ct) ' We are done nRet = cipherFinal(hContext) Debug.Print "cipherFinal returns " & nRet & " (expected 0)" ' TEST 2 - ECB mode algstr = "Aes128/ecb" Debug.Print algstr Dim dummy() As Byte ' Initialize context for repeated chunks hContext = cipherInit(ENCRYPT, algstr, key, dummy) Debug.Print "cipherInit returns 0x" & Hex(hContext) & " (expected NON-zero)" Debug.Assert hContext <> 0 ' Pass data in chunks of 16 bytes (or multiples of) ' Part 1... pt = StrConv("Now is the time for all good men", vbFromUnicode) Debug.Print "PT=" & cnvHexStrFromBytes(pt) ct = cipherUpdate(hContext, pt) Debug.Print "CT=" & cnvHexStrFromBytes(ct) ' Part 2... pt = StrConv(" to come to the aid of the party", vbFromUnicode) Debug.Print "PT=" & cnvHexStrFromBytes(pt) ct = cipherUpdate(hContext, pt) Debug.Print "CT=" & cnvHexStrFromBytes(ct) ' We are done nRet = cipherFinal(hContext) Debug.Assert nRet = 0 ' TEST 3 - CTR mode algstr = "Aes128/ctr" Debug.Print algstr ' Initialize context for repeated chunks hContext = cipherInit(ENCRYPT, algstr, key, iv) Debug.Print "cipherInit returns 0x" & Hex(hContext) & " (expected NON-zero)" Debug.Assert hContext <> 0 ' Pass data in chunks of 16 bytes (or multiples of) ' Part 1... pt = StrConv("Now is the time for all good men", vbFromUnicode) Debug.Print "PT=" & cnvHexStrFromBytes(pt) ct = cipherUpdate(hContext, pt) Debug.Print "CT=" & cnvHexStrFromBytes(ct) ' Part 2... ' In CTR mode (and OFB and CFB) the last block need not be a multiple pt = StrConv(" to come to the aid of", vbFromUnicode) Debug.Print "PT=" & cnvHexStrFromBytes(pt) ct = cipherUpdate(hContext, pt) Debug.Print "CT=" & cnvHexStrFromBytes(ct) ' We are done nRet = cipherFinal(hContext) Debug.Assert nRet = 0
KY=0123456789ABCDEFF0E1D2C3B4A59687 IV=FEDCBA9876543210FEDCBA9876543210 Aes128/CBC cipherInit returns 0x2AB4B01D (expected NON-zero) PT=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E CT=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177 PT=20746F20636F6D6520746F2074686520616964206F6620746865207061727479 CT=211FC26A1FF51CE35741B76A77DB6DE21EE94708B546919999A4557F43AAA2BB cipherFinal returns 0 (expected 0) Aes128/ecb cipherInit returns 0x243820F9 (expected NON-zero) PT=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E CT=F0D1AD6F901FFFAE5572A6928DAB52B064B25C79F876730321E36DC01011ACCE PT=20746F20636F6D6520746F2074686520616964206F6620746865207061727479 CT=02D6F240643271217758C44862BB1B6862CD10757387256BC678A1708483862A Aes128/ctr cipherInit returns 0x5FFB439 (expected NON-zero) PT=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E CT=3FAC68CBAE6D774151306E9DB16CE0191C51E91959DA4F082B7CE3498C2D20D7 PT=20746F20636F6D6520746F2074686520616964206F66 CT=8437EC92088FE4C19FB49BDF2BADF7C7FD6FB9A7D52A