Public Function DecryptFile(ByVal sourceFile As String, _ ByVal destFile As String, _ ByVal key As Byte(), _ ByVal iv As Byte()) As String 'RijndaelManagedオブジェクトの作成 Dim rijndael As New System.Security.Cryptography.RijndaelManaged()
'共有キーと初期化ベクタを設定 rijndael.Key = key rijndael.IV = iv
'暗号化されたファイルを読み込むためのFileStream 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() '暗号化されたデータを読み込むためのCryptoStreamの作成 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 '復号化に失敗すると例外CryptographicExceptionが発生 readLen = cryptStrm.Read(bs, 0, bs.Length) If readLen = 0 Then Exit While End If outFs.Write(bs, 0, readLen) End While
---追加した部分-------------------------- Dim DecryptStr As String 'Shift JISとして文字列に変換 DecryptStr = System.Text.Encoding.GetEncoding(932).GetString(bs) Console.WriteLine(DecryptStr) -----------------------------------------