Created
April 23, 2010 05:15
-
-
Save taka2/376212 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ' 参考サイト: http://msdn.microsoft.com/ja-jp/library/system.security.cryptography.tripledes(v=VS.71).aspx | |
| Imports System | |
| Imports System.IO | |
| Imports System.Security.Cryptography | |
| Imports System.Text | |
| Class VBNETTest | |
| Public Shared Sub Main() | |
| Dim keyBytesBase64 = System.Convert.FromBase64String("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") | |
| Dim i As Integer | |
| Dim keyBytes(23) As Byte | |
| For i=0 to 23 | |
| keyBytes(i) = keyBytesBase64(i) | |
| Next | |
| EnDecryptData("VBNETTest.vb", "crypted.txt", keyBytes, Nothing, True) | |
| EnDecryptData("crypted.txt", "plain.txt", keyBytes, Nothing, False) | |
| End Sub | |
| ' inNameのファイルをTripleDES(tdesKey, tdesIV, Mode=ECB)で暗号化してoutNameファイルに保存する | |
| Public Shared Sub EncryptData(inName As String, outName As String, tdesKey() As Byte, tdesIV() As Byte) | |
| EnDecryptData(inName, outName, tdesKey, tdesIV, True) | |
| End Sub | |
| ' inNameのファイルをTripleDES(tdesKey, tdesIV, Mode=ECB)で復号化してoutNameファイルに保存する | |
| Public Shared Sub DecryptData(inName As String, outName As String, tdesKey() As Byte, tdesIV() As Byte) | |
| EnDecryptData(inName, outName, tdesKey, tdesIV, False) | |
| End Sub | |
| Public Shared Sub EnDecryptData(inName As String, outName As String, tdesKey() As Byte, tdesIV() As Byte, isEncrypt As Boolean) | |
| 'Create the file streams to handle the input and output files. | |
| Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read) | |
| Dim fout As New FileStream(outName, FileMode.OpenOrCreate, _ | |
| FileAccess.Write) | |
| fout.SetLength(0) | |
| 'Create variables to help with read and write. | |
| Dim bin(100) As Byte 'This is intermediate storage for the encryption. | |
| Dim rdlen As Long = 0 'This is the total number of bytes written. | |
| Dim totlen As Long = fin.Length 'This is the total length of the input file. | |
| Dim len As Integer 'This is the number of bytes to be written at a time. | |
| Dim tdes As New TripleDESCryptoServiceProvider() | |
| tdes.Mode = CipherMode.ECB | |
| Dim encStream As CryptoStream | |
| If isEncrypt Then | |
| encStream = New CryptoStream(fout, tdes.CreateEncryptor(tdesKey, tdesIV), CryptoStreamMode.Write) | |
| Else | |
| encStream = New CryptoStream(fout, tdes.CreateDecryptor(tdesKey, tdesIV), CryptoStreamMode.Write) | |
| End If | |
| 'Read from the input file, then encrypt and write to the output file. | |
| While rdlen < totlen | |
| len = fin.Read(bin, 0, 100) | |
| encStream.Write(bin, 0, len) | |
| rdlen = rdlen + len | |
| End While | |
| encStream.Close() | |
| End Sub | |
| End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment