CryptoSys API Library Manual

Example: Aes128.Decrypt  Aes128.Encrypt  (5)

[VB6 equivalent: 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

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

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)

See Also:
Aes128.Decrypt Method (String, String, Mode, String)
Aes128.Encrypt Method (String, String, Mode, String)

[Contents] [Index]

[PREV: Example: Aes128.Decrypt ...]   [Contents]   [Index]   
   [NEXT: Example: Aes128.ErrCode ...]

Copyright © 2001-23 D.I. Management Services Pty Ltd. All rights reserved. Generated 2023-05-20T13:01:10Z.