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()
'A 64 Bit Key and IV is required for this provider.
'Set secret key For DES algorithm.
DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
'Set initialization vector.
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
'create file stream to read encrypted file back
Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
'create DES Decryptor from our des instance
Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
'create crypto stream set to read and do a des decryption transform on incoming bytes
Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
'print out the contents of the decrypted file
Dim fsDecrypted As New StreamWriter(sOutputFilename)
fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
fsDecrypted.Flush()
fsDecrypted.Close()
End Sub
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
Imports System Imports System.IO Imports System.Security Imports System.Security.Cryptography Imports System.Text Module Module1 'Must be 64 bits, 8 bytes. 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() 'Set secret key For DES algorithm. 'A 64 Bit Key and IV is required for this provider DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey) 'Set initialization vector. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey) 'create DES Encryptor from this instance Dim desencrypt As ICryptoTransform = DES.CreateEncryptor() 'Create Crypto Stream that transforms file stream using des encryption Dim cryptostream As New CryptoStream(fsEncrypted, _ desencrypt, _ CryptoStreamMode.Write) 'Read the file text into our byte array Dim bytearrayinput(fsInput.Length - 1) As Byte fsInput.Read(bytearrayinput, 0, bytearrayinput.Length) 'write out DES encrypted file cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length) cryptostream.Close() End Sub Sub DecryptFile(ByVal sInputFilename As String, _ ByVal sOutputFilename As String, _ ByVal sKey As String) Dim DES As New DESCryptoServiceProvider() 'A 64 Bit Key and IV is required for this provider. 'Set secret key For DES algorithm. DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey) 'Set initialization vector. DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey) 'create file stream to read encrypted file back Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read) 'create DES Decryptor from our des instance Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor() 'create crypto stream set to read and do a des decryption transform on incoming bytes Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read) 'print out the contents of the decrypted file Dim fsDecrypted As New StreamWriter(sOutputFilename) fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd) fsDecrypted.Flush() fsDecrypted.Close() End Sub End Module