Sample code for 30+ languages & platforms
VB.NET

Example: Crypt2.RandomizeIV method

Demonstrates using a random initialization vector for AES GCM encryption.

Chilkat VB.NET Downloads

VB.NET
Dim success As Boolean = False

' This example assumes the Chilkat API to have been previously unlocked.
' See Global Unlock Sample for sample code.

Dim crypt As New Chilkat.Crypt2

crypt.CryptAlgorithm = "aes"
crypt.CipherMode = "gcm"
crypt.KeyLength = 256

Dim K As String = "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"
Dim AAD As String = "feedfacedeadbeeffeedfacedeadbeefabaddad2"
Dim PT As String = "This is the text to be AES-GCM encrypted."

' Generate a random IV.
crypt.RandomizeIV()
Dim IV As String = crypt.GetEncodedIV("hex")

crypt.SetEncodedKey(K,"hex")

success = crypt.SetEncodedAad(AAD,"hex")

' Return the encrypted bytes as base64
crypt.EncodingMode = "base64"
crypt.Charset = "utf-8"
Dim cipherText As String = crypt.EncryptStringENC(PT)
If (crypt.LastMethodSuccess <> True) Then
    Debug.WriteLine(crypt.LastErrorText)
    Exit Sub
End If


' Get the GCM authenticated tag computed when encrypting.
Dim authTag As String = crypt.GetEncodedAuthTag("base64")

Debug.WriteLine("Cipher Text: " & cipherText)
Debug.WriteLine("Auth Tag: " & authTag)

' Let's send the IV, CipherText, and AuthTag to the decrypting party.
' We'll send them concatenated like this: [IV || Ciphertext || AuthTag]
' In base64 format.
Dim bdEncrypted As New Chilkat.BinData
bdEncrypted.AppendEncoded(IV,"hex")
bdEncrypted.AppendEncoded(cipherText,"base64")
bdEncrypted.AppendEncoded(authTag,"base64")

Dim concatenatedGcmOutput As String = bdEncrypted.GetEncoded("base64")
Debug.WriteLine("Concatenated GCM Output: " & concatenatedGcmOutput)

' Sample output so far:

' -------------------------------------------------------------------------------------
' Now let's GCM decrypt...
' -------------------------------------------------------------------------------------

Dim decrypt As New Chilkat.Crypt2

' The values shared and agreed upon by both sides beforehand are: algorithm, cipher mode, secret key, and AAD.
' Sometimes the IV can be a value already known and agreed upon, but in this case the encryptor sends the IV to the decryptor.
decrypt.CryptAlgorithm = "aes"
decrypt.CipherMode = "gcm"
decrypt.KeyLength = 256
decrypt.SetEncodedKey(K,"hex")
decrypt.SetEncodedAad(AAD,"hex")

Dim bdFromEncryptor As New Chilkat.BinData
bdFromEncryptor.AppendEncoded(concatenatedGcmOutput,"base64")

Dim sz As Integer = bdFromEncryptor.NumBytes

' Extract the parts.
Dim extractedIV As String = bdFromEncryptor.GetEncodedChunk(0,16,"hex")
Dim extractedCipherText As String = bdFromEncryptor.GetEncodedChunk(16,sz - 32,"base64")
Dim expectedAuthTag As String = bdFromEncryptor.GetEncodedChunk(sz - 16,16,"base64")

' Before GCM decrypting, we must set the authenticated tag to the value that is expected.
' The decryption will fail if the resulting authenticated tag is not equal to the expected result.
success = decrypt.SetEncodedAuthTag(expectedAuthTag,"base64")

' Also set the IV.
decrypt.SetEncodedIV(extractedIV,"hex")

' Decrypt..
decrypt.EncodingMode = "base64"
decrypt.Charset = "utf-8"
Dim decryptedText As String = decrypt.DecryptStringENC(extractedCipherText)
If (decrypt.LastMethodSuccess <> True) Then
    ' Failed.  The resultant authenticated tag did not equal the expected authentication tag.
    Debug.WriteLine(decrypt.LastErrorText)
    Exit Sub
End If


Debug.WriteLine("Decrypted: " & decryptedText)