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
Imports System
Imports System.Text
Imports System.Security.Cryptography
Class MainClass
Public Shared Sub Main(ByVal args As String())
Dim utf As Encoding = New UTF8Encoding
Dim aes As Rijndael = New RijndaelManaged
Dim key As Byte() = utf.GetBytes("hemmeligabcdefgh12345678")
Dim iv As Byte() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
Dim encrypt As ICryptoTransform = aes.CreateEncryptor(key, iv)
Dim plain1 As String = "Dette er en lille test"
Dim cipher As Byte() = encrypt.TransformFinalBlock(utf.GetBytes(plain1), 0, utf.GetByteCount(plain1))
Dim i As Integer
For i = 0 To cipher.Length-1
Console.WriteLine(cipher(i))
Next
Dim decrypt As ICryptoTransform = aes.CreateDecryptor(key, iv)
Dim plain2 As String = utf.GetString(decrypt.TransformFinalBlock(cipher, 0, cipher.Length))
Console.WriteLine(plain2)
End Sub
End Class