Another flawlessly written program to up your C# skills! The code is written and tested in Visual Studio 2012. Features of this project are:
Please Note:
** Do not Copy & Paste code written here ; instead type it in your Development Environment
** Testing done in .Net Framework 4.5 but code should be very similar for previous versions of .Net Framework
** All Program Codes written here are 100% tested & running.
- Encrypt and Decrypt any type of file/s in your computer system.
- Easy to use interface. Few clicks required only.
- Choose passkey also known as a password. May consist of letters and numbers[Maximum 8].
- Keep original files intact. You may change code to delete original files but we did not implement this currently.
- A class with functions to encrypt and decrypt files is created. This file may be reused.
- The Rijndael algorithm of Encryption and Decryption is used internally to Cipher & Decipher.
C# Encryption Decryption |
public partial class MainForm
: Form
{
public
MainForm()
{
InitializeComponent();
}
string
encfileextention;
bool
encflagOpen;
bool
encflagSave;
ENC_DEC
ed = new ENC_DEC();
string
decfileextention;
bool
decflagOpen;
bool
decflagSave;
private void
rdoEncryption_CheckedChanged(object
sender, EventArgs e)
{
if
(rdoEncryption.Checked == true)
{
gbEncryption.Visible = true;
gbDecryption.Visible = false;
}
else
{
gbEncryption.Visible = false;
gbDecryption.Visible = true;
}
}
private void
rdoDecryption_CheckedChanged(object
sender, EventArgs e)
{
//Same as for the above radio button
}
private void
MainForm_Load(object sender, EventArgs
e)
{
MessageBox.Show("Welcome!");
}
private void
btnEncOpen_Click(object sender, EventArgs
e)
{
OpenFileDialog
dlg = new OpenFileDialog();
dlg.Filter = "All
files (*.*)|*.*";
if
(dlg.ShowDialog() == DialogResult.OK)
{
String
s = dlg.FileName;
txtEncInput.Text = s;
FileInfo
myfile = new FileInfo(s);
if
((myfile.Length / 1024000) == 0)
{
lblEncSize.Text = "SIZE
: " + Convert.ToString(myfile.Length
/ 1024) + " Kb";
}
else
{
lblEncSize.Text = "SIZE
: " + Convert.ToString(myfile.Length
/ 1024000) + " Mb";
}
encfileextention =
myfile.Extension;
encflagOpen = true;
if
((encflagSave == true) && (encflagOpen == true))
{
btnEncrypt.Enabled = true;
encflagOpen = false;
encflagSave = false;
}
}
}
private void
btnEncSave_Click(object sender, EventArgs
e)
{
SaveFileDialog
dlg = new SaveFileDialog();
dlg.Filter = "All
files (*.*)|*.*";
if
(dlg.ShowDialog() == DialogResult.OK)
{
string
s = dlg.FileName;
txtEncOutput.Text = s;
String
m = dlg.FileName;
FileInfo
myfile = new FileInfo(m);
if
(myfile.Extension == null)
txtEncOutput.Text = m;
else
txtEncOutput.Text = m +
encfileextention;
encflagSave = true;
}
if
((encflagSave == true) && (encflagOpen == true))
{
btnEncrypt.Enabled = true;
encflagOpen = false;
encflagSave = false;
}
}
private void
btnDecOpen_Click(object sender, EventArgs
e)
{
OpenFileDialog
dlg = new OpenFileDialog();
dlg.CheckFileExists = true;
dlg.Filter = "All
files (*.*)|*.*";
if
(dlg.ShowDialog() == DialogResult.OK)
{
String
s = dlg.FileName;
txtDecInput.Text = s;
FileInfo
myfile = new FileInfo(s);
if
((myfile.Length / 1024000) == 0)
{
lblDecSize.Text = "SIZE
: " + Convert.ToString(myfile.Length
/ 1024) + " Kb";
}
else
{
lblDecSize.Text = "SIZE
: " + Convert.ToString(myfile.Length
/ 1024000) + " Mb";
}
decfileextention =
myfile.Extension;
decflagOpen = true;
}
else
MessageBox.Show("You
did not select any file");
if
((decflagSave == true) && (decflagOpen == true))
{
btnDecrypt.Enabled = true;
decflagOpen = false;
decflagSave = false;
}
}
private void
btnDecSave_Click(object sender, EventArgs
e)
{
SaveFileDialog
dlg = new SaveFileDialog();
dlg.Filter = "All
files (*.*)|*.*";
if
(dlg.ShowDialog() == DialogResult.OK)
{
string
m = dlg.FileName;
txtDecOutput.Text = m;
FileInfo
myfile = new FileInfo(m);
if
(myfile.Extension == null)
txtDecOutput.Text = m;
else
txtDecOutput.Text = m +
decfileextention;
}
else
MessageBox.Show("You
have not selected any file");
decflagSave = true;
if
((decflagSave == true) && (decflagOpen == true))
{
btnDecrypt.Enabled = true;
decflagOpen = false;
decflagSave = false;
}
}
private void
btnEncrypt_Click(object sender, EventArgs
e)
{
this.Text
= "Please wait";
ed.EncFile(txtEncInput.Text, txtEncOutput.Text,
textBox1.Text + "qlku");
MessageBox.Show("New
File Location : '" + txtEncOutput.Text + "'",
encfileextention + " file converted successfully");
this.Text
= "Password protect your files here !";
}
private void
btnDecrypt_Click(object sender, EventArgs
e)
{
this.Text
= "Please wait";
ed.DecFile(txtDecInput.Text,
txtDecOutput.Text, textBox2.Text + "qlku");
MessageBox.Show("New
File Location : '" + txtDecOutput.Text + "'",
decfileextention + " file converted successfully");
this.Text
= "Password protect your files here !";
}
}
class ENC_DEC
{
public void
DecFile(string inputFile, string
outputFile, string Pass)
{
try
{
string
password = Pass; // Your Key Here
UnicodeEncoding
UE = new UnicodeEncoding();
byte[]
key = UE.GetBytes(password);
FileStream
fsCrypt = new FileStream(inputFile,
FileMode.Open);
RijndaelManaged
RMCrypto = new RijndaelManaged();
CryptoStream
cs = new CryptoStream(fsCrypt,
RMCrypto.CreateDecryptor(key, key), CryptoStreamMode.Read);
FileStream
fsOut = new FileStream(outputFile,
FileMode.Create);
int
data;
while
((data = cs.ReadByte()) != -1)
{
fsOut.WriteByte((byte)data);
}
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
catch
{
MessageBox.Show("You
have entered wrong password");
}
}
public void
EncFile(string inputFile, string
outputFile, string Mypassword)
{
try
{
string
password = @Mypassword; // Your Key Here
UnicodeEncoding
UE = new UnicodeEncoding();
byte[]
key = UE.GetBytes(password);
string
cryptFile = outputFile;
FileStream
fsCrypt = new FileStream(cryptFile,
FileMode.Create);
RijndaelManaged
RMCrypto = new RijndaelManaged();
CryptoStream
cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write);
FileStream
fsIn = new FileStream(inputFile,
FileMode.Open);
int
data;
while
((data = fsIn.ReadByte()) != -1)
{
cs.WriteByte((byte)data);
}
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch
{
MessageBox.Show("Encryption
failed!", "Error");
}
}
}
Please Note:
** Do not Copy & Paste code written here ; instead type it in your Development Environment
** Testing done in .Net Framework 4.5 but code should be very similar for previous versions of .Net Framework
** All Program Codes written here are 100% tested & running.