Carries out the DES transformation function on a byte array
according to the direction
and mode set up by an earlier call to DES_Init()
or DES_InitHex()
.
Public Declare Function DES_Update Lib "diCryptoSys.dll"
(ByVal hContext As Long, ByRef lpData As Byte, ByVal nDataLen As Long) As Long
nRet = DES_Update(hContext, abData(0), nDataLen)
long __stdcall DES_Update(long hContext, unsigned char *lpData, long nDataLen);
DES_Init()
or DES_InitHex()
.If successful, the return value is 0; otherwise it returns a non-zero error code.
Des.Update Method (Byte[])
The input data lpData must be an exact multiple of 8 bytes long. If not, an error code will be returned. Note that the output overwrites the input.
' From FIPS 81 Appendix C ' AN EXAMPLE OF THE CIPHER BLOCK CHAINING (CBC) MODE Dim nRet As Long Dim hContext As Long Dim sCorrect As String Dim abKey() As Byte Dim abInitV() As Byte Dim abBlock() As Byte Dim vntPlain(2) As Variant Dim vntCipher(2) As Variant Dim i As Integer sCorrect = "683788499a7c05f6" abKey = cnvBytesFromHexStr("0123456789abcdef") abInitV = cnvBytesFromHexStr("1234567890abcdef") Debug.Print "KY=" & cnvHexStrFromBytes(abKey) Debug.Print "IV=" & cnvHexStrFromBytes(abInitV) ' The plain text is the ASCII code for "Now is the time for all ". ' We will store in an array of arrays (!) vntPlain(0) = cnvBytesFromHexStr("4e6f772069732074") vntPlain(1) = cnvBytesFromHexStr("68652074696d6520") vntPlain(2) = cnvBytesFromHexStr("666f7220616c6c20") ' Initialise context hContext = DES_Init(abKey(0), ENCRYPT, "CBC", abInitV(0)) If hContext = 0 Then nRet = DES_InitError() Debug.Print "DES_Init Failed: " & apiErrorLookup(nRet) Exit Function End If For i = 0 To 2 ' Get next block of plaintext from array abBlock = vntPlain(i) Debug.Print "PT(" & i & ")=" & cnvHexStrFromBytes(abBlock) nRet = DES_Update(hContext, abBlock(0), 8) Debug.Print "CT(" & i & ")=" & cnvHexStrFromBytes(abBlock) vntCipher(i) = abBlock Next Debug.Print "OK =" & sCorrect ' Clear the context nRet = DES_Final(hContext) ' Now do in decrypt mode hContext = DES_Init(abKey(0), DECRYPT, "CBC", abInitV(0)) If hContext = 0 Then nRet = DES_InitError() Debug.Print "DES_Init Failed: " & apiErrorLookup(nRet) Exit Function End If For i = 0 To 2 ' Get next block of ciphertext from array abBlock = vntCipher(i) Debug.Print "CT(" & i & ")=" & cnvHexStrFromBytes(abBlock) nRet = DES_Update(hContext, abBlock(0), 8) Debug.Print "PT(" & i & ")=" & cnvHexStrFromBytes(abBlock) & " [" & StrConv(abBlock, vbUnicode) & "]" Next nRet = DES_Final(hContext)
This should result in output as follows:
KY=0123456789ABCDEF IV=1234567890ABCDEF PT(0)=4E6F772069732074 CT(0)=E5C7CDDE872BF27C PT(1)=68652074696D6520 CT(1)=43E934008C389C0F PT(2)=666F7220616C6C20 CT(2)=683788499A7C05F6 OK =683788499a7c05f6 CT(0)=E5C7CDDE872BF27C PT(0)=4E6F772069732074 [Now is t] CT(1)=43E934008C389C0F PT(1)=68652074696D6520 [he time ] CT(2)=683788499A7C05F6 PT(2)=666F7220616C6C20 [for all ]
DES_Init
DES_InitHex
DES_UpdateHex
DES_Final