Encrypts or decrypts data represented as a base64 string using a specified mode. The key and initialization vector are represented as base64 strings.
Public Declare Function DES_B64Mode Lib "diCryptoSys.dll"
(ByVal strOutput As String, ByVal strInput As String,
ByVal strKey As String, ByVal bEncrypt As Boolean,
ByVal strMode As String, ByVal strIV As String) As Long
nRet = DES_B64Mode(strOutput, strInput, strKey, bEncrypt, strMode, strIV)
String of sufficient length to receive the output.String containing the input data in base64.String containing the key in base64.Boolean direction flag:
set as True to encrypt or False
to decrypt.String specifying the confidentiality mode:String containing the initialization vector (IV), if required,
in base64.
long _stdcall DES_B64Mode(char *strOutput, const char *strInput,
const char *strKey, int bEncrypt, const char *strMode, const char *sIV);
Long: If successful, the return value is 0;
otherwise it returns a non-zero error code.
Des.Encrypt Method (String, String, Mode, String, EncodingBase)
Des.Decrypt Method (String, String, Mode, String, EncodingBase)
The length of the input string strInput must represent a multiple of the block size (8 bytes) when decoded.
If not, an error will be returned.
The initialization vector
string strIV must represent exactly 8 bytes
unless strMode is ECB, in which case strIV is ignored (use "").
The key strKey must also represent exactly 8 bytes, the required key length.
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.
Dim nRet As Long Dim strOutput As String Dim strInput As String Dim strKey As String Dim strIV As String Dim sCorrect As String ' Key: "0123456789abcdef" ' IV: "1234567890abcdef" ' Plaintext: "Now is the time for all " ' "4e6f77206973207468652074696d6520666f7220616c6c20" ' Ciphertext: "e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6" strInput = "Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwg" strKey = "ASNFZ4mrze8=" strIV = "EjRWeJCrze8=" sCorrect = "5cfN3ocr8nxD6TQAjDicD2g3iEmafAX2" ' Set strOutput to be same length as strInput strOutput = String(Len(strInput), " ") Debug.Print "KY="; strKey Debug.Print "IV="; strIV Debug.Print "PT="; strInput ' Encrypt in one-off process nRet = DES_B64Mode(strOutput, strInput, strKey, True, "CBC", strIV) Debug.Print "CT="; strOutput Debug.Print "OK="; sCorrect ' Now decrypt back to plain text strInput = strOutput nRet = DES_B64Mode(strOutput, strInput, strKey, DECRYPT, "CBC", strIV) Debug.Print "P'="; strOutput
This should result in output as follows:
KY=ASNFZ4mrze8= IV=EjRWeJCrze8= PT=Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwg CT=5cfN3ocr8nxD6TQAjDicD2g3iEmafAX2 OK=5cfN3ocr8nxD6TQAjDicD2g3iEmafAX2 P'=Tm93IGlzIHRoZSB0aW1lIGZvciBhbGwg