Encrypt and Decrypt Data

advertisement
CS407- Distributed Systems & Databases
BS Computer Science(SSUET)
LAB #9
Encrypt And Decrypt Data
Introduction:
1. Asymmetric Encryption: Encrypting data with a key (public-key or private-key) of a key-pair
makes it computationally infeasible to decrypt without using the complement key in the same
key-pair.
2. Symmetric Encryption: Encrypting data with a key (secret-key) makes it computationally
infeasible to decrypt without using the same key. Also symmetric encryption is faster than
asymmetric
encryption.
3. Hashing: Produces a unique message digest of known fixed size and it is computationally
infeasible to get the original data from Message Digest.
(RC2CryptoServiceProvider, DESCryptoService Provider, TrippleDESCryptoService Provider
and RijndaelManaged)
Engr. Muhammad Nadeem
Page 1
CS407- Distributed Systems & Databases
BS Computer Science(SSUET)
TASK#1 :
Public Key Encryption (RSA Method) for Encryption and Decryption
The RSA Public Key Encryption is useful method for Encryption and Decryption .The Code behind
the Method is mention below.
using
using
using
using
using
using
using
using
using
System;
System.Collections;
System.Collections.Generic;
System.Data;
System.Diagnostics;
System.Security;
System.Security.Cryptography;
System.Text;
System.IO;
public class Tester
{
public static void Main()
{
RSACryptoServiceProvider myRSAProvide = new RSACryptoServiceProvider();
string strCrypt = null;
byte[] bteCrypt = null;
byte[] bteResult = null;
try
{
strCrypt = "12345678";
bteCrypt = Encoding.ASCII.GetBytes(strCrypt);
bteResult = myRSAProvide.Encrypt(bteCrypt, false);
Console.WriteLine(Encoding.ASCII.GetString(bteResult));
}
catch (CryptographicException ex)
Engr. Muhammad Nadeem
Page 2
CS407- Distributed Systems & Databases
BS Computer Science(SSUET)
{
Console.WriteLine(ex.Message);
}
string strResault = null;
byte[] bteDecrypt = null;
try
{
bteDecrypt = myRSAProvide.Decrypt(bteResult, false);
strResault = Encoding.ASCII.GetString(bteDecrypt);
Console.WriteLine(strResault);
}
catch (CryptographicException ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
It is very simple pubic key encryption and decryption method.
TASK # 2 :
Triple DES Encryption and Decryption using User provided key
Objective:
In this article, I will explain how to do a Triple DES encryption on a plain text using user
provided key. I will calculate a MD5 Hash on the key provided by the user. And that key will
be user to encrypt and decrypt the message.
Explanation of DES
DES is a symmetric key encryption algorithm. Same key is being used for encryption and
decryption. So challenge in using symmetric key algorithm is that we need to have the same
key for decryption which is used for encryption. People follow different approach to save key.
Either they append key with cryptic text or physically save it somewhere. I am going to ask
user to input some string as key. I will calculate MD5 hash on that string input by user to
make key. Then I will use this key to encrypt and decrypt the plain text.
Working
Engr. Muhammad Nadeem
Page 3
CS407- Distributed Systems & Databases
BS Computer Science(SSUET)
1. User will enter the key
2. User will enter the plain text
3. When User will click the Encrypt button, plain text will get encrypted and display in
textbox2.
4. When user will click on Decrypt button in textbox3 plain text will get display.
Screen
Functions :
Function to create DES
This function will create TripleDES instance. This is taking a string as key value and will
calculate MD5 hash on input parameter string. This hash value would be used as real key for
the encryption.
static TripleDES CreateDES(string key)
{
MD5 md5 = new MD5CryptoServiceProvider();
TripleDES des = new TripleDESCryptoServiceProvider();
des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key));
des.IV = new byte[des.BlockSize / 8];
return des;
}
Engr. Muhammad Nadeem
Page 4
CS407- Distributed Systems & Databases
BS Computer Science(SSUET)
Function to Encrypt
1. This function is taking Plain text to encrypt and key
2. This function is returning a byte array
3. As parameter for CreateDES , I am passing the key.
public static byte[] Encryption(string PlainText,string key)
{
TripleDES des = CreateDES(key);
ICryptoTransform ct = des.CreateEncryptor();
byte[] input = Encoding.Unicode.GetBytes(PlainText);
return ct.TransformFinalBlock(input, 0, input.Length);
}
Function to Decrypt
1. This function is taking key and CypherText to encrypt.
2. It is returning a string.
3. It is creating TripleDES on given key.
public static string Decryption(string CypherText,string key)
{
byte[] b = Convert.FromBase64String(CypherText);
TripleDES des = CreateDES(key);
ICryptoTransform ct = des.CreateDecryptor();
byte[] output = ct.TransformFinalBlock(b, 0, b.Length);
return Encoding.Unicode.GetString(output);
}
Full Code
Below is the full code for encryption and decryption. There are two button click events on
which we are performing the action.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace Encryptionusing_Des
Engr. Muhammad Nadeem
Page 5
CS407- Distributed Systems & Databases
BS Computer Science(SSUET)
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Encrypt_Click(object sender, EventArgs e)
{
byte[] buffer = Encryption(textBox1.Text,txtKey.Text);
string b = Convert.ToBase64String(buffer);
textBox2.Text = b;
}
public static byte[] Encryption(string PlainText,string key)
{
TripleDES des = CreateDES(key);
ICryptoTransform ct = des.CreateEncryptor();
byte[] input = Encoding.Unicode.GetBytes(PlainText);
return ct.TransformFinalBlock(input, 0, input.Length);
}
public static string Decryption(string CypherText,string key)
{
byte[] b = Convert.FromBase64String(CypherText);
TripleDES des = CreateDES(key);
ICryptoTransform ct = des.CreateDecryptor();
byte[] output = ct.TransformFinalBlock(b, 0, b.Length);
return Encoding.Unicode.GetString(output);
}
private void Decrypt_Click(object sender, EventArgs e)
{
textBox3.Text = Decryption(textBox2.Text,txtKey.Text);
}
static TripleDES CreateDES(string key)
{
MD5 md5 = new MD5CryptoServiceProvider();
TripleDES des = new TripleDESCryptoServiceProvider();
des.Key = md5.ComputeHash(Encoding.Unicode.GetBytes(key));
des.IV = new byte[des.BlockSize / 8];
return des;
}
}
}
Output
Engr. Muhammad Nadeem
Page 6
CS407- Distributed Systems & Databases
Engr. Muhammad Nadeem
BS Computer Science(SSUET)
Page 7
Download