Data Encryption in VB.NET using DES

DES is a block cipher that uses shared secret encryption, there are 72,000,000,000,000,000 (72 quadrillion) or more possible encryption keys that can be used in VB.NET.
  • 5628

DES is a block cipher that uses shared secret encryption. It is a widely-used method of data encryption using a private (secret) key that was judged so difficult to break by the U.S. government that it was restricted for exportation to other countries. There are 72,000,000,000,000,000 (72 quadrillion) or more possible encryption keys that can be used. It was selected by the National Bureau of Standards as an official Federal Information Processing Standard (FIPS) for the United States in 1976 and which has subsequently enjoyed widespread use internationally.

There are lots of sophisticated and virtually unbreakable encryption tools out there such as Microsoft's Encrypting File System (EFS) to third-party solutons like AxCrypt, PGP, and Encrypt Easy. You can encrypt emails and Zip files automatically if you want to. But the most common used technique is DES it applies a 56-bit key to each 64-bit block of data. The process can run in several modes and involves 16 rounds or operations. Although this is considered "strong" encryption, many companies use "triple DES", which applies three keys in succession.

encr.gif

Lets take an example which clear exactly how we can use it, and how to encrypt the text file:

Example:

Imports System.Security
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO
 
Public Class Tester
    Public Shared Sub Main()
        Try

            Dim encryptor As DESCryptoServiceProvider = New DESCryptoServiceProvider()
 
            encryptor.Key = ASCIIEncoding.ASCII.GetBytes("12345678")

            encryptor.IV = ASCIIEncoding.ASCII.GetBytes("12345678")

            Dim encryptation As ICryptoTransform = encryptor.CreateEncryptor(encryptor.Key, encryptor.IV)
 
            Dim Sourcefile As FileStream = New FileStream("TextFile.txt"FileMode.Open, FileAccess.Read)
            Dim Outputfile As FileStream = New FileStream("OutputTextFile.txt"FileMode.Create, FileAccess.Write)
            Dim encrprocess As CryptoStream = New CryptoStream(Outputfile, encryptation, CryptoStreamMode.Write)
 
            Dim inputarray(Sourcefile.Length - 1) As Byte
 
            Sourcefile.Read(inputarray, 0, inputarray.Length)
            encrprocess.Write(inputarray, 0, inputarray.Length)
 
            encrprocess.Close()
            Sourcefile.Close()
            Outputfile.Close()
 
        Catch a As Exception
            Console.WriteLine(a.Message)
 
        End Try
        Console.ReadLine() 
    End Sub
End Class

Happy Learning

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.