' 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.IO
Imports System.Text
Imports System.Security.Cryptography

Public Class MainClass
    Public Shared Sub Save(data As String, key As String, fnm As String)
        Dim stm As Stream = New FileStream(fnm, FileMode.Create, FileAccess.Write)
        Dim alg As Rijndael = Rijndael.Create
        alg.IV = New Byte() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
        alg.Key = Encoding.Default.GetBytes(key)
        Dim crypto As New CryptoStream(stm, alg.CreateEncryptor(), CryptoStreamMode.Write)
        Dim b As Byte() = Encoding.Default.GetBytes(data)
        crypto.Write(b, 0, b.Length)
        crypto.Close()
    End Sub
    Public Shared Function Load(fnm As String, key As String) As String
        Dim n As Integer = (New FileInfo(fnm)).Length
        Dim stm As Stream = New FileStream(fnm, FileMode.Open, FileAccess.Read)
        Dim alg As Rijndael = Rijndael.Create
        alg.IV = New Byte() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
        alg.Key = Encoding.Default.GetBytes(key)
        Dim crypto As New CryptoStream(stm, alg.CreateDecryptor(), CryptoStreamMode.Read)
        Dim b(n) As Byte
        n = crypto.Read(b, 0, b.Length)
        crypto.Close()
        return Encoding.Default.GetString(b, 0, n)
    End Function
    Public Shared Sub Main(ByVal args As String())
        Save("Dette er en lille test", "012345670123456701234567", "C:\cryp.dat")
        Console.WriteLine(Load("C:\cryp.dat", "012345670123456701234567"))
    End Sub
End Class
