CryptoSys API Library Manual


Encrypts data in a byte array using the specified block cipher algorithm, mode and padding.
@deprecated use CIPHER_EncryptBytes2() instead.

VB6/VBA Syntax

Public Declare Function CIPHER_EncryptBytesPad Lib "diCryptoSys.dll" (ByRef abOutput As Byte, ByVal nOutBytes As Long, ByRef abInput As Byte, ByVal nInputLen As Long, ByRef abKey As Byte, ByRef abIV As Byte, ByVal strAlgModePad As String, ByVal nOptions As Long) As Long

nRet = CIPHER_EncryptBytesPad(abOutput(0), nOutBytes, abInput(0), nInputLen, abKey(0), abIV(0), strAlgModePad, nOptions)

C/C++ Syntax

long __stdcall CIPHER_EncryptBytesPad(unsigned char *lpOutput, long nOutBytes, const unsigned char *lpInput, long nInputLen, const unsigned char *lpKey, const unsigned char *lpIV, const char *szAlgModePad, long nOptions);


[out] array of sufficient length to receive the output.
[in] specifying the length of the output buffer in bytes.
[in] array containing the input data.
[in] specifying the length of the input data in bytes.
[in] array containing the key.
[in] containing the initialization vector (IV), or zero (0) or NULL for ECB mode.
[in] containing the block cipher algorithm, mode and padding (see Specifying the algorithm, mode and padding for generic block cipher functions).
[in] option flags.

Returns (VB6/C)

If successful, the return value is the number of bytes required in the output; otherwise it returns a negative error code.

.NET Equivalent

Cipher.Encrypt Method


See the remarks for CIPHER_EncryptBytes2().

@warning [Changed in v5.2] The padding type is now ignored for CTR, OFB and CFB modes. In earlier versions it would have been added regardless even though it wasn't needed. Now that's not done.


Dim key() As Byte
Dim iv() As Byte
Dim pt() As Byte
Dim ct() As Byte
Dim ok() As Byte
Dim keylen As Long
Dim ivlen As Long
Dim ptlen As Long
Dim ctlen As Long

key = cnvBytesFromHexStr("737C791F25EAD0E04629254352F7DC6291E5CB26917ADA32")
keylen = Ubound(key) + 1
iv = cnvBytesFromHexStr("B36B6BFB6231084E")
ivlen = Ubound(iv) + 1
Debug.Print ("KY=" & cnvHexStrFromBytes(key))
Debug.Print ("IV=" & cnvHexStrFromBytes(iv))
pt = cnvBytesFromHexStr("5468697320736F6D652073616D706520636F6E74656E742E")
ptlen = Ubound(pt) + 1
ok = cnvBytesFromHexStr("d76fd1178fbd02f84231f5c1d2a2f74a4159482964f675248254223daf9af8e4")
Debug.Print ("PT=" & cnvHexStrFromBytes(pt))
Debug.Print ("PT='" & StrConv(pt, vbUnicode) + "'")
' 1. Find out how long an output buffer we need
ctlen = CIPHER_EncryptBytesPad(0, 0, pt(0), ptlen, key(0), iv(0), "Tdea/CBC/Pkcs5", 0)
Debug.Print "CIPHER_EncryptBytesPad returns " & ctlen
' 2. Allocate the buffer
ReDim ct(ctlen - 1)
' 3. Encrypt to output buffer
ctlen = CIPHER_EncryptBytesPad(ct(0), ctlen, pt(0), ptlen, key(0), iv(0), "Tdea/CBC/Pkcs5", 0)

Debug.Print ("CT=" & cnvHexStrFromBytes(ct))
Debug.Print ("OK=" & cnvHexStrFromBytes(ok))

This should result in output as follows:

PT='This some sampe content.'
CIPHER_EncryptBytesPad returns 32

See Also


[Contents] [Index]

[PREV: CIPHER_EncryptBytes2...]   [Contents]   [Index]   
   [NEXT: CIPHER_FileDecrypt...]

Copyright © 2001-18 D.I. Management Services Pty Ltd. All rights reserved.