一.加密文件
private void EncryptFile(string inputFile, string outputFile) //加密
{
try
{
//string password = @"123456";
string password = tb_password.Text;
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();
MessageBox.Show("Encrypt Source file succeed!", "Msg :");
}
catch
{
MessageBox.Show("Encrypt Source file fail!", "Error :");
}
}
二.解密文件
private void DecryptFile(string inputFile, string outputFile) //解密
{
try
{
//string password = @"123456";
string password = tb_password.Text;
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();
MessageBox.Show("Decrypt Source file succeed!", "Msg :");
}
catch
{
MessageBox.Show("Decrypt Source file fail", "Error :");
}
}