CryptoSys PKI examples VB6 to VB.NET

CIPHER_Bytes

Encrypts/decrypts an array of bytes using specified block cipher algorithm and mode.

VB6/VBA

Debug.Print "Testing CIPHER_Bytes ..."
Dim nRet As Long
Dim strOutput As String
Dim strInput As String
Dim sCorrect As String
Dim abKey()  As Byte
Dim abInitV() As Byte
Dim abResult() As Byte
Dim abData() As Byte
Dim abCheck() As Byte
Dim nDataLen As Long

' Set up input in byte arrays
strInput = "Now is the time for all good men"
sCorrect = "C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177"
abKey = cnvBytesFromHexStr("0123456789ABCDEFF0E1D2C3B4A59687")
abInitV = cnvBytesFromHexStr("FEDCBA9876543210FEDCBA9876543210")
abData = StrConv(strInput, vbFromUnicode)
nDataLen = UBound(abData) - LBound(abData) + 1

' Pre-dimension output array
ReDim abResult(nDataLen - 1)

Debug.Print "KY=" & cnvHexStrFromBytes(abKey)
Debug.Print "IV=" & cnvHexStrFromBytes(abInitV)
Debug.Print "PT=" & strInput
Debug.Print "PT=" & cnvHexStrFromBytes(abData)
' Encrypt in one-off process (abResult <-- abData)
nRet = CIPHER_Bytes(ENCRYPT, abResult(0), abData(0), nDataLen, _
    abKey(0), abInitV(0), "aes128-cbc", 0)
Debug.Print "CIPHER_Bytes(ENCRYPT) returns " & nRet
Debug.Print "CT=" & cnvHexStrFromBytes(abResult)
Debug.Print "OK=" & sCorrect

' Now decrypt back (abCheck <-- abResult)
ReDim abCheck(nDataLen - 1)
nRet = CIPHER_Bytes(DECRYPT, abCheck(0), abResult(0), nDataLen, _
    abKey(0), abInitV(0), "", PKI_BC_AES128 + PKI_MODE_CBC)
Debug.Print "CIPHER_Bytes(DECRYPT) returns " & nRet
' And decode back from a byte array into a string
Debug.Print "P'=" & cnvHexStrFromBytes(abCheck)
strOutput = StrConv(abCheck(), vbUnicode)
Debug.Print "P'=" & strOutput

Output

Testing CIPHER_Bytes ...
KY=0123456789ABCDEFF0E1D2C3B4A59687
IV=FEDCBA9876543210FEDCBA9876543210
PT=Now is the time for all good men
PT=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E
CIPHER_Bytes(ENCRYPT) returns 0
CT=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177
OK=C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177
CIPHER_Bytes(DECRYPT) returns 0
P'=4E6F77206973207468652074696D6520666F7220616C6C20676F6F64206D656E
P'=Now is the time for all good men

VB.NET

Console.WriteLine("Testing CIPHER_Bytes ...")
''Dim nRet As Integer
Dim strOutput As String
Dim strInput As String
Dim sCorrect As String
Dim abKey() As Byte
Dim abInitV() As Byte
Dim abResult() As Byte
Dim abData() As Byte
Dim abCheck() As Byte
''Dim nDataLen As Integer

' Set up input in byte arrays
strInput = "Now is the time for all good men"
sCorrect = "C3153108A8DD340C0BCB1DFE8D25D2320EE0E66BD2BB4A313FB75C5638E9E177"
abKey = Cnv.FromHex("0123456789ABCDEFF0E1D2C3B4A59687")
abInitV = Cnv.FromHex("FEDCBA9876543210FEDCBA9876543210")
abData = System.Text.Encoding.Default.GetBytes(strInput)
''nDataLen = UBound(abData) - LBound(abData) + 1

' Pre-dimension output array
''ReDim abResult(nDataLen - 1)

Console.WriteLine("KY=" & Cnv.ToHex(abKey))
Console.WriteLine("IV=" & Cnv.ToHex(abInitV))
Console.WriteLine("PT=" & strInput)
Console.WriteLine("PT=" & Cnv.ToHex(abData))
' Encrypt in one-off process (abResult <-- abData)
abResult = Cipher.Encrypt(abData, abKey, abInitV, CipherAlgorithm.Tdea, Mode.CBC)
''Console.WriteLine("CIPHER_Bytes(ENCRYPT) returns " & nRet)
Console.WriteLine("CT=" & Cnv.ToHex(abResult))
Console.WriteLine("OK=" & sCorrect)

' Now decrypt back (abCheck <-- abResult)
''ReDim abCheck(nDataLen - 1)
abCheck = Cipher.Decrypt(abResult, abKey, abInitV, CipherAlgorithm.Tdea, Mode.CBC)
''Console.WriteLine("CIPHER_Bytes(DECRYPT) returns " & nRet)
' And decode back from a byte array into a string
Console.WriteLine("P'=" & Cnv.ToHex(abCheck))
strOutput = System.Text.Encoding.Default.GetString(abCheck)
Console.WriteLine("P'=" & strOutput)

[Contents]

[HOME]   [NEXT: CIPHER_File...]

Copyright © 2010 D.I. Management Services Pty Ltd. All rights reserved.