VB.net AES Encryption Modul

Benötigt: .Net Framework 3.5 (AES Cryptosystem)

Test Programm

Imports Encrpyt.CryptoAES.Crypto
Module Module1
    Sub Main()
        Dim enc = New Crypto
        enc.key = "x4!UnUfugeyaprachuT8DesAst55ew!2"
        Dim cstring As String = enc.EncryptString("I like encryption!")
        Console.WriteLine("Encrypted: " & cstring)
        Console.WriteLine("Decrypted: " & enc.DecryptString(cstring))
        Dim k = Console.ReadKey()
    End Sub
End Module

Encryption Modul

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Module CryptoAES
    Public Class Crypto
        Private _key As String
        Public Property key() As String
            Get
                Return _key
            End Get
            Set(ByVal value As String)
                _key = value
            End Set
        End Property
        Public Sub EncryptFile(ByVal sInputFilename As String, _
                       ByVal sOutputFilename As String, _
                       ByVal sKey As String)
            Dim fsInput As New FileStream(sInputFilename, _
                                        FileMode.Open, FileAccess.Read)
            Dim fsEncrypted As New FileStream(sOutputFilename, _
                                        FileMode.Create, FileAccess.Write)
            Dim AES As New AesCryptoServiceProvider()
            'Set secret key for AES algorithm.
            'A 256-bit key and an IV are required for this provider.
            AES.KeySize = 256
            AES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
            'Set the initialization vector.
            AES.IV = ASCIIEncoding.ASCII.GetBytes(Left(sKey, 16))
            'Create the AES encryptor from this instance.
            Dim aesencrypt As ICryptoTransform = AES.CreateEncryptor()
            'Create the crypto stream that transforms the file stream by using AES encryption.
            Dim cryptostream As New CryptoStream(fsEncrypted, _
                                                aesencrypt, _
                                                CryptoStreamMode.Write)
            'Read the file text to the byte array.
            Dim bytearrayinput(CInt(fsInput.Length - 1)) As Byte
            fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
            'Write out the AES encrypted file.
            cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
            cryptostream.Close()
            fsInput.Close()
        End Sub
        Public Sub DecryptFile(ByVal sInputFilename As String, _
            ByVal sOutputFilename As String, _
            ByVal sKey As String)
            Dim AES As New AesCryptoServiceProvider()
            'A 64-bit key and an IV are required for this provider.
            'Set the secret key for the AES algorithm.
            AES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
            'Set the initialization vector.
            AES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
            'Create the file stream to read the encrypted file back.
            Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
            'Create the AES decryptor from the AES instance.
            Dim aesdecrypt As ICryptoTransform = AES.CreateDecryptor()
            'Create the crypto stream set to read and to do a AES decryption transform on incoming bytes.
            Dim cryptostreamDecr As New CryptoStream(fsread, aesdecrypt, CryptoStreamMode.Read)
            'Print out the contents of the decrypted file.
            Dim fsDecrypted As New StreamWriter(sOutputFilename)
            fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
            fsDecrypted.Flush()
            fsDecrypted.Close()
            fsread.Close()
        End Sub
        Public Function EncryptString(ByVal sString As String) As String
            'Initialize AES Crypto Provider
            Dim AES As New AesCryptoServiceProvider()
            'Set KeySize to 256 Bit
            AES.KeySize = 256
            'Set the initialization vector and the encryption key
            AES.IV = ASCIIEncoding.ASCII.GetBytes(Left(_key, 16))
            AES.Key = ASCIIEncoding.ASCII.GetBytes(_key)
            'Create the AES Encryptor
            Dim aesencrypt As ICryptoTransform = AES.CreateEncryptor
            'Convert plain text to bytes
            Dim plaintext() As Byte = System.Text.Encoding.Unicode.GetBytes(sString)
            'Create the Stream
            Dim ms As New System.IO.MemoryStream
            'Create the encoder
            Dim encStream As New CryptoStream(ms, aesencrypt, CryptoStreamMode.Write)
            encStream.Write(plaintext, 0, plaintext.Length)
            encStream.FlushFinalBlock()
            Return Convert.ToBase64String(ms.ToArray)
        End Function
        Public Function DecryptString(ByVal sString As String) As String
            'Initialize AES Crypto Provider
            Dim AES As New AesCryptoServiceProvider()
            'Set KeySize to 256 Bit
            AES.KeySize = 256
            'Set the initialization vector and the encryption key
            AES.IV = ASCIIEncoding.ASCII.GetBytes(Left(_key, 16))
            AES.Key = ASCIIEncoding.ASCII.GetBytes(_key)
            'Create the AES Decryptor
            Dim aesdecrypt As ICryptoTransform = AES.CreateDecryptor
            'Convert plain text to bytes
            Dim encryptedstring() As Byte = Convert.FromBase64String(sString)
            'Create the Stream
            Dim ms As New System.IO.MemoryStream
            'Create the decoder
            Dim decStream As New CryptoStream(ms, aesdecrypt, CryptoStreamMode.Write)
            decStream.Write(encryptedstring, 0, encryptedstring.Length)
            decStream.FlushFinalBlock()
            Return System.Text.Encoding.Unicode.GetString(ms.ToArray)
        End Function
    End Class
End Module

Schreiben Sie einen Kommentar

Ihre E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.