Monday, 28 January 2013

Encrypting a File

Cryptography is an indispensable tool for protecting information in computer systems.Cryptography plays an important role in Security. You Encrypt and Decrypt the information using cryptography.

ncrypt the File using DES (Data Encryption standard) algorithm.

1.Using Visual Studio create a project

2.Add the following assembly on the page

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.IO;
sing Microsoft.VisualBasic;

3.Now write the Code in the Page

public class CrytographySample
{
    public static void Main()
    {
         try
        {
            DESCryptoServiceProvider sampleDESProvider = new DESCryptoServiceProvider();
             sampleDESProvider.Key = ASCIIEncoding.ASCII.GetBytes("987654321");
            sampleDESProvider.IV = ASCIIEncoding.ASCII.GetBytes("Architais@HM");

            ICryptoTransform sampleCryptoTransform = myDESProvider.CreateEncryptor(sampleDESProvider.Key ,sampleDESProvider.IV );

            FileStream SampleInFileStream = new FileStream("sample.txt", FileMode.Open, FileAccess.Read);
            FileStream SampleOutFileStream = new FileStream("SampleDes.txt", FileMode.Create, FileAccess.Write);
            CryptoStream sampleCryptoStream = new CryptoStreamSampleOutFileStream sampleCryptoTransform ,CryptoStreamMode.Write);
             byte[] bytearraysample = new byte[SampleInFileStream .Length];
             SampleInFileStream .Read(bytearrayinput, 0, bytearrayinput.Length);
            sampleCryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length);
             sampleCryptoStream.Close();
            SampleInFileStream.Close();
            SampleOutFileStream.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

4.Now create a text file in the folder appliation bin folder with some text.

5. Run the application.
Yollla! the text is encrypted.
In case you find any difficulties in doing this. Send a mail to me.

No comments:

Post a Comment