Dim fsInput As New FileStream(sInputFilename, _
FileMode.Open, FileAccess.Read)
Dim fsEncrypted As New FileStream(sOutputFilename, _
FileMode.Create, FileAccess.Write)
Sub DecryptFile(ByVal sInputFilename As String, _
ByVal sOutputFilename As String, _
ByVal sKey As String)
Dim DES As New DESCryptoServiceProvider()
'このプロバイダには 64 ビット キーと IV が必要です。
'DES アルゴリズム用の秘密キーを設定します。
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
'初期化ベクタを設定します。
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'暗号化されたファイルを読み込むためのファイル ストリームを作成します。
Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
'DES のインスタンスから DES Decryptor を作成します。
Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
'受けとったバイトに対して読み取りと DES 復号化変換を行うように設定された CryptoStream を作成します。
Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
'復号化されたファイルの内容を出力します。
Dim fsDecrypted As New StreamWriter(sOutputFilename)
fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
fsDecrypted.Flush()
fsDecrypted.Close()
End Sub
Main () プロシージャに、EncryptFile および DecryptFile の両方を呼び出すコードを追加します。
Public Sub Main()
EncryptFile("c:\temp\test.txt", _
"c:\temp\Encrypted.txt", _
sSecretKey)
DecryptFile("c:\temp\Encrypted.txt", _
"c:\temp\Decrypted.txt", _
sSecretKey)
End Sub
このコードをテキスト (.txt) ファイルでテストし、ファイルが正しく暗号化され、復号化されることを確認します。復号化先は、元のファイルではなく新しいファイルを指定するようにします (この資料の Sub Main() プロシージャを参照のこと)。復号化後のファイルを調べ、元のファイルと比較します。
完全なコード リスト
Imports System
Imports System.IO
Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Module Module1
'64 ビット、8 バイトでなければなりません。
Private Const sSecretKey As String = "Password"
Public Sub Main()
EncryptFile("c:\temp\test.txt", _
"c:\temp\Encrypted.txt", _
sSecretKey)
DecryptFile("c:\temp\Encrypted.txt", _
"c:\temp\Decrypted.txt", _
sSecretKey)
End Sub
Sub EncryptFile(ByVal sInputFilename As String, _
ByVal sOutputFilename As String, _
ByVal sKey As String)
Dim fsInput As New FileStream(sInputFilename, _
FileMode.Open, FileAccess.Read)
Dim fsEncrypted As New FileStream(sOutputFilename, _
FileMode.Create, FileAccess.Write)
Dim DES As New DESCryptoServiceProvider()
'DES アルゴリズム用の秘密キーを設定します。
'このプロバイダには 64 ビット キーと IV が必要です。
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
'初期化ベクタを設定します。
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'このインスタンスから DES Encryptor を作成します。
Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()
'DES 暗号化を使用してファイル ストリームを変換する CryptoStream を作成します。
Dim cryptostream As New CryptoStream(fsEncrypted, _
desencrypt, _
CryptoStreamMode.Write)
'ファイルのテキストをバイト配列に読み込みます。
Dim bytearrayinput(fsInput.Length - 1) As Byte
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
'DES で暗号化されたファイルを書き出します。
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
cryptostream.Close()
fsEncrypted.Close()
fsInput.Close()
End Sub
Sub DecryptFile(ByVal sInputFilename As String, _
ByVal sOutputFilename As String, _
ByVal sKey As String)
Dim DES As New DESCryptoServiceProvider()
'このプロバイダには 64 ビット キーと IV が必要です。
'DES アルゴリズム用の秘密キーを設定します。
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
'初期化ベクタを設定します。
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'暗号化されたファイルを読み込むためのファイル ストリームを作成します。
Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
'DES のインスタンスから DES Decryptor を作成します。
Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
'受け取ったバイトに対して読み取りと DES 復号化変換を行うように設定された CryptoStream を作成します。
Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
'復号化されたファイルの内容を出力します。
Dim fsDecrypted As New StreamWriter(sOutputFilename)
fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
fsDecrypted.Flush()
fsDecrypted.Close()
fsread.Close()
End Sub
End Module