Found here!
The Requirements for .NET Cryptography
To tap into the .NET security features, you need Imports statements and encryption packs. First, to experiment with any of the code in this article, be sure that you add the following Imports statements at the top of your Visual Basic code window:
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Second, note that the U.S. government restricted encryption in the past to prevent certain encryption technology from being exported. Although the restrictions are no longer in effect, the .NET framework prohibits “strong” or “high” encryption in export versions of the Windows OS. If you don’t already have strong encryption capabilities in your version of Windows, you can download an update on the Microsoft Web site. Install the High Encryption Pack included in Service Pack 2 for Windows 2000, or Service Pack 6a for NT. Internet Explorer 5.5 also includes the High Encryption Pack for users of Windows ME, 95, and 98.
Here’s the code for the encryption/decryption utility. It surrounds the encryption, decryption, and key-generating procedures with some user interface conveniences. It provides a TextBox into which the user types a filename and another TextBox to type in the key.
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography
Public Class Form1
Inherits System.Windows.Forms.Form
'create an 8-byte long array to hold the key
Public TheKey(7) As Byte
'Stuff some random values into the vector:
Private Vector() As Byte = {&H12, &H44, &H16, &HEE, &H88, &H15, &HDD, &H41}
'Windows Form Designer generated code
'/////ENCRYPTION PROCEDURE
Sub Encrypt( ByVal inName As String , ByVal outName As String )
Try
Dim storage(4096) As Byte 'create buffer
Dim totalBytesWritten As Long = 8 'Keeps track of bytes written.
Dim packageSize As Integer 'Specifies the number of bytes written at one time.
'Declare the file streams.
Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
Dim fout As New FileStream(outName, FileMode.OpenOrCreate, _
FileAccess.Write)
fout.SetLength(0)
Dim totalFileLength As Long = fin.Length 'Specifies the size of the source file.
'create the Crypto object
Dim des As New DESCryptoServiceProvider()
Dim crStream As New CryptoStream(fout, _
des.CreateEncryptor(TheKey, Vector), CryptoStreamMode.Write)
'flow the streams
While totalBytesWritten < totalFileLength
packageSize = fin.Read(storage, 0, 4096)
crStream.Write(storage, 0, packageSize)
totalBytesWritten = Convert.ToInt32(totalBytesWritten + packageSize / des.BlockSize * des.BlockSize)
End While
crStream.Close()
Catch e As Exception
MsgBox(e.Message)
End Try
End Sub
'//////// DECRYPTION PROCEDURE
' This procedure differs from the encryption procedure only in the substitution of
'des.CreateDecryptor for des.CreateEncryptor. Also, the error message is different.
Sub Decrypt( ByVal inName As String , ByVal outName As String )
Try
Dim storage(4096) As Byte
Dim totalBytesWritten As Long = 8
Dim packageSize As Integer
Dim fin As New FileStream(inName, FileMode.Open, FileAccess.Read)
Dim fout As New FileStream(outName, FileMode.OpenOrCreate, _
FileAccess.Write)
fout.SetLength(0)
Dim totalFileLength As Long = fin.Length
Dim des As New DESCryptoServiceProvider()
Dim crStream As New CryptoStream(fout, _
des.CreateDecryptor(TheKey, Vector), CryptoStreamMode.Write)
Dim ex As Exception
While totalBytesWritten < totalFileLength
packageSize = fin.Read(storage, 0, 4096)
crStream.Write(storage, 0, packageSize)
totalBytesWritten = Convert.ToInt32(totalBytesWritten + packageSize / des.BlockSize * des.BlockSize)
Console.WriteLine("Processed {0} bytes, {1} bytes total", packageSize, _
totalBytesWritten)
End While
crStream.Close()
Catch e As Exception
MsgBox(e.Message & "Please ensure that you are using the correct password")
End Try
End Sub
'BROWSE BUTTON
Private Sub Button1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.ShowDialog()
TextBox1.Text = OpenFileDialog1.FileName
End Sub
'ENCRYPT/DECRYPT BUTTON
Private Sub Button2_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim targetfile, sourcefile As String
'check to see if they've entered a filename at all
If TextBox1.Text = "" Or TextBox2.Text = "" Then MsgBox("You must enter a filename and a password.") : Exit Sub
'check to see if it is a decryption (the filename ends in "xx")
Dim ext As String 'file extension
Dim mainpath As String 'the file path minus the extension
Dim n As Integer 'location of period in filepath
n = TextBox1.Text.IndexOf(".") ' returns -1 if there is no extension
If n <> -1 Then 'extract the extension
ext = TextBox1.Text.Substring(n + 1)
mainpath = TextBox1.Text.Substring(0, TextBox1.Text.Length - ext.Length - 1)
Else
mainpath = TextBox1.Text
End If
'check for "xx" at the end of the filename, indicating an already encrypted file:
If mainpath.Substring(mainpath.Length - 2) = "xx" Then 'this file will be decrypted
'DECRYPT:
sourcefile = TextBox1.Text
'compose filepath by removing the "xx":
mainpath = mainpath.Substring(0, mainpath.Length - 2)
If ext <> "" Then mainpath &= "." & ext
targetfile = mainpath
CreateKey(TextBox2.Text) 'create the key
Decrypt(sourcefile, targetfile)
Label3.Text = "Finished decryption..."
Exit Sub
End If
'ENCRYPT
'there was no "xx" appended, so they must be encrypting a file
sourcefile = TextBox1.Text
'compose the encrypted file's filepath by appending "xx":
mainpath &= "xx"
If ext <> "" Then mainpath &= "." & ext
targetfile = mainpath
CreateKey(TextBox2.Text) 'create the key
Encrypt(sourcefile, targetfile)
Label3.Text = "Finished encryption..."
End Sub
'////// Procedure that hashes the password and creates a key
Sub CreateKey( ByVal strKey As String )
' Byte array to hold key
Dim arrByte(7) As Byte
Dim AscEncod As New ASCIIEncoding()
Dim i As Integer = 0
AscEncod.GetBytes(strKey, i, strKey.Length, arrByte, i)
'Get the hash value of the password
Dim hashSha As New SHA1CryptoServiceProvider()
Dim arrHash() As Byte = hashSha.ComputeHash(arrByte)
'put the hash value into the key
For i = 0 To 7
TheKey(i) = arrHash(i)
Next i
End Sub
End Class