CryptoSys API examples VB6 to VB.NET

AES128_Bytes

Encrypts or decrypts an array of Bytes in one step in Electronic Codebook (EBC) mode.

VB6/VBA

Debug.Print "Testing AES128_Bytes ..."
Dim nRet As Long
Dim abBlock() As Byte
Dim abKey() As Byte
Dim abPlain() As Byte
Dim abCipher() As Byte
Dim nBytes As Long

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

' Convert input to bytes
abKey = cnvBytesFromHexStr("000102030405060708090a0b0c0d0e0f")
abPlain = cnvBytesFromHexStr("00112233445566778899aabbccddeeff")
abCipher = cnvBytesFromHexStr("69c4e0d86a7b0430d8cdb78070b4c55a")

abBlock = abPlain
nBytes = UBound(abBlock) - LBound(abBlock) + 1
Debug.Print "KY=", cnvHexStrFromBytes(abKey)
Debug.Print "PT=", cnvHexStrFromBytes(abBlock)
' Encrypt in one-off process
nRet = AES128_Bytes(abBlock(0), abBlock(0), nBytes, abKey(0), ENCRYPT)
Debug.Print "CT=", cnvHexStrFromBytes(abBlock)
Debug.Print "OK=", cnvHexStrFromBytes(abCipher)
Debug.Assert (StrConv(abBlock, vbUnicode) = StrConv(abCipher, vbUnicode))

' Now decrypt back to plain text
nRet = AES128_Bytes(abBlock(0), abBlock(0), nBytes, abKey(0), DECRYPT)
Debug.Print "P'=", cnvHexStrFromBytes(abBlock)
Debug.Assert (StrConv(abBlock, vbUnicode) = StrConv(abPlain, vbUnicode))

Output

Testing AES128_Bytes ...
KY=   000102030405060708090A0B0C0D0E0F
PT=   00112233445566778899AABBCCDDEEFF
CT=   69C4E0D86A7B0430D8CDB78070B4C55A
OK=   69C4E0D86A7B0430D8CDB78070B4C55A
P'=   00112233445566778899AABBCCDDEEFF

VB.NET

Console.WriteLine("Testing AES128_Bytes ...")
''Dim nRet As Integer
Dim abBlock() As Byte
Dim abKey() As Byte
Dim abPlain() As Byte
Dim abCipher() As Byte
''Dim nBytes As Integer

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

' Convert input to bytes
abKey = Cnv.FromHex("000102030405060708090a0b0c0d0e0f")
abPlain = Cnv.FromHex("00112233445566778899aabbccddeeff")
abCipher = Cnv.FromHex("69c4e0d86a7b0430d8cdb78070b4c55a")

abBlock = abPlain
''nBytes = UBound(abBlock) - LBound(abBlock) + 1
Console.WriteLine("KY=" & Cnv.ToHex(abKey))
Console.WriteLine("PT=" & Cnv.ToHex(abBlock))
' Encrypt in one-off process
abBlock = Aes128.Encrypt(abBlock, abKey, Mode.ECB, Nothing)
Console.WriteLine("CT=" & Cnv.ToHex(abBlock))
Console.WriteLine("OK=" & Cnv.ToHex(abCipher))
Debug.Assert(Cnv.ToHex(abBlock) = Cnv.ToHex(abCipher))

' Now decrypt back to plain text
abBlock = Aes128.Decrypt(abBlock, abKey, Mode.ECB, Nothing)
Console.WriteLine("P'=" & Cnv.ToHex(abBlock))
Debug.Assert(Cnv.ToHex(abBlock) = Cnv.ToHex(abPlain))

[Contents]

[HOME]   [NEXT: AES128_BytesMode...]

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