Encrypts/decrypts a base64-encoded string using Triple DES with base64-encoded key and IV.
VB6/VBA
Debug.Print "Testing TDEA_B64Mode ..." 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
Output
Testing TDEA_B64Mode ... KY= c3x5HyXq0OBGKSVDUvfcYpHlyyaRetoy PT= VGhpcyBzb21lIHNhbXBlIGNvbnRlbnQuCAgICAgICAg= IV= s2tr+2IxCE4= CT= 12/RF4+9AvhCMfXB0qL3SkFZSClk9nUkglQiPa+a+OQ= 0 OK= 12/RF4+9AvhCMfXB0qL3SkFZSClk9nUkglQiPa+a+OQ= P'= VGhpcyBzb21lIHNhbXBlIGNvbnRlbnQuCAgICAgICAg= 0
VB.NET
Console.WriteLine("Testing TDEA_B64Mode ...")
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 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 = System.Convert.ToBase64String(Cnv.FromHex(sHexInput))
sKey = System.Convert.ToBase64String(Cnv.FromHex(sHexKey))
sInitV = System.Convert.ToBase64String(Cnv.FromHex(sHexInitV))
sCorrect = System.Convert.ToBase64String(Cnv.FromHex(sHexCorrect))
Console.WriteLine("KY=" & " " & sKey)
Console.WriteLine("PT=" & " " & sInput)
Console.WriteLine("IV=" & " " & sInitV)
sOutput = Tdea.Encrypt(sInput, sKey, Mode.CBC, sInitV, EncodingBase.Base64)
Console.WriteLine("CT=" & " " & sOutput & " " & General.ErrorCode)
Console.WriteLine("OK=" & " " & sCorrect)
sInput = sOutput
sOutput = Tdea.Decrypt(sInput, sKey, Mode.CBC, sInitV, EncodingBase.Base64)
Console.WriteLine("P'=" & " " & sOutput & " " & General.ErrorCode)
[Contents]