' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.Text
Imports System.Security.Cryptography

Public Class MainClass
    Private Shared utf As Encoding = New UTF8Encoding()
    Private Shared aes As Rijndael = New RijndaelManaged()
    Private Shared iv As Byte() = {1, 2, 3, 4, 5, 6, _
        7, 8, 9, 10, 11, 12, _
        13, 14, 15, 16}
    Public Shared Function Encrypt(plain As String, key As String) As String
        Dim aesenc As ICryptoTransform = aes.CreateEncryptor(utf.GetBytes(key), iv)
        Return Convert.ToBase64String(aesenc.TransformFinalBlock(utf.GetBytes(plain), 0, utf.GetByteCount(plain)))
    End Function
    Public Shared Function Decrypt(cipher As String, key As String) As String
        Dim aesdec As ICryptoTransform = aes.CreateDecryptor(utf.GetBytes(key), iv)
        Dim b As Byte() = Convert.FromBase64String(cipher)
        Return utf.GetString(aesdec.TransformFinalBlock(b, 0, b.Length))
    End Function
    Public Shared Sub Main(args As String())
        Console.WriteLine(Encrypt("Dette er en test !", "hemmeligabcdefgh12345678"))
        Console.WriteLine(Decrypt(Encrypt("Dette er en test !", "hemmeligabcdefgh12345678"), "hemmeligabcdefgh12345678"))
    End Sub
End Class
