TDEA_B64Mode encrypts or decrypts data
encoded in base64
using a specified mode. The key and initialization vector
are required in base64.
Public Declare Function TDEA_B64Mode Lib "diCrPKI.dll"
(ByVal strOutput As String, ByVal strInput As String, ByVal strKey As String,
ByVal fEncrypt As Long, ByVal strMode As String, ByVal strIV As String) As Long
nRet = TDEA_B64Mode(strOutput, strInput, strKey, fEncrypt, strMode, strIV)
String of sufficient length to receive the output.String containing the input data in base64.String containing the key in base64 format.Boolean direction flag:
set as ENCRYPT (True) to encrypt or DECRYPT (False)
to decrypt.String specifying the confidentiality mode:String containing the initialization vector (IV), if required,
in base64 format.
long _stdcall TDEA_B64Mode(char *szOutput, const char *szInput, const char *szKey, long fEncrypt, const char *szMode, const char *szIV);
Long: If successful, the return value is zero;
otherwise it returns a non-zero error code.
Tdea.Encrypt Method (String, String, Mode, String, EncodingBase)
Tdea.Decrypt Method (String, String, Mode, String, EncodingBase)
For ECB and CBC modes, the input string strInput must represent an exact multiple of eight bytes when decoded
otherwise a "Data not a valid length" error will result.
The key string strKey must represent exactly 24 bytes when decoded.
The initialization vector
string strIV must represent exactly 8 bytes when decoded
unless strMode is ECB,
in which case strIV is ignored (use "").
Valid base64 characters are [A-Za-z0-9+/] with '=' used as a padding character.
The output string strOutput must be set up with at least the same
number of characters as the input string before calling.
The variables strOutput and strInput should be different.
Examples in base64 are pretty obscure to read. This is the same example from
TDEA_HexMode
using base64 strings instead.
Dim nRet As Long Dim sHexCorrect As String Dim sHexInput As String Dim sHexKey As String Dim sHexInitV As String Dim sOutput As String Dim sInput As String Dim sKey As String Dim sInitV As String Dim bEncrypt As Boolean Dim sCorrect As String ' Start with input in hex sHexInput = "5468697320736F6D652073616D706520636F6E74656E742E0808080808080808" ' T h i s _ s o m e _ s a m p e _ c o n t e n t . (padding 8 x 08) sHexKey = "737C791F25EAD0E04629254352F7DC6291E5CB26917ADA32" sHexInitV = "B36B6BFB6231084E" sHexCorrect = "d76fd1178fbd02f84231f5c1d2a2f74a4159482964f675248254223daf9af8e4" ' Convert to base64 sInput = cnvB64StrFromBytes(cnvBytesFromHexStr(sHexInput)) sKey = cnvB64StrFromBytes(cnvBytesFromHexStr(sHexKey)) sInitV = cnvB64StrFromBytes(cnvBytesFromHexStr(sHexInitV)) sCorrect = cnvB64StrFromBytes(cnvBytesFromHexStr(sHexCorrect)) ' Set sOutput to be same length as sInput sOutput = String(Len(sInput), " ") Debug.Print "KY="; sKey Debug.Print "PT="; sInput Debug.Print "IV="; sInitV nRet = TDEA_B64Mode(sOutput, sInput, sKey, ENCRYPT, "CBC", sInitV) Debug.Print "CT="; sOutput; nRet Debug.Print "OK="; sCorrect sInput = sOutput nRet = TDEA_B64Mode(sOutput, sInput, sKey, DECRYPT, "CBC", sInitV) Debug.Print "P'="; sOutput; nRet
This should result in output as follows:
KY=c3x5HyXq0OBGKSVDUvfcYpHlyyaRetoy IV=s2tr+2IxCE4= PT=VGhpcyBzb21lIHNhbXBlIGNvbnRlbnQuCAgICAgICAg= CT=12/RF4+9AvhCMfXB0qL3SkFZSClk9nUkglQiPa+a+OQ= 0 OK=12/RF4+9AvhCMfXB0qL3SkFZSClk9nUkglQiPa+a+OQ= P'=VGhpcyBzb21lIHNhbXBlIGNvbnRlbnQuCAgICAgICAg= 0