Introduction
This software allows you to encrypt and decrypt text with a specified key, yielding an encoded message, and decode encrypted messages, recovering the original text.
Background
The basic idea of this project is to save data from hackers. When you send message through encrypter, no one can read the text without having the encrypter software and key that you use.
You want to send a mail message which can be read by many people (family, friends, colleagues, etc.) When you send a message through unsecured channel (public mail server, ICQ, etc.),
you use this software.
Using the Code
Use these namespaces in your code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
Use this code for encrypting and decrypting text:
namespace Encrypter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string key, EnValue;
private void Form1_Load(object sender, EventArgs e)
{
txtKey.Text = "o7x8y6";
}
private void btnEncrypt_Click(object sender, EventArgs e)
{
key = Convert.ToString(txtKey.Text);
EnValue = Convert.ToString(txtToEncript.Text);
if (key != "" && EnValue!="" )
{
txtResult.Text=EncryptStringAES(EnValue, key);
}
else
{
lblResult.Text = "Enter text to Encrypt";
return;
}
}
private void btnDecript_Click(object sender, EventArgs e)
{
EnValue = Convert.ToString(txtResult.Text);
key = Convert.ToString(txtKey.Text);
if (key != "" && EnValue != "")
{
txtToEncript.Text = DecryptStringAES(EnValue,key);
}
else
{
lblResult.Text = "Enter text to Decrypt";
return;
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private static byte[] _salt = Encoding.ASCII.GetBytes("o6806642kbM7c5");
public static string EncryptStringAES(string plainText, string sharedSecret)
{
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException("plainText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");
string outStr = null;
RijndaelManaged aesAlg = null;
try
{
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);
using (CryptoStream csEncrypt =
new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
}
outStr = Convert.ToBase64String(msEncrypt.ToArray());
}
}
catch (Exception ex)
{
Label l1 = new Label();
l1.ForeColor = Color.Red;
l1.Text = "Enter Proper Key value.";
l1.Show();
Form1 f = new Form1();
f.Controls.Add(l1);
}
finally
{
if (aesAlg != null)
aesAlg.Clear();
}
return outStr;
}
public static string DecryptStringAES(string cipherText, string sharedSecret)
{
if (string.IsNullOrEmpty(cipherText))
throw new ArgumentNullException("cipherText");
if (string.IsNullOrEmpty(sharedSecret))
throw new ArgumentNullException("sharedSecret");
RijndaelManaged aesAlg = null;
string plaintext = null;
try
{
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(sharedSecret, _salt);
byte[] bytes = Convert.FromBase64String(cipherText);
using (MemoryStream msDecrypt = new MemoryStream(bytes))
{
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = ReadByteArray(msDecrypt);
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (CryptoStream csDecrypt =
new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
plaintext = srDecrypt.ReadToEnd();
}
}
}
catch (Exception ex)
{
Label l = new Label();
l.ForeColor = Color.Red;
l.Text="Enter Proper Key value.";
l.Show();
Form1 f = new Form1();
f.Controls.Add(l);
}
finally
{
if (aesAlg != null)
aesAlg.Clear();
}
return plaintext;
}
private static byte[] ReadByteArray(Stream s)
{
byte[] rawLength = new byte[sizeof(int)];
if (s.Read(rawLength, 0, rawLength.Length) != rawLength.Length)
{
throw new SystemException("Stream did not contain properly formatted byte array");
}
byte[] buffer = new byte[BitConverter.ToInt32(rawLength, 0)];
if (s.Read(buffer, 0, buffer.Length) != buffer.Length)
{
throw new SystemException("Did not read byte array properly");
}
return buffer;
}
private void btnCpyEncrt_Click(object sender, EventArgs e)
{
Clipboard.SetText(txtResult.Text);
}
}
}
How to Use this Service
- Enter message in the text field at the bottom.
- Enter any unordinary and unique password (and confirm password).
- Press 'Encrypt' button.
- Select and copy encrypted code and send it to respondent. It's safe to send it as an email message.
- Inform him about your secret password.
- When recipient has received encrypted message, he visits this site and decrypts message to original form using your secret password.
History
- 4th March, 2013: Initial version