What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleEncrypt and decrypt a file using a DES algorithm in VB .NET
DescriptionThis example shows how to encrypt and decrypt a file using a DES algorithm in VB .NET.
KeywordsNetBIOS, name, NetBIOS name, computer name, host name
CategoriesAlgorithms, Files and Directories, Software Engineering
Subroutine CryptFile encrypts or decrypts a file, saving the results in a new file. It starts by creating file streams for the new and old files.

Next the program makes a triple DES cryptographic service provider. You could use other providers here. The program loops through key sizes in bits, starting from 1024 and working down, until it finds a key size supported by the provider. It then gets the provider's block size.

Next the program calls subroutine MakeKeyAndIV to build a key and an initialization vector (IV) out of the password provided to the subroutine. The program uses the key and IV to create a transform object that either encrypts or decrypts.

The code creates a CryptoStream object attached to the output stream using the transform. It then reads data from the input stream and writes it into the crypto stream, which automatically encrypts it and sends the result to the output stream.

' Encrypt or decrypt a file, saving the results in another
' file.
Private Sub CryptFile(ByVal password As String, ByVal _
    in_file As String, ByVal out_file As String, ByVal _
    encrypt As Boolean)
    ' Create input and output file streams.
    Dim in_stream As New FileStream(in_file, FileMode.Open, _
    Dim out_stream As New FileStream(out_file, _
        FileMode.Create, FileAccess.Write)

    ' Make a triple DES service provider.
    Dim des_provider As New TripleDESCryptoServiceProvider

    ' Find a valid key size for this provider.
    Dim key_size_bits As Integer = 0
    For i As Integer = 1024 To 1 Step -1
        If des_provider.ValidKeySize(i) Then
            key_size_bits = i
            Exit For
        End If
    Next i
    Debug.Assert(key_size_bits > 0)

    ' Get the block size for this provider.
    Dim block_size_bits As Integer = des_provider.BlockSize

    ' Generate the key and initialization vector.
    Dim key As Byte() = Nothing
    Dim iv As Byte() = Nothing
    MakeKeyAndIV(password, key_size_bits, block_size_bits, _
        key, iv)

    ' Make the encryptor or decryptor.
    Dim crypto_transform As ICryptoTransform
    If encrypt Then
        crypto_transform = _
            des_provider.CreateEncryptor(key, iv)
        crypto_transform = _
            des_provider.CreateDecryptor(key, iv)
    End If

    ' Attach a crypto stream to the output stream.
    Dim crypto_stream As New CryptoStream(out_stream, _
        crypto_transform, CryptoStreamMode.Write)

    ' Encrypt or decrypt the file.
    Const BLOCK_SIZE As Integer = 1024
    Dim buffer(BLOCK_SIZE) As Byte
    Dim bytes_read As Integer
        ' Read some bytes.
        bytes_read = in_stream.Read(buffer, 0, BLOCK_SIZE)
        If bytes_read = 0 Then Exit Do

        ' Write the bytes into the CryptoStream.
        crypto_stream.Write(buffer, 0, bytes_read)

    ' Close the streams.
End Sub
Subroutine MakeKeyAndIV makes a new PasswordDeriveBytes object based on the password using the SHA384 hashing algorithm. It uses the object's GetBytes method to initialize the key and IV to bytes from this object.
' Use the password to generate key bytes.
Private Sub MakeKeyAndIV(ByVal password As String, ByVal _
    key_size_bits As Integer, ByVal block_size_bits As _
    Integer, ByRef key As Byte(), ByRef iv As Byte())
    Dim password_derive_bytes As New PasswordDeriveBytes( _
        txtPassword.Text, Nothing, "SHA384", 1000)

    key = password_derive_bytes.GetBytes(key_size_bits \ 8)
    iv = password_derive_bytes.GetBytes(block_size_bits \ 8)
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.