CryptoSys API examples VB6 to VB.NET

AES128_Hex

Encrypts or decrypts data represented as a hexadecimal string using a key represented in hexadecimal notation.

VB6/VBA

Debug.Print "Testing AES128_Hex ..."
Dim nRet As Long
Dim strOutput As String
Dim strInput As String
Dim strHexKey As String
Dim sPlain As String
Dim sCipher As String

'FIPS-197
'C.1 AES-128 (Nk=4, Nr=10)
'PLAINTEXT: 00112233445566778899aabbccddeeff
'KEY: 000102030405060708090a0b0c0d0e0f
strHexKey = "000102030405060708090a0b0c0d0e0f"
sPlain = "00112233445566778899aabbccddeeff"
sCipher = "69c4e0d86a7b0430d8cdb78070b4c55a"

strInput = sPlain
' Set strOutput to be same length as strInput
strOutput = String(Len(strInput), " ")

Debug.Print "KY=", strHexKey
Debug.Print "PT=", strInput
' Encrypt in one-off process
nRet = AES128_Hex(strOutput, strInput, strHexKey, ENCRYPT)
Debug.Print "CT=", strOutput, nRet
Debug.Print "OK=", sCipher
Debug.Assert (strOutput = sCipher)

' Now decrypt back to plain text
strInput = strOutput
nRet = AES128_Hex(strOutput, strInput, strHexKey, DECRYPT)
Debug.Print "P'=", strOutput, nRet
Debug.Assert (strOutput = sPlain)

Output

Testing AES128_Hex ...
KY=   000102030405060708090a0b0c0d0e0f
PT=   00112233445566778899aabbccddeeff
CT=   69C4E0D86A7B0430D8CDB78070B4C55A           0 
OK=   69c4e0d86a7b0430d8cdb78070b4c55a
P'=   00112233445566778899AABBCCDDEEFF           0 

VB.NET

Console.WriteLine("Testing AES128_Hex ...")
Dim nRet As Integer
Dim strOutput As String
Dim strInput As String
Dim strHexKey As String
Dim sPlain As String
Dim sCipher As String

'FIPS-197
'C.1 AES-128 (Nk=4, Nr=10)
'PLAINTEXT: 00112233445566778899aabbccddeeff
'KEY: 000102030405060708090a0b0c0d0e0f
strHexKey = "000102030405060708090a0b0c0d0e0f"
sPlain = "00112233445566778899aabbccddeeff"
sCipher = "69c4e0d86a7b0430d8cdb78070b4c55a"

strInput = sPlain
' Set strOutput to be same length as strInput
''strOutput = String(Len(strInput), " ")

Console.WriteLine("KY=" & " " & strHexKey)
Console.WriteLine("PT=" & " " & strInput)
' Encrypt in one-off process
strOutput = Aes128.Encrypt(strInput, strHexKey, Mode.ECB, Nothing)
Console.WriteLine("CT=" & " " & strOutput & " " & nRet)
Console.WriteLine("OK=" & " " & sCipher)
Debug.Assert(strOutput.ToLower = sCipher.ToLower)

' Now decrypt back to plain text
strInput = strOutput
strOutput = Aes128.Decrypt(strInput, strHexKey, Mode.ECB, Nothing)
Console.WriteLine("P'=" & " " & strOutput & " " & nRet)
Debug.Assert(strOutput.ToLower = sPlain.ToLower)

[Contents]

[HOME]   [NEXT: AES128_HexMode...]

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