ARTICLE

Create and use an RC2 object to encrypt and decrypt data in a file in VB.Net

Posted by Brijesh Jalan Articles | Cryptography in VB.NET August 16, 2010
In this tutorial we will learn how to use RC2 object to encrypt and decrypt data in a file.
Reader Level:

This is the method by which we can encrypt and decrypt the data in the file. It is an important part of Security used in VB.Net application.

1.Open visual studio and create a project

2.Add the following assembly on the page

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

3.Now write the following code for Encryption and Decryption

Module
RC2Sample
    Sub Main()
        Try
            Dim RC2alg As RC2 = RC2.Create("RC2")
            Dim sData As String = "My name is yahoo"
            Dim FileName As String = "CText.txt"

 
            EncryptTextToFile(sData, FileName, RC2alg.Key, RC2alg.IV)
            Dim Final As String = DecryptTextFromFile(FileName, RC2alg.Key, RC2alg.IV)
            Console.WriteLine(Final)
        Catch e As Exception
            Console.WriteLine(e.Message)
        End Try
        Console.ReadLine()
    End Sub
    Sub EncryptTextToFile(ByVal Data As String, ByVal FileName As String, ByVal Key() As Byte, ByVal IV() As Byte)
        Try
            Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate)
            Dim RC2alg As RC2 = RC2.Create
            Dim cStream As New CryptoStream(fStream, RC2alg.CreateEncryptor(Key, IV), CryptoStreamMode.Write)
            Dim sWriter As New StreamWriter(cStream)
            sWriter.WriteLine(Data)
            sWriter.Close()
            cStream.Close()
            fStream.Close()
        Catch e As CryptographicException
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
        Catch e As UnauthorizedAccessException
            Console.WriteLine("A file error occurred: {0}", e.Message)
        End Try
        Console.ReadLine()
    End Sub
    Function DecryptTextFromFile(ByVal FileName As String, ByVal Key() As Byte, ByVal IV() As Byte) As String
        Try
            Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate)
            Dim RC2alg As RC2 = RC2.Create
            Dim cStream As New CryptoStream(fStream, RC2alg.CreateDecryptor(Key, IV), CryptoStreamMode.Read)
            Dim sReader As New StreamReader(cStream)
            Dim val As String = sReader.ReadLine()
            sReader.Close()
            cStream.Close()
            fStream.Close()
             Return val
        Catch e As CryptographicException
            Console.WriteLine("A Cryptographic error occurred: {0}", e.Message)
            Return Nothing
        Catch e As UnauthorizedAccessException
            Console.WriteLine("A file error occurred: {0}", e.Message)
            Return Nothing
        End Try
    End Function

End
Module

4.The Encrypted file is store where the project is saved.

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor