001 002 003 004 005 006 007 008 009 010
011 012 013 014 015 016 017 018 019 020
021 022 023 024 025 026 027 028 029 030
031 032 033 034 035 036 037 038 039 040
041 042 043 044 045 046 047 048 049 050
051 052 053 054 055 056 057 058 059 060
061 062 063 064 065 066 067 068 069 070
071 072 073 074 075 076 077 078 079 080
081 082 083 084 085 086 087 088 089 090
091 092 093 094 095 096 097 098 099 100
101 102 103 104 105 106 107 108 109 110
111 112
|
Option Strict On
Imports System
Imports System.IO
Imports System.Text
Imports System.Security
Imports System.Security.Cryptography
Module Module1
Sub Main()
' 鍵は任意のビット数で可
Dim key() As Byte = Encoding.ASCII.GetBytes("longlongpassword")
' 初期化ベクタは8バイト
Dim iv() As Byte = Encoding.ASCII.GetBytes("password")
' 暗号化して保存
EncryptByRC2("E:\Text.txt", "E:\Encrypted.txt", key, iv)
' 復号化して保存
DecryptByRC2("E:\Encrypted.txt", "E:\Decrypted.txt", key, iv)
End Sub
' RC2アルゴリズムによって暗号化する
Sub EncryptByRC2(ByVal inputFileName As String, ByVal encryptedFileName As String, ByVal encryptionKey() As Byte, ByVal iv() As Byte)
Dim inputStream As FileStream
Dim encryptedStream As FileStream
' 暗号化する対象のFileStreamオブジェクト
inputStream = New FileStream(inputFileName, FileMode.Open, FileAccess.Read)
' 暗号化したものを書き込むためのFileStreamオブジェクト
encryptedStream = New FileStream(encryptedFileName, FileMode.Create, FileAccess.Write)
' DESと呼ばれる方法で暗号化するためのオブジェクト
Dim rc2 As New RC2CryptoServiceProvider()
' 鍵はビット数で可
rc2.Key = encryptionKey
' 初期化ベクタ(暗号化と復号化の際には同じIVを必要とする)
rc2.IV = iv
' CryptoStreamオブジェクトを作成する
Dim transform As ICryptoTransform = rc2.CreateEncryptor() ' Encryptorを作成する
Dim cryptoStream As New CryptoStream(encryptedStream, transform, CryptoStreamMode.Write)
' 暗号化する対象をバイト配列として読み込む
Dim buffer() As Byte
Dim length As Integer = CInt(inputStream.Length)
ReDim buffer(length)
inputStream.Read(buffer, 0, length)
' CryptoStreamによって暗号化して書き込む
cryptoStream.Write(buffer, 0, length)
' CryptoStreamを閉じる
cryptoStream.Close()
' FileStreamを閉じる
inputStream.Close()
encryptedStream.Close()
End Sub
' RC2アルゴリズムで暗号化されたものを復号化する
Sub DecryptByRC2(ByVal inputFileName As String, ByVal decryptedFileName As String, ByVal decryptionKey() As Byte, ByVal iv() As Byte)
Dim inputStream As FileStream
Dim decryptedStream As StreamWriter
' 復号化する対象のFileStreamオブジェクト
inputStream = New FileStream(inputFileName, FileMode.Open, FileAccess.Read)
' 復号化したものを書き込むためのStreamWriterオブジェクト
decryptedStream = New StreamWriter(decryptedFileName, False, Encoding.UTF8)
' DESと呼ばれる方法で暗号化するためのオブジェクト
Dim rc2 As New RC2CryptoServiceProvider()
' 鍵はビット数で可
rc2.Key = decryptionKey
' 初期化ベクタ(暗号化と復号化の際には同じIVを必要とする)
rc2.IV = iv
' CryptoStreamオブジェクトを作成する
Dim transform As ICryptoTransform = rc2.CreateDecryptor() ' Decryptorを作成する
Dim cryptoStream As New CryptoStream(inputStream, transform, CryptoStreamMode.Read)
' CryptoStreamから読み込むためのStreamReaderを作成する
Dim reader As New StreamReader(cryptoStream)
' 復号化して書き込む
decryptedStream.Write(reader.ReadToEnd())
' ストリームを閉じる
reader.Close()
cryptoStream.Close()
decryptedStream.Close()
End Sub
End Module
|