1. Hashing
Hashing is a type of encryption in which we can only encrypt the data ,but can't decrypt it .
Here is the code implementation
import the following namespace:
using System.Security.Cryptography;
private void buttonGenerate_Click(object sender, EventArgs e)
{
UnicodeEncoding ue = new UnicodeEncoding();
byte[] normalPass = ue.GetBytes(textBoxPassword.Text);
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hashCode = md5.ComputeHash(normalPass);
labelHashCode.Text = Convert.ToBase64String(hashCode);
}
2. Encryption Decryption
In the following program encryption and decryption is done based on an 8 bit key.
using System.Security.Cryptography;
using System.IO;
using System.Diagnostics;
namespace CryptoGraphyy
{
public partial class EncryptDecrypt : Form
{
static string EncrypData;
static byte[] bytes = System.Text.ASCIIEncoding.ASCII.GetBytes("HellOWor");
private void buttonEncrypt_Click(object sender, EventArgs e)
{
try
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(textBox1.Text);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
EncrypData = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
StreamWriter sw = new StreamWriter(@"C:\MyEncryption.txt");
sw.Write(EncrypData);
sw.Flush();
sw.Close();
MessageBox.Show("Data Encrypted!");
Process.Start("notepad",@"C:\MyEncryption.txt");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonDecrypt_Click(object sender, EventArgs e)
{
try
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(EncrypData));
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
StreamWriter sw = new StreamWriter(@"C:\MyDecrypt.txt");
sw.Write(reader.ReadToEnd());
sw.Flush();
sw.Close();
MessageBox.Show("Data Decrypted!");
Process.Start("notepad", @"C:\MyDecrypt.txt");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
3. Encryption and Decryption using Rijndael Algorithm
using System.IO;
using System.Security.Cryptography;
using System.Diagnostics;
namespace CryptoGraphyy
{
public partial class PasswordEncryption : Form
{
string passValue;
public PasswordEncryption()
{
InitializeComponent();
}
private void buttonEncrypt_Click(object sender, EventArgs e)
{
try
{
passValue = textBox1.Text;
RandomNumberGenerator rnd = RandomNumberGenerator.Create();
byte[] salt = new byte[16];
rnd.GetBytes(salt);
Rfc2898DeriveBytes derBytes = new Rfc2898DeriveBytes(passValue, salt);
byte[] key = derBytes.GetBytes(16);
Rijndael cryptoAlg = Rijndael.Create();
cryptoAlg.Key = key;
FileStream fs = new FileStream(@"d:\EncryptPass.txt", FileMode.Create);
fs.Write(salt, 0, salt.Length);
fs.Write(cryptoAlg.IV, 0, cryptoAlg.IV.Length);
CryptoStream cryptStream = new CryptoStream(fs, cryptoAlg.CreateEncryptor(), CryptoStreamMode.Write);
//Converting a string to an byte array
byte[] ar = System.Text.Encoding.UTF8.GetBytes(passValue);
cryptStream.Write(ar, 0, ar.Length);
cryptStream.FlushFinalBlock();
cryptStream.Close();
fs.Close();
MessageBox.Show("Data Encrypted");
Process.Start("notepad", @"d:\EncryptPass.txt");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void buttonDecrypt_Click(object sender, EventArgs e)
{
try
{
passValue = textBox1.Text;
Rijndael deCryptoAlg = Rijndael.Create();
FileStream fs3 = new FileStream(@"d:\DecryptPass.txt", FileMode.OpenOrCreate);
FileStream fs2 = new FileStream(@"d:\EncryptPass.txt", FileMode.Open);
byte[] salt = new byte[16];
byte[] IV = new byte[deCryptoAlg.IV.Length];
fs2.Read(salt, 0, salt.Length);
fs2.Read(IV, 0, IV.Length);
Rfc2898DeriveBytes derByte = new Rfc2898DeriveBytes(passValue, salt);
byte[] key = derByte.GetBytes(16);
deCryptoAlg.Key = key;
deCryptoAlg.IV = IV;
CryptoStream cryStream = new CryptoStream(fs2, deCryptoAlg.CreateDecryptor(), CryptoStreamMode.Read);
int bytesRead = 0;
byte[] buffer = new byte[256];
do
{
bytesRead = cryStream.Read(buffer, 0, 256);
} while (bytesRead > 0);
fs3.Write(buffer, 0, 256);
fs3.Flush();
fs3.Close();
fs2.Close();
cryStream.Close();
MessageBox.Show("File Decrypted");
Process.Start("notepad", @"d:\DecryptPass.txt");
}
catch (Exception ex)
{
MessageBox.Show("Error"+ex.Message);
}
}
}
}
0 comments:
Post a Comment