VB.NET
Public Shared Sub EncryptFile(ByVal sourceFile As String, _
ByVal destFile As String, _
ByRef key As Byte(), _
ByRef iv As Byte())
Dim rijndael As New System.Security.Cryptography.RijndaelManaged()
key = rijndael.Key
iv = rijndael.IV
Dim outFs As New System.IO.FileStream( _
destFile, System.IO.FileMode.Create, System.IO.FileAccess.Write)
Dim encryptor As System.Security.Cryptography.ICryptoTransform = _
rijndael.CreateEncryptor()
Dim cryptStrm As New System.Security.Cryptography.CryptoStream( _
outFs, encryptor, System.Security.Cryptography.CryptoStreamMode.Write)
Dim inFs As New System.IO.FileStream( _
sourceFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)
Dim bs As Byte() = New Byte(1023) {}
Dim readLen As Integer
While True
readLen = inFs.Read(bs, 0, bs.Length)
If readLen = 0 Then
Exit While
End If
cryptStrm.Write(bs, 0, readLen)
End While
inFs.Close()
cryptStrm.Close()
encryptor.Dispose()
outFs.Close()
End Sub
Public Shared Sub DecryptFile(ByVal sourceFile As String, _
ByVal destFile As String, _
ByVal key As Byte(), _
ByVal iv As Byte())
Dim rijndael As New System.Security.Cryptography.RijndaelManaged()
rijndael.Key = key
rijndael.IV = iv
Dim inFs As New System.IO.FileStream( _
sourceFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)
Dim decryptor As System.Security.Cryptography.ICryptoTransform = _
rijndael.CreateDecryptor()
Dim cryptStrm As New System.Security.Cryptography.CryptoStream( _
inFs, decryptor, System.Security.Cryptography.CryptoStreamMode.Read)
Dim outFs As New System.IO.FileStream( _
destFile, System.IO.FileMode.Create, System.IO.FileAccess.Write)
Dim bs As Byte() = New Byte(1023) {}
Dim readLen As Integer
While True
readLen = cryptStrm.Read(bs, 0, bs.Length)
If readLen = 0 Then
Exit While
End If
outFs.Write(bs, 0, readLen)
End While
outFs.Close()
cryptStrm.Close()
decryptor.Dispose()
inFs.Close()
End Sub
Dim key As Byte(), iv As Byte()
EncryptFile("C:\test.txt", "C:\test.enc", key, iv)
DecryptFile("C:\test.enc", "C:\test2.txt", key, iv)
C#public static void EncryptFile(
string sourceFile, string destFile, out byte[] key, out byte[] iv)
{
System.Security.Cryptography.RijndaelManaged rijndael =
new System.Security.Cryptography.RijndaelManaged();
key = rijndael.Key;
iv = rijndael.IV;
System.IO.FileStream outFs = new System.IO.FileStream(
destFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
System.Security.Cryptography.ICryptoTransform encryptor =
rijndael.CreateEncryptor();
System.Security.Cryptography.CryptoStream cryptStrm =
new System.Security.Cryptography.CryptoStream(
outFs, encryptor,
System.Security.Cryptography.CryptoStreamMode.Write);
System.IO.FileStream inFs = new System.IO.FileStream(
sourceFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] bs = new byte[1024];
int readLen;
while ((readLen = inFs.Read(bs, 0, bs.Length)) > 0)
{
cryptStrm.Write(bs, 0, readLen);
}
inFs.Close();
cryptStrm.Close();
encryptor.Dispose();
outFs.Close();
}
public static void DecryptFile(
string sourceFile, string destFile, byte[] key, byte[] iv)
{
System.Security.Cryptography.RijndaelManaged rijndael =
new System.Security.Cryptography.RijndaelManaged();
rijndael.Key = key;
rijndael.IV = iv;
System.IO.FileStream inFs = new System.IO.FileStream(
sourceFile, System.IO.FileMode.Open, System.IO.FileAccess.Read);
System.Security.Cryptography.ICryptoTransform decryptor =
rijndael.CreateDecryptor();
System.Security.Cryptography.CryptoStream cryptStrm =
new System.Security.Cryptography.CryptoStream(
inFs, decryptor,
System.Security.Cryptography.CryptoStreamMode.Read);
System.IO.FileStream outFs = new System.IO.FileStream(
destFile, System.IO.FileMode.Create, System.IO.FileAccess.Write);
byte[] bs = new byte[1024];
int readLen;
while ((readLen = cryptStrm.Read(bs, 0, bs.Length)) > 0)
{
outFs.Write(bs, 0, readLen);
}
outFs.Close();
cryptStrm.Close();
decryptor.Dispose();
inFs.Close();
}
byte[] key, iv;
EncryptFile(@"C:\test.txt", @"C:\test.enc", out key, out iv);
DecryptFile(@"C:\test.enc", @"C:\test2.txt", key, iv);