VB.NET
Private Shared Sub GenerateKeyFromPassword(ByVal password As String, _
ByVal keySize As Integer, _
ByRef key As Byte(), _
ByVal blockSize As Integer, _
ByRef iv As Byte())
Dim salt As Byte() = System.Text.Encoding.UTF8.GetBytes("saltは必ず8バイト以上")
Dim deriveBytes As New System.Security.Cryptography.Rfc2898DeriveBytes( _
password, salt)
deriveBytes.IterationCount = 1000
key = deriveBytes.GetBytes(keySize \ 8)
iv = deriveBytes.GetBytes(blockSize \ 8)
End Sub
Public Shared Sub EncryptFile(ByVal sourceFile As String, _
ByVal destFile As String, _
ByVal password As String)
Dim rijndael As New System.Security.Cryptography.RijndaelManaged()
Dim key As Byte(), iv As Byte()
GenerateKeyFromPassword(password, rijndael.KeySize, key, rijndael.BlockSize, iv)
rijndael.Key = key
rijndael.IV = 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 password As String)
Dim rijndael As New System.Security.Cryptography.RijndaelManaged()
Dim key As Byte(), iv As Byte()
GenerateKeyFromPassword(password, rijndael.KeySize, key, rijndael.BlockSize, iv)
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
C#private static void GenerateKeyFromPassword(string password,
int keySize, out byte[] key, int blockSize, out byte[] iv)
{
byte[] salt = System.Text.Encoding.UTF8.GetBytes("saltは必ず8バイト以上");
System.Security.Cryptography.Rfc2898DeriveBytes deriveBytes =
new System.Security.Cryptography.Rfc2898DeriveBytes(password, salt);
deriveBytes.IterationCount = 1000;
key = deriveBytes.GetBytes(keySize / 8);
iv = deriveBytes.GetBytes(blockSize / 8);
}
public static void EncryptFile(
string sourceFile, string destFile, string password)
{
System.Security.Cryptography.RijndaelManaged rijndael =
new System.Security.Cryptography.RijndaelManaged();
byte[] key, iv;
GenerateKeyFromPassword(
password, rijndael.KeySize, out key, rijndael.BlockSize, out iv);
rijndael.Key = key;
rijndael.IV = 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, string password)
{
System.Security.Cryptography.RijndaelManaged rijndael =
new System.Security.Cryptography.RijndaelManaged();
byte[] key, iv;
GenerateKeyFromPassword(
password, rijndael.KeySize, out key, rijndael.BlockSize, out iv);
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();
}