Encrypts data in a byte array using the specified block cipher algorithm, mode and padding.
@deprecated
use CIPHER_EncryptBytes2() instead.
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)
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);
NULL
for ECB mode.If successful, the return value is the number of bytes required in the output; otherwise it returns a negative error code.
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:
KY=737C791F25EAD0E04629254352F7DC6291E5CB26917ADA32 IV=B36B6BFB6231084E PT=5468697320736F6D652073616D706520636F6E74656E742E PT='This some sampe content.' CIPHER_EncryptBytesPad returns 32 CT=D76FD1178FBD02F84231F5C1D2A2F74A4159482964F675248254223DAF9AF8E4 OK=D76FD1178FBD02F84231F5C1D2A2F74A4159482964F675248254223DAF9AF8E4