21-07-2011, 09:50 AM
Aim:
To write a C# program to perform encryption and decryption of the given data.
Algorithm
ALGORITHM FOR ENCRYPTION:
Step 1: Declare the class as encrypt_class.
Step 2: Start the main function.
Step 3: Declare the variable str in string data type.
Step 4: Create the object for inbuild encryption
algorithm TripleDESCryptoServiceProvider.
Step 5: Create the data file using Filestream
class.
Step 6: Create the object for class cryptoStream.
Cryptostream is a class to invoke the
Encryotor algorithm.
Get the input from the user using Console.Readline().
Step 7: Write the input data using WriteLine function.
Step 8: Create the key file using File.Create.
Step 9: convert the key file into binary form.
Step 10:Close the file.
Step 11rint Data encrypted using console.writeLine.
ALGORITHM FOR DECRYPTION :
Step 1: Declare a class as decrypt_class
Step 2: start the main function
Step 3:Create the object for the inbuild algorithm
TripleDESCrypotserviceProvider.
Step 4: Create the file in c drive using FileStream fs =File.openRead.
Step 5: Using Binary Reader convert file as in binary form.
Step 6:Read the number of bytes of key file.
Step 7: Open the data file using FileRead function.
Step 8: Cryptostream is a class to use invoke the Decrytptor.
Step 9: Create the object for CryptoStream class.
Step 10: Using StreamReader read the data file .
Step 11: Print the content of data file using Console.writeLine.
Step 12: Close the file.
Program:
Code:
Encryption
using System;
using System.IO;
using System.Security.Cryptography;
class encrypt_class
{
public static void Main()
{
String str;
TripleDESCryptoServiceProvider cp= new TripleDESCryptoServiceProvider();
FileStream fs = File.Create("C:\\file.dat");
CryptoStream cs = new
CryptoStream(fs,cp.CreateEncryptor(),CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
Console.WriteLine("Enter a string");
str=Console.ReadLine();
sw.WriteLine(str);
sw.Close();
fs= File.Create("C:\\file.key");
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(cp.Key);
bw.Write(cp.IV);
bw.Close();
Console.WriteLine("Data Encrypted........");
}
}
Decryption:
using System;
using System.IO;
using System.Security.Cryptography;
class decrypt_class
{
public static void Main()
{
TripleDESCryptoServiceProvider cp= new TripleDESCryptoServiceProvider();
FileStream fs = File.OpenRead("C:\\file.key");
BinaryReader br = new BinaryReader(fs);
cp.Key=br.ReadBytes(24);
cp.IV=br.ReadBytes(8);
fs= File.OpenRead("C:\\file.dat");
CryptoStream cs = new
CryptoStream(fs,cp.CreateDecryptor(),CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
Console.WriteLine("Content in the file (After Decryption)");
Console.WriteLine(sr.ReadLine());
sr.Close();
}
}
Execution:
csc encrypt_class.cs
encrypt_class file
Enter the string
Abcdefghijklmnopqrstuvwxyz
Data encrypted……
csc decrypt_class.cs
decrypt_class file
Content in the file is (After decryption)
Abcdefghijklmnopqrstuvwxyz.
ENCRYPTION AND DECRYPTION PROGRAM USING COM
Code:
Dim dstr As String
Dim i As Integer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
For i = 1 To Len(TextBox1.Text)
dstr = dstr + Chr(Asc(Mid(TextBox1.Text, i, 1)) + i)
Next
TextBox1.Text = dstr
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
dstr = ""
For i = Len(TextBox1.Text) To 1 Step -1
dstr = Chr(Asc(Mid(TextBox1.Text, i, 1)) - i) + dstr
Next
TextBox1.Text = dstr
End Sub