CryptoSys API examples VB6 to VB.NET

DES_HexMode

Encrypts or decrypts data represented as a hexadecimal string using a specified mode.

VB6/VBA

Debug.Print "Testing DES_HexMode ..."
Dim nRet As Long
Dim strOutput As String
Dim strInput As String
Dim strKey As String
Dim strIV As String
Dim sCorrect As String

' "Now is the time for all " in hex
strInput = "4e6f77206973207468652074696d6520666f7220616c6c20"
strKey = "0123456789abcdef"
strIV = "1234567890abcdef"
sCorrect = "e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6"
' 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_HexMode(strOutput, strInput, strKey, True, "CBC", strIV)
Debug.Print "CT=", strOutput
Debug.Print "OK=", sCorrect

' Now decrypt back to plain text
strInput = strOutput
nRet = DES_HexMode(strOutput, strInput, strKey, DECRYPT, "CBC", strIV)
Debug.Print "P'=", strOutput

Output

Testing DES_HexMode ...
KY=   0123456789abcdef
IV=   1234567890abcdef
PT=   4e6f77206973207468652074696d6520666f7220616c6c20
CT=   E5C7CDDE872BF27C43E934008C389C0F683788499A7C05F6
OK=   e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6
P'=   4E6F77206973207468652074696D6520666F7220616C6C20

VB.NET

Console.WriteLine("Testing DES_HexMode ...")
''Dim nRet As Integer
Dim strOutput As String
Dim strInput As String
Dim strKey As String
Dim strIV As String
Dim sCorrect As String

' "Now is the time for all " in hex
strInput = "4e6f77206973207468652074696d6520666f7220616c6c20"
strKey = "0123456789abcdef"
strIV = "1234567890abcdef"
sCorrect = "e5c7cdde872bf27c43e934008c389c0f683788499a7c05f6"
' Set strOutput to be same length as strInput
''strOutput = String(Len(strInput), " ")

Console.WriteLine("KY=" & " " & strKey)
Console.WriteLine("IV=" & " " & strIV)
Console.WriteLine("PT=" & " " & strInput)
' Encrypt in one-off process
strOutput = Des.Encrypt(strInput, strKey, Mode.CBC, strIV)
Console.WriteLine("CT=" & " " & strOutput)
Console.WriteLine("OK=" & " " & sCorrect)

' Now decrypt back to plain text
strInput = strOutput
strOutput = Des.Decrypt(strInput, strKey, Mode.CBC, strIV)
Console.WriteLine("P'=" & " " & strOutput)

[Contents]

[HOME]   [NEXT: GCM_Encrypt...]

Copyright © 2009-20 D.I. Management Services Pty Ltd. All rights reserved.