Enciphers data in array of bytes using specified stream cipher.
Public Declare Function CIPHER_StreamBytes Lib "diCryptoSys.dll" (ByRef lpOutput As Byte, ByRef lpData As Byte, ByVal nDataLen As Long, ByRef lpKey As Byte, ByVal nKeyLen As Long, ByRef lpIV As Byte, ByVal nIvLen As Long, ByVal nCounter As Long, ByVal nOptions As Long) As Long
nRet = CIPHER_StreamBytes(lpOutput(0), abData(0), nDataLen, abKey(0), nKeyLen, abIV(0), nIvLen, nCounter, nOptions)
long __stdcall CIPHER_StreamBytes(unsigned char *lpOutput, unsigned char *lpData, long nDataLen, unsigned char *lpKey, long nKeyLen, unsigned char *lpIV, long nIvLen, long nCounter, long nOptions);
If successful, the return value is zero; otherwise it returns a nonnegative error code.
Public Function cipherStreamBytes
(lpInput() As Byte, lpKey() As Byte, lpIV() As Byte, nOptions As Long, Optional nCounter As Long = 0) As Byte()
static crsysapi::bvec_t crsysapi::CipherStream::Bytes (const bvec_t &data, const bvec_t &key, const bvec_t &iv, Alg alg, int counter=0)
static CipherStream.bytes(data, key, iv, alg, counter=0)
This performs a one-off encryption over a byte array using the specified stream cipher. The key and IV are passed as byte arrays. The output will be exactly the same length as the input. The output buffer lpOutput may be the same as the input lpInput.
null
for IV.Dim abKey() As Byte Dim abInput() As Byte Dim lpOutput() As Byte Dim nRet As Long Dim nDataLen As Long Dim nKeyLen As Long Dim strCorrect As String ' Arcfour test vector abKey = cnvBytesFromHexStr("0123456789abcdef") abInput = cnvBytesFromHexStr("0123456789abcdef") strCorrect = "75b7878099e0c596" ReDim lpOutput(UBound(abInput)) nDataLen = UBound(abInput) + 1 nKeyLen = UBound(abKey) + 1 Debug.Print "KY=" & cnvHexStrFromBytes(abKey) Debug.Print "PT=" & cnvHexStrFromBytes(abInput) ' Encipher using Arcfour nRet = CIPHER_StreamBytes(lpOutput(0), abInput(0), nDataLen, abKey(0), nKeyLen, ByVal 0&, 0, 0, API_SC_ARCFOUR) Debug.Print "CT=" & cnvHexStrFromBytes(lpOutput) Debug.Print "OK=" & strCorrect ' Now decipher just by calling again. Use same output as input. nRet = CIPHER_StreamBytes(lpOutput(0), lpOutput(0), nDataLen, abKey(0), nKeyLen, ByVal 0&, 0, 0, API_SC_ARCFOUR) Debug.Print "P'=" & cnvHexStrFromBytes(lpOutput)
This should result in output as follows:
KY=0123456789ABCDEF PT=0123456789ABCDEF CT=75B7878099E0C596 OK=75b7878099e0c596 P'=0123456789ABCDEF
' ChaCha20 test vector from "ChaCha20 and Poly1305 for IETF protocols" Dim abKey() As Byte Dim abInput() As Byte Dim lpOutput() As Byte Dim abIV() As Byte Dim nRet As Long Dim nDataLen As Long Dim nKeyLen As Long Dim nIvLen As Long Dim strCorrect As String Dim strData As String ' Obtain parameters in byte array format abKey = cnvBytesFromHexStr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f") abIV = cnvBytesFromHexStr("000000000000004a00000000") strData = _ "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it." abInput = StrConv(strData, vbFromUnicode) strCorrect = _ "6E2E359A2568F98041BA0728DD0D6981E97E7AEC1D4360C20A27AFCCFD9FAE0BF91B65C5524733AB8F593DABCD62B3571639D624E65152AB8F530C359F0861D807CA0DBF500D6A6156A38E088A22B65E52BC514D16CCF806818CE91AB77937365AF90BBF74A35BE6B40B8EEDF2785E42874D" ReDim lpOutput(UBound(abInput)) nDataLen = UBound(abInput) + 1 nKeyLen = UBound(abKey) + 1 nIvLen = UBound(abIV) + 1 Debug.Print "KY=" & cnvHexStrFromBytes(abKey) Debug.Print "IV=" & cnvHexStrFromBytes(abIV) Debug.Print "PT=" & cnvHexStrFromBytes(abInput) ' Encipher using ChaCha20 with counter=1 nRet = CIPHER_StreamBytes(lpOutput(0), abInput(0), nDataLen, abKey(0), nKeyLen, abIV(0), nIvLen, 1, API_SC_CHACHA20) Debug.Print "CIPHER_StreamBytes returns " & nRet & " (expecting 0)" Debug.Print "CT=" & cnvHexStrFromBytes(lpOutput) Debug.Print "OK=" & strCorrect ' Now decipher just by calling again. Use same output as input. nRet = CIPHER_StreamBytes(lpOutput(0), lpOutput(0), nDataLen, abKey(0), nKeyLen, abIV(0), nIvLen, 1, API_SC_CHACHA20) Debug.Print "P'=" & cnvHexStrFromBytes(lpOutput) Debug.Print "P'=" & StrConv(lpOutput, vbUnicode)
This should result in output as follows:
KY=000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F IV=000000000000004A00000000 PT=4C616469657320616E642047656E746C656D656E206F662074686520636C617373206F66202739393A204966204920636F756C64206F6666657220796F75206F6E6C79206F6E652074697020666F7220746865206675747572652C2073756E73637265656E20776F756C642062652069742E CIPHER_StreamBytes returns 0 (expecting 0) CT=6E2E359A2568F98041BA0728DD0D6981E97E7AEC1D4360C20A27AFCCFD9FAE0BF91B65C5524733AB8F593DABCD62B3571639D624E65152AB8F530C359F0861D807CA0DBF500D6A6156A38E088A22B65E52BC514D16CCF806818CE91AB77937365AF90BBF74A35BE6B40B8EEDF2785E42874D OK=6E2E359A2568F98041BA0728DD0D6981E97E7AEC1D4360C20A27AFCCFD9FAE0BF91B65C5524733AB8F593DABCD62B3571639D624E65152AB8F530C359F0861D807CA0DBF500D6A6156A38E088A22B65E52BC514D16CCF806818CE91AB77937365AF90BBF74A35BE6B40B8EEDF2785E42874D P'=4C616469657320616E642047656E746C656D656E206F662074686520636C617373206F66202739393A204966204920636F756C64206F6666657220796F75206F6E6C79206F6E652074697020666F7220746865206675747572652C2073756E73637265656E20776F756C642062652069742E P'=Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it.
Dim lpCipher() As Byte Dim lpPlain() As Byte Dim lpKey() As Byte Dim lpIV() As Byte Dim strData As String lpKey = cnvBytesFromHexStr("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f") lpIV = cnvBytesFromHexStr("000000000000004a00000000") strData = _ "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it." lpPlain = StrConv(strData, vbFromUnicode) ' Encode as bytes ' Encipher using ChaCha20 with counter=1 lpCipher = cipherStreamBytes(lpPlain, lpKey, lpIV, API_SC_CHACHA20, 1) Debug.Print "CT=" & cnvHexStrFromBytes(lpCipher) Debug.Print "OK=6E2E359A2568F98041BA0728DD0D6981E97E7AEC1D4360C20A27AFCCFD9FAE0BF91B65C5524733AB8F593DABCD62B3571639D624E65152AB8F" _ & "530C359F0861D807CA0DBF500D6A6156A38E088A22B65E52BC514D16CCF806818CE91AB77937365AF90BBF74A35BE6B40B8EEDF2785E42874D" ' Now decipher just by calling again. ... lpPlain = cipherStreamBytes(lpCipher, lpKey, lpIV, API_SC_CHACHA20, 1) Debug.Print "PT=" & StrConv(lpPlain, vbUnicode) ' Decode bytes to string ' Arcfour test vector lpKey = cnvBytesFromHexStr("0123456789abcdef") lpPlain = cnvBytesFromHexStr("0123456789abcdef") lpIV = vbNullString ' Empty (null) array ' Encipher using Arcfour lpCipher = cipherStreamBytes(lpPlain, lpKey, lpIV, API_SC_ARCFOUR) Debug.Print "CT=" & cnvHexStrFromBytes(lpCipher) Debug.Print "OK=75b7878099e0c596"
CIPHER_StreamHex
CIPHER_StreamInit