Mit dieser Funktion kann man eine Datei mit dem Rijndael Algorithmus entschlüsseln
Imports System.Security.Cryptography
Imports System.IO
Imports System.Text
Public Function DecryptFile(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(OldFileStream, RijndaelManaged.CreateDecryptor(sKey, sKey), CryptoStreamMode.Read)
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 = CryptoStream.Read(Data, 0, FileLen - BufferCount)
NewFileStream.Write(Data, 0, Buffer)
Exit Do
End If
Buffer = CryptoStream.Read(Data, 0, BufferSize)
NewFileStream.Write(Data, 0, BufferSize)
BufferCount += Buffer
Loop
NewFileStream.Close()
CryptoStream.Close()
OldFileStream.Close()
Catch
Return False
End Try
Return True
End Function
1 Kommentare zum Snippet