Decrypts data in a byte array using the specified block cipher algorithm, mode and padding.
@deprecated
use CIPHER_DecryptBytes2() instead.
Public Declare Function CIPHER_DecryptBytesPad 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_DecryptBytesPad(abOutput(0), nOutBytes, abInput(0), nInputLen, abKey(0),
abIV(0), strAlgModePad, nOptions)
long __stdcall CIPHER_DecryptBytesPad(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_DecryptBytes2().
@warning [Changed in v5.2] The padding type is now ignored for CTR, OFB and CFB modes. In earlier versions this function would have attempted to strip it after decryption. Now that's not done.
Dim key() As Byte
Dim iv() As Byte
Dim pn() As Byte
Dim ct() As Byte
Dim ok() As Byte
Dim p1() As Byte
Dim pnlen As Long
Dim ctlen As Long
Dim p1len As Long
key = cnvBytesFromHexStr("737C791F25EAD0E04629254352F7DC6291E5CB26917ADA32")
iv = cnvBytesFromHexStr("B36B6BFB6231084E")
Debug.Print ("KY=" & cnvHexStrFromBytes(key))
Debug.Print ("IV=" & cnvHexStrFromBytes(iv))
ct = cnvBytesFromHexStr("d76fd1178fbd02f84231f5c1d2a2f74a4159482964f675248254223daf9af8e4")
ctlen = UBound(ct) + 1
ok = cnvBytesFromHexStr("5468697320736F6D652073616D706520636F6E74656E742E")
' DECRYPT
' 1. Output from decryption is always <= length of input so allocate it first
p1len = ctlen
ReDim p1(p1len - 1)
' 2. Perform decryption including padding into output buffer
p1len = CIPHER_DecryptBytesPad(p1(0), p1len, ct(0), ctlen, key(0), iv(0), "Tdea/CBC/Pkcs5", 0)
Debug.Print "CIPHER_DecryptBytesPad returns " & p1len
' 3. Re-dimension the output to the correct length
ReDim Preserve p1(p1len - 1)
Debug.Print ("P'=" & cnvHexStrFromBytes(p1))
Debug.Print ("P'='" & StrConv(p1, vbUnicode) + "'")
Debug.Print "Check actual padding by decrypting with NoPadding..."
ReDim pn(ctlen - 1)
pnlen = CIPHER_DecryptBytesPad(pn(0), pnlen, ct(0), ctlen, key(0), iv(0), "Tdea/CBC/NoPadding", 0)
Debug.Print "CIPHER_DecryptBytesPad(NoPadding) returns " & pnlen
Debug.Print ("Pn=" & cnvHexStrFromBytes(pn))
This should result in output as follows:
KY=737C791F25EAD0E04629254352F7DC6291E5CB26917ADA32 IV=B36B6BFB6231084E CIPHER_DecryptBytesPad returns 24 P'=5468697320736F6D652073616D706520636F6E74656E742E P'='This some sampe content.' Check actual padding by decrypting with NoPadding... CIPHER_DecryptBytesPad(NoPadding) returns 32 Pn=5468697320736F6D652073616D706520636F6E74656E742E0808080808080808