using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace Hcryptor
{
public partial
class
Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls
=
false;
}
private void Form1_Load(
object
sender, EventArgs e)
{
notifyIcon1.ShowBalloonTip(
999
,
"Welcome to use Hcryptor"
,
"Written by henry217"
,ToolTipIcon.
None
);
}
private void checkBox1_CheckedChanged(
object
sender, EventArgs e)
{
if
(checkBox1.Checked)
{
checkBox2.Checked
=
false;
}
}
private void checkBox2_CheckedChanged(
object
sender, EventArgs e)
{
if
(checkBox2.Checked)
{
checkBox1.Checked
=
false;
}
}
/
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
functions
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
/
public static byte[] AESEncrypt(byte[] Data, string Key, string Vector)
{
byte[] bKey
=
new byte[
32
];
/
/
采用
32
位密码加密
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
/
/
如果用户输入的密码不足
32
位,自动填充空格至
32
位
byte[] bVector
=
new byte[
16
];
/
/
密钥向量,为
16
位
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
/
/
如果用户定义的密钥向量不足
16
位,自动填充空格至
16
位
byte[] Cryptograph
=
null;
/
/
加密后的密文
Rijndael Aes
=
Rijndael.Create();
try
{
using (MemoryStream Memory
=
new MemoryStream())
{
/
/
把内存流对象包装成加密流对象
using (CryptoStream Encryptor
=
new CryptoStream(Memory, Aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write))
{
Encryptor.Write(Data,
0
, Data.Length);
Encryptor.FlushFinalBlock();
Cryptograph
=
Memory.ToArray();
}
}
}
catch
{
Cryptograph
=
null;
}
return
Cryptograph;
}
public static byte[] AESDecrypt(byte[] Data, string Key, string Vector)
{
byte[] bKey
=
new byte[
32
];
Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);
byte[] bVector
=
new byte[
16
];
Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);
byte[] original
=
null;
/
/
解密后的明文
Rijndael Aes
=
Rijndael.Create();
try
{
using (MemoryStream Memory
=
new MemoryStream(Data))
{
/
/
把内存流对象包装成加密对象
using (CryptoStream Decryptor
=
new CryptoStream(Memory, Aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read))
{
/
/
明文存储区
using (MemoryStream originalMemory
=
new MemoryStream())
{
byte[]
Buffer
=
new byte[
1024
];
int
readBytes
=
0
;
while
((readBytes
=
Decryptor.Read(
Buffer
,
0
,
Buffer
.Length)) >
0
)
{
originalMemory.Write(
Buffer
,
0
, readBytes);
}
original
=
originalMemory.ToArray();
}
}
}
}
catch
{
original
=
null;
}
return
original;
}
private void button1_Click(
object
sender, EventArgs e)
{
openFileDialog1.ShowDialog();
foreach (string path
in
openFileDialog1.FileNames)
{
if
(
File
.Exists(path))
{
richTextBox1.Text
=
richTextBox1.Text
+
path
+
"\n"
;
}
}
}
private void button2_Click(
object
sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
if
(checkBox1.Checked
=
=
false&&checkBox2.Checked
=
=
false)
{
notifyIcon1.ShowBalloonTip(
999
,
"Warning"
,
"You should select what to do!"
, ToolTipIcon.Warning);
return
;
}
if
(richTextBox1.Text
=
=
"")
{
notifyIcon1.ShowBalloonTip(
999
,
"Warning"
,
"You should choose files to perform!"
, ToolTipIcon.Warning);
return
;
}
if
(textBox1.Text
=
=
"")
{
notifyIcon1.ShowBalloonTip(
999
,
"Warning"
,
"You should enter the password!"
, ToolTipIcon.Warning);
return
;
}
if
(textBox2.Text
=
=
""&&checkBox1.Checked
=
=
true)
{
notifyIcon1.ShowBalloonTip(
999
,
"Warning"
,
"You should enter the extension of the encrypted files!"
, ToolTipIcon.Warning);
return
;
}
progressBar1.Style
=
ProgressBarStyle.Marquee;
backgroundWorker1.RunWorkerAsync();
progressBar1.Style
=
ProgressBarStyle.Blocks;
}
private void backgroundWorker1_DoWork(
object
sender, DoWorkEventArgs e)
{
foreach(string path
in
richTextBox1.Lines)
{
/
/
MessageBox.Show(path);
if
(
File
.Exists(path))
{
try
{
byte[] os
=
FileContent(path),ns;
if
(checkBox1.Checked
=
=
true)
{
/
/
加密
ns
=
AESEncrypt(os, textBox1.Text, textBox1.Text);
FileStream fs
=
new FileStream(path
+
"."
+
textBox2.Text, FileMode.Create, FileAccess.Write);
fs.Write(ns,
0
, ns.Length);
fs.Close();
File
.Delete(path);
progressBar1.Style
=
ProgressBarStyle.Blocks;
richTextBox1.Text
=
"";
notifyIcon1.ShowBalloonTip(
999
,
"Info"
,
"Done"
, ToolTipIcon.Info);
}
else
{
ns
=
AESDecrypt(os, textBox1.Text, textBox1.Text);
FileInfo fi
=
new FileInfo(path);
string np
=
fi.DirectoryName
+
Path.GetFileNameWithoutExtension(path);
FileStream wi
=
new FileStream(np, FileMode.Create, FileAccess.Write);
wi.Write(ns,
0
, ns.Length);
wi.Close();
progressBar1.Style
=
ProgressBarStyle.Blocks;
richTextBox1.Text
=
"";
notifyIcon1.ShowBalloonTip(
999
,
"Info"
,
"Done"
, ToolTipIcon.Info);
}
}
catch(Exception)
{
notifyIcon1.ShowBalloonTip(
999
,
"Error"
,
"Something went wrong!"
, ToolTipIcon.Error);
}
}
else
{
if
(path!
=
"")
notifyIcon1.ShowBalloonTip(
999
,
"Error"
, path
+
" doesn't exist"
,ToolTipIcon.Error);
continue
;
}
}
}
private byte[] FileContent(string fileName)
{
using (FileStream fs
=
new FileStream(fileName, FileMode.
Open
, FileAccess.Read))
{
try
{
byte[] buffur
=
new byte[fs.Length];
fs.Read(buffur,
0
, (
int
)fs.Length);
fs.Close();
return
buffur;
}
catch (Exception ex)
{
throw ex;
}
}
}
private void button3_Click(
object
sender, EventArgs e)
{
checkBox1.Checked
=
false;
checkBox2.Checked
=
false;
textBox1.Text
=
"";
textBox2.Text
=
"";
richTextBox1.Text
=
"";
}
}
}