CryptoSys API examples VB6 to VB.NET

AES128_HexMode

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

VB6/VBA

Debug.Print "Testing AES128_HexMode ..."
Dim nRet As Long
Dim strOutput As String
Dim strInput As String
Dim strHexKey As String
Dim sPlain As String
Dim sCipher As String
Dim strHexIV As String
'Case #1: Encrypting 16 bytes (1 block) using AES-CBC with 128-bit key
'Key       : 0x06a9214036b8a15b512e03d534120006
'IV        : 0x3dafba429d9eb430b422da802c9fac41
'Plaintext : "Single block msg"
'Ciphertext: 0xe353779c1079aeb82708942dbe77181a

strHexKey = "06a9214036b8a15b512e03d534120006"
strHexIV = "3dafba429d9eb430b422da802c9fac41"
sPlain = cnvHexStrFromString("Single block msg")
sCipher = "e353779c1079aeb82708942dbe77181a"

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

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

' Decrypt to check
strInput = strOutput
nRet = AES128_HexMode(strOutput, strInput, strHexKey, DECRYPT, "CBC", strHexIV)
Debug.Print "P'=", strOutput, nRet
Debug.Print "OK=", sPlain
Debug.Assert (strOutput = sPlain)

' Case #4: Encrypting 64 bytes (4 blocks) using AES-CBC with 128-bit key
' Key       : 0x56e47a38c5598974bc46903dba290349
' IV        : 0x8ce82eefbea0da3c44699ed7db51b7d9
' Plaintext : 0xa0a1a2a3a4a5a6a7a8a9aaabacadaeaf
'               b0b1b2b3b4b5b6b7b8b9babbbcbdbebf
'               c0c1c2c3c4c5c6c7c8c9cacbcccdcecf
'               d0d1d2d3d4d5d6d7d8d9dadbdcdddedf
' Ciphertext: 0xc30e32ffedc0774e6aff6af0869f71aa
'               0f3af07a9a31a9c684db207eb0ef8e4e
'               35907aa632c3ffdf868bb7b29d3d46ad
'               83ce9f9a102ee99d49a53e87f4c3da55

strHexKey = "56e47a38c5598974bc46903dba290349"
strHexIV = "8ce82eefbea0da3c44699ed7db51b7d9"
sPlain = "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf" _
    & "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" _
    & "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" _
    & "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
sCipher = "c30e32ffedc0774e6aff6af0869f71aa" _
    & "0f3af07a9a31a9c684db207eb0ef8e4e" _
    & "35907aa632c3ffdf868bb7b29d3d46ad" _
    & "83ce9f9a102ee99d49a53e87f4c3da55"
    
strInput = sPlain
' Set strOutput to be same length as strInput
strOutput = String(Len(strInput), " ")

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

' Decrypt to check
strInput = strOutput
nRet = AES128_HexMode(strOutput, strInput, strHexKey, DECRYPT, "CBC", strHexIV)
Debug.Print "P'=", strOutput, nRet
Debug.Print "OK=", sPlain
Debug.Assert (strOutput = sPlain)

Output

Testing AES128_HexMode ...
KY=   06a9214036b8a15b512e03d534120006
IV=   3dafba429d9eb430b422da802c9fac41
PT=   53696E676C6520626C6F636B206D7367
CT=   E353779C1079AEB82708942DBE77181A           0 
OK=   e353779c1079aeb82708942dbe77181a
P'=   53696E676C6520626C6F636B206D7367           0 
OK=   53696E676C6520626C6F636B206D7367
KY=   56e47a38c5598974bc46903dba290349
IV=   8ce82eefbea0da3c44699ed7db51b7d9
PT=   a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf
CT=   C30E32FFEDC0774E6AFF6AF0869F71AA0F3AF07A9A31A9C684DB207EB0EF8E4E35907AA632C3FFDF868BB7B29D3D46AD83CE9F9A102EE99D49A53E87F4C3DA55             0 
OK=   c30e32ffedc0774e6aff6af0869f71aa0f3af07a9a31a9c684db207eb0ef8e4e35907aa632c3ffdf868bb7b29d3d46ad83ce9f9a102ee99d49a53e87f4c3da55
P'=   A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF             0 
OK=   a0a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedf

VB.NET

Console.WriteLine("Testing AES128_HexMode ...")
Dim nRet As Integer
Dim strOutput As String
Dim strInput As String
Dim strHexKey As String
Dim sPlain As String
Dim sCipher As String
Dim strHexIV As String
'Case #1: Encrypting 16 bytes (1 block) using AES-CBC with 128-bit key
'Key       : 0x06a9214036b8a15b512e03d534120006
'IV        : 0x3dafba429d9eb430b422da802c9fac41
'Plaintext : "Single block msg"
'Ciphertext: 0xe353779c1079aeb82708942dbe77181a

strHexKey = "06a9214036b8a15b512e03d534120006"
strHexIV = "3dafba429d9eb430b422da802c9fac41"
sPlain = Cnv.ToHex("Single block msg")
sCipher = "e353779c1079aeb82708942dbe77181a"

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

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

' Decrypt to check
strInput = strOutput
strOutput = Aes128.Decrypt(strInput, strHexKey, Mode.CBC, strHexIV)
Console.WriteLine("P'=" & strOutput & nRet)
Console.WriteLine("OK=" & sPlain)
Debug.Assert(strOutput.ToLower = sPlain.ToLower)

' Case #4: Encrypting 64 bytes (4 blocks) using AES-CBC with 128-bit key
' Key       : 0x56e47a38c5598974bc46903dba290349
' IV        : 0x8ce82eefbea0da3c44699ed7db51b7d9
' Plaintext : 0xa0a1a2a3a4a5a6a7a8a9aaabacadaeaf
'               b0b1b2b3b4b5b6b7b8b9babbbcbdbebf
'               c0c1c2c3c4c5c6c7c8c9cacbcccdcecf
'               d0d1d2d3d4d5d6d7d8d9dadbdcdddedf
' Ciphertext: 0xc30e32ffedc0774e6aff6af0869f71aa
'               0f3af07a9a31a9c684db207eb0ef8e4e
'               35907aa632c3ffdf868bb7b29d3d46ad
'               83ce9f9a102ee99d49a53e87f4c3da55

strHexKey = "56e47a38c5598974bc46903dba290349"
strHexIV = "8ce82eefbea0da3c44699ed7db51b7d9"
sPlain = "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf" _
    & "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" _
    & "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" _
    & "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
sCipher = "c30e32ffedc0774e6aff6af0869f71aa" _
    & "0f3af07a9a31a9c684db207eb0ef8e4e" _
    & "35907aa632c3ffdf868bb7b29d3d46ad" _
    & "83ce9f9a102ee99d49a53e87f4c3da55"

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

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

' Decrypt to check
strInput = strOutput
strOutput = Aes128.Decrypt(strInput, strHexKey, Mode.CBC, strHexIV)
Console.WriteLine("P'=" & strOutput & nRet)
Console.WriteLine("OK=" & sPlain)
Debug.Assert(strOutput.ToLower = sPlain.ToLower)

[Contents]

[HOME]   [NEXT: AES128_InitError...]

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