Encrypt a Text file Using DES in VB.NET: Part 1

In this article we will learn how to Encrypt a text file Using DES Method in VB.NET.
  • 12013
 

1.Open visual studio and create a project

2.Add the following assembly on the page

Imports System.Security
Imports
System.Security.Cryptography
Imports
System.Text
Imports
System.IO

3.Now write the Code in the Page

 Public Class Tester
    Public Shared Sub Main()
        Try

 
            Dim myDESProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider()
             myDESProvider.Key = ASCIIEncoding.ASCII.GetBytes("12345678")
            myDESProvider.IV = ASCIIEncoding.ASCII.GetBytes("12345678")
             Dim myICryptoTransform As ICryptoTransform = myDESProvider.CreateEncryptor(myDESProvider.Key, myDESProvider.IV)
             Dim ProcessFileStream As FileStream = New FileStream("Encrypted.txt", FileMode.Open, FileAccess.Read)
            Dim ResultFileStream As FileStream = New FileStream("Decrypted.txt", FileMode.Create, FileAccess.Write)
            Dim myCryptoStream As CryptoStream = New CryptoStream(ResultFileStream, myICryptoTransform, CryptoStreamMode.Write)
             Dim bytearrayinput(ProcessFileStream.Length - 1) As Byte

 
            ProcessFileStream.Read(bytearrayinput, 0, bytearrayinput.Length)
            myCryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length)
            myCryptoStream.Close()
            ProcessFileStream.Close()
            ResultFileStream.Close()
         Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        Console.ReadLine()
    End Sub

End
Class

4.Now convert the file.text file in the folder D:\New Folder (2)\ConsoleApplication4\ConsoleApplication4\bin\Debug (My text file path) to Encrypted text file.

Now start debugging, you will see a file.txt automatically converted into Encrypted. text file.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.