How to Encrypt a File With VB

How to Encrypt a File With VB thumbnail
Lock up your files with encryption.

The Visual Basic programming language, published by Microsoft, comes with a built-in library for handling encryption and cryptography. Though the encryption library takes care of the most difficult parts of encrypting a file, it is not obvious how to use the tools provided to encrypt a file. The most important beginning is, that to encrypt your file, you must first read it as an array of bytes, not text or other data types.

Instructions

    • 1

      Create a new project in Visual Basic by clicking "File" and "New Project." Select the "Console Application" option. Later, you can apply the same code used in this tutorial to any Graphical User Interface (GUI) application you decide to build.

    • 2

      Import the Cryptography library by pasting the following line of code at the top of your application:

      Imports System.Security.Cryptography

    • 3

      Read the data from the file you wish to encrypt as an array of bytes using the following command:

      Dim bytes() = My.Computer.FileSystem.ReadAllBytes("C:\file.dat")

      Replace "C:\file.dat" with the path and name of the file you would like to encrypt.

    • 4

      Paste the following command to declare the key and initialization vector for the encryption function. You will need this information in order to decrypt the file:

      Dim key() as Byte = {24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1 }

      Dim init_vector() as Byte = {8,7,6,5,4,3,2,1 }

      Naturally, you should replace all the numbers in both the key and the initialization vector with other numbers. Anyone who knows these numbers can decrypt your file, so make them hard to guess. The numbers should all be valid bytes: between 0 and 255.

    • 5

      Initialize the encryptor, an encryption stream and a memory stream to hold the encrypted data while you decide what to do with it. Paste the following code:

      dim tdescsp = New TripleDESCryptoServiceProvider()

      dim encryptor = tdescsp.CreateEncryptor(key, init_vector)

      dim memStream = new System.IO.MemoryStream

      dim encryptionStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write)

    • 6

      Encrypt the byte array from above by pasting the following:

      encryptionStream.Write(bytes, 0, bytes.Length)

      encryptionStream.FlushFinalBlock()

      memStream.Position = 0

    • 7

      Read the encrypted data back into your memStream:

      Dim encrypted_bytes(memStream.Length - 1) as Byte

      memStream.Read(encrypted_bytes, 0, memStream.Length)

      memStream.Close()

    • 8

      Paste the following to print the encrypted data to the Console:

      for each x in encrypted_bytes

      Console.Write(x)

      Console.Write(" ")

      next

      Console.ReadKey()

    • 9

      Press the green arrow to run the application.

Related Searches:

References

  • Photo Credit padlock and chain image by Nicemonkey from Fotolia.com

Comments

You May Also Like

Related Ads

Featured