Dieser Code verschlüsselt und entschlüsselt eine Datei. (Schlüssel und IV werden etwas anders gemacht als bei deinem alten Code).
Imports System.IO
Imports System.Security.Cryptography
Public Class Form1
Public Sub encryptFile(inPath As String, outPath As String, skey As String, _
IV As String)
Dim bIV() As Byte = System.Text.Encoding.UTF8.GetBytes(IV)
Dim hiv() As Byte
Dim hkey() As Byte
Using db1 As New Rfc2898DeriveBytes(IV, bIV),
db2 As New Rfc2898DeriveBytes(skey, bIV)
hiv = db1.GetBytes(16)
hkey = db2.GetBytes(16)
End Using
Using rijndael As New RijndaelManaged(),
inStream As New FileStream(inPath, FileMode.Open),
outStream As New FileStream(outPath, FileMode.Create),
enc As ICryptoTransform = rijndael.CreateEncryptor(hkey, hIV),
cs As New CryptoStream(inStream, enc, CryptoStreamMode.Read)
Dim inBuff(1024) As Byte
Dim inBytes As Integer
Do
inBytes = cs.Read(inBuff, 0, inBuff.Length)
outStream.Write(inBuff, 0, inBytes)
Loop While inBytes > 0
End Using
End Sub
Public Sub decryptFile(inPath As String, outPath As String, skey As String, _
IV As String)
Dim bIV() As Byte = System.Text.Encoding.UTF8.GetBytes(IV)
Dim hiv() As Byte
Dim hkey() As Byte
Using db1 As New Rfc2898DeriveBytes(IV, bIV),
db2 As New Rfc2898DeriveBytes(skey, bIV)
hiv = db1.GetBytes(16)
hkey = db2.GetBytes(16)
End Using
Using rijndael As New RijndaelManaged(),
inStream As New FileStream(inPath, FileMode.Open),
outStream As New FileStream(outPath, FileMode.Create),
dec As ICryptoTransform = rijndael.CreateDecryptor(hkey, hIV),
cs As New CryptoStream(inStream, dec, CryptoStreamMode.Read)
Dim inBuff(1024) As Byte
Dim inBytes As Integer
Do
inBytes = cs.Read(inBuff, 0, inBuff.Length)
outStream.Write(inBuff, 0, inBytes)
Loop While inBytes > 0
End Using
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
Dim pw As String = "password1"
Dim iv As String = "MeinSenfDazu"
Dim path1 As String = "C:\Test\aesmsgi.txt"
Dim path2 As String = "C:\Test\aesmsg.enc"
Dim path3 As String = "C:\Test\aesmsgo.txt"
encryptFile(path1, path2, pw, iv)
decryptFile(path2, path3, pw, iv)
Debug.WriteLine("Original: " & File.ReadAllText(path1))
Debug.WriteLine("Decrypted: " & File.ReadAllText(path3))
End Sub
End Class ________
Alle Angaben ohne Gewähr. Keine Haftung für Vorschläge, Tipps oder sonstige Hilfe, falls es schiefgeht, nur Zeit verschwendet oder man sonst nicht zufrieden ist |