Code:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFD.Title = "Insert a File";
openFD.InitialDirectory = "F://";
openFD.FileName = "";
DialogResult result = openFD.ShowDialog();
string filename;
filename = openFD.FileName;
if (result == DialogResult.OK)
{
textBox2.Text = filename;
}
else
{
return;
}
}
private void button2_Click(object sender, EventArgs e)
{
string password = textBox1.Text;
string salt_value = "abcdefg";
string hash = "sha1";
int passwordIterations = 10;
string initVector = "@1A2C5D1F286AB54";
int keySize = 128;
Encrypt(@"filename", @"C:\Encrypted.ppt", password, salt_value, hash, passwordIterations, initVector, keySize);
}
public static void Encrypt(string _inputFile, string _outputFile, string password, string salt_value, string hash, int passwordIteration, string initVector, int keySize)
{
if (password.Length > 8)
password = password.Substring(0, 8);
else if (password.Length < 8)
{
int add = 8 - password.Length;
for (int i = 0; i < add; i++)
password = password + i;
}
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] salt_valueBytes = Encoding.ASCII.GetBytes(salt_value);
PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt_valueBytes, hash, passwordIteration);
byte[] keyBytes = key.GetBytes(keySize / 8);
FileStream fsCrypt = new FileStream(_outputFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.Mode = CipherMode.CBC;
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(keyBytes, initVectorBytes), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(_inputFile, FileMode.OpenOrCreate);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
cs = null;
fsIn = null;
}
private void button3_Click(object sender, EventArgs e)
{
openFD.Title = "Insert a File";
openFD.InitialDirectory = "F://";
openFD.FileName = "";
DialogResult result = openFD.ShowDialog();
string filename;
filename = openFD.FileName;
if (result == DialogResult.OK)
{
textBox3.Text = filename;
}
else
{
return;
}
}
private void button4_Click(object sender, EventArgs e)
{
string password = textBox1.Text;
string salt_value = "abcdefg";
string hash = "sha1";
int passwordIterations = 10;
string initVector = "@1A2C5D1F286AB54";
int keySize = 128;
Decrypt(@"filename", @"C:\Decrypted.ppt", password, salt_value, hash, passwordIterations, initVector, keySize);
}
public static void Decrypt(string _inputFile, string _outputFile, string password, string salt_value, string hash, int passwordIteration, string initVector, int keySize)
{
if (password.Length > 8)
password = password.Substring(0, 8);
else if (password.Length < 8)
{
int add = 8 - password.Length;
for (int i = 0; i < add; i++)
password = password + i;
}
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] salt_valueBytes = Encoding.ASCII.GetBytes(salt_value);
PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt_valueBytes, hash, passwordIteration);
byte[] keyBytes = key.GetBytes(keySize /8);
FileStream fsCrypt = new FileStream(_outputFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.Mode = CipherMode.CBC;
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(keyBytes, initVectorBytes), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(_inputFile, FileMode.OpenOrCreate);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
cs.Close();~IndexOutOfRangeException was unhandled
fsIn.Close();
fsCrypt.Close();
cs = null;
fsIn = null;
fsCrypt = null;
}
}
}
The source code can be download from the link below: