Feedback

VB - Dateiverschlüsselung mit Rijndael

Veröffentlicht von am 6/3/2006
(5 Bewertungen)
Mit dieser Funktion kann man eine Datei mit dem Rijndael Algorithmus verschlüsseln
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Public Function EncryptFile(ByVal sSource As String, ByVal sTarget As String, ByVal sPassword As String, Optional ByVal BufferSize As Long = 4096) As Boolean
    Try
        Dim sKey As Byte() = (New UnicodeEncoding).GetBytes(sPassword.PadRight(8, "*"))
        Dim OldFileStream As New FileStream(sSource, FileMode.Open)
        Dim NewFileStream As New FileStream(sTarget, FileMode.Create)
        Dim RijndaelManaged As New RijndaelManaged
        Dim CryptoStream As New CryptoStream(NewFileStream, RijndaelManaged.CreateEncryptor(sKey, sKey), CryptoStreamMode.Write)

        Dim Data(BufferSize) As Byte
        Dim Buffer As Long
        Dim BufferCount As Long
        Dim FileLen As Long = OldFileStream.Length

        Do
            If BufferCount >= FileLen - BufferSize Then
                ReDim Data(FileLen - BufferCount)
                Buffer = OldFileStream.Read(Data, 0, FileLen - BufferCount)
                CryptoStream.Write(Data, 0, Buffer)
                Exit Do
            End If

            Buffer = OldFileStream.Read(Data, 0, BufferSize)
            CryptoStream.Write(Data, 0, BufferSize)
            BufferCount += Buffer
        Loop

        OldFileStream.Close()
        CryptoStream.Close()
        NewFileStream.Close()
    Catch
        Return False
    End Try
    Return True
End Function
Abgelegt unter Verschlüsselung, Rijndael, AES.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!