using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace 文件加密
{
/// <summary>
/// 文件加密类
/// </summary>
public class EncryptedJM
{
/// <summary>
/// 文件加密
/// </summary>
/// <param name="infile">输入文件(要加密的文件)</param>
/// <param name="outfile">输出文件(加密后的文件)</param>
/// <param name="password">密码</param>
public static void Encrypt(string infile,string outfile,string password)
{
byte[] buffer = Encoding.Default.GetBytes(password);//将密码转换为字节数组
MD5 md5 = MD5.Create();//创建MD5实例
byte[] hash = md5.ComputeHash(buffer, 0, buffer.Length);进行MD5加密,结果为128位16个字节,作为密钥
byte[] iv = new byte[8];//初始化向量,64位
for (int i = 0; i < 8; i++)
{
iv[i] = hash[i];
}
TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
tdsp.Key = hash;//密钥
tdsp.IV = iv;
FileStream infs = new FileStream(infile, FileMode.OpenOrCreate, FileAccess.Read);//读取文件流
FileStream outfs = new FileStream(outfile, FileMode.Create, FileAccess.Write);//写入文件流
CryptoStream cs = new CryptoStream(outfs, tdsp.CreateEncryptor(tdsp.Key, tdsp.IV), CryptoStreamMode.Write);//加密转换流
byte[] datas = new byte[1024];
int size = 0;
outfs.Write(hash, 0, hash.Length);//将密钥写入文件头
BinaryWriter bw = new BinaryWriter(cs);
while ((size = infs.Read(datas, 0, 1024)) != 0) //循环写入数据
{
//cs.Write(datas, 0, size);
//cs.Flush();
bw.Write(datas, 0, size);
}
bw.Flush();
bw.Close();
cs.Close();
outfs.Close();
infs.Close();
System.Windows.Forms.MessageBox.Show("加密完成!", "消息");
}
/// <summary>
/// 文件解密
/// </summary>
/// <param name="infile">输入文件(要解密的文件)</param>
/// <param name="outfile">输出文件,解密后的文件</param>
/// <param name="password">密码</param>
public static void DeEncrypt(string infile, string outfile, string password)
{
byte[] key = new byte[16];
FileStream infs = new FileStream(infile, FileMode.OpenOrCreate, FileAccess.Read);
infs.Read(key, 0, 16);//前16个字节为密钥
byte[] buffer = Encoding.Default.GetBytes(password);//将密码转换为字节数组
MD5 md5 = MD5.Create();//创建MD5实例
byte[] hash = md5.ComputeHash(buffer, 0, buffer.Length);进行MD5加密,结果为128位,16个字节
if (!CompareBytes(hash, key)) //比较读取进来的密码和文件加密时的密码
{
System.Windows.Forms.MessageBox.Show("密码错误", "信息");
infs.Close();
return;
}
byte[] iv = new byte[8];
for (int i = 0; i < 8; i++)
{
iv[i] = key[i];
}
TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
tdsp.Key = key;//密钥
tdsp.IV = iv;
//infs.Seek(0, SeekOrigin.Begin);
FileStream outfs = new FileStream(outfile, FileMode.Create, FileAccess.Write);
//byte[] d = new byte[infs.Length-16];
//infs.Read(d, 0,d.Length);
//MemoryStream ms = new MemoryStream(d);
CryptoStream cs = new CryptoStream(infs, tdsp.CreateDecryptor(tdsp.Key,tdsp.IV), CryptoStreamMode.Read);
byte bt = new byte();
bt = (byte)cs.ReadByte();
while (infs.Position<infs.Length)
{
outfs.WriteByte(bt);
try
{
bt = (byte)cs.ReadByte();//这里不知道原因,会发生异常。
}
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
break;
}
}
// br.Close();
//cs.Close();
cs.Flush();
cs.Close();
infs.Close();
outfs.Close();
System.Windows.Forms.MessageBox.Show("解密完成!","消息");
}
/// <summary>
/// 比较两个字节数组是否相等,此处用于校验密码
/// </summary>
/// <param name="bt1"></param>
/// <param name="bt2"></param>
/// <returns></returns>
private static bool CompareBytes(byte[] bt1, byte[] bt2)
{
if (bt1.Length != bt2.Length)
{
return false;
}
bool r = true;
for (int i = 0; i < bt1.Length; i++)
{
if (bt1[i] != bt2[i])
{
r = false;
break;
}
}
return r;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace 文件加密
{
/// <summary>
/// 文件加密类
/// </summary>
public class EncryptedJM
{
/// <summary>
/// 文件加密
/// </summary>
/// <param name="infile">输入文件(要加密的文件)</param>
/// <param name="outfile">输出文件(加密后的文件)</param>
/// <param name="password">密码</param>
public static void Encrypt(string infile,string outfile,string password)
{
byte[] buffer = Encoding.Default.GetBytes(password);//将密码转换为字节数组
MD5 md5 = MD5.Create();//创建MD5实例
byte[] hash = md5.ComputeHash(buffer, 0, buffer.Length);进行MD5加密,结果为128位16个字节,作为密钥
byte[] iv = new byte[8];//初始化向量,64位
for (int i = 0; i < 8; i++)
{
iv[i] = hash[i];
}
TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
tdsp.Key = hash;//密钥
tdsp.IV = iv;
FileStream infs = new FileStream(infile, FileMode.OpenOrCreate, FileAccess.Read);//读取文件流
FileStream outfs = new FileStream(outfile, FileMode.Create, FileAccess.Write);//写入文件流
CryptoStream cs = new CryptoStream(outfs, tdsp.CreateEncryptor(tdsp.Key, tdsp.IV), CryptoStreamMode.Write);//加密转换流
byte[] datas = new byte[1024];
int size = 0;
outfs.Write(hash, 0, hash.Length);//将密钥写入文件头
BinaryWriter bw = new BinaryWriter(cs);
while ((size = infs.Read(datas, 0, 1024)) != 0) //循环写入数据
{
//cs.Write(datas, 0, size);
//cs.Flush();
bw.Write(datas, 0, size);
}
bw.Flush();
bw.Close();
cs.Close();
outfs.Close();
infs.Close();
System.Windows.Forms.MessageBox.Show("加密完成!", "消息");
}
/// <summary>
/// 文件解密
/// </summary>
/// <param name="infile">输入文件(要解密的文件)</param>
/// <param name="outfile">输出文件,解密后的文件</param>
/// <param name="password">密码</param>
public static void DeEncrypt(string infile, string outfile, string password)
{
byte[] key = new byte[16];
FileStream infs = new FileStream(infile, FileMode.OpenOrCreate, FileAccess.Read);
infs.Read(key, 0, 16);//前16个字节为密钥
byte[] buffer = Encoding.Default.GetBytes(password);//将密码转换为字节数组
MD5 md5 = MD5.Create();//创建MD5实例
byte[] hash = md5.ComputeHash(buffer, 0, buffer.Length);进行MD5加密,结果为128位,16个字节
if (!CompareBytes(hash, key)) //比较读取进来的密码和文件加密时的密码
{
System.Windows.Forms.MessageBox.Show("密码错误", "信息");
infs.Close();
return;
}
byte[] iv = new byte[8];
for (int i = 0; i < 8; i++)
{
iv[i] = key[i];
}
TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
tdsp.Key = key;//密钥
tdsp.IV = iv;
//infs.Seek(0, SeekOrigin.Begin);
FileStream outfs = new FileStream(outfile, FileMode.Create, FileAccess.Write);
//byte[] d = new byte[infs.Length-16];
//infs.Read(d, 0,d.Length);
//MemoryStream ms = new MemoryStream(d);
CryptoStream cs = new CryptoStream(infs, tdsp.CreateDecryptor(tdsp.Key,tdsp.IV), CryptoStreamMode.Read);
byte bt = new byte();
bt = (byte)cs.ReadByte();
while (infs.Position<infs.Length)
{
outfs.WriteByte(bt);
try
{
bt = (byte)cs.ReadByte();//这里不知道原因,会发生异常。
}
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
break;
}
}
// br.Close();
//cs.Close();
cs.Flush();
cs.Close();
infs.Close();
outfs.Close();
System.Windows.Forms.MessageBox.Show("解密完成!","消息");
}
/// <summary>
/// 比较两个字节数组是否相等,此处用于校验密码
/// </summary>
/// <param name="bt1"></param>
/// <param name="bt2"></param>
/// <returns></returns>
private static bool CompareBytes(byte[] bt1, byte[] bt2)
{
if (bt1.Length != bt2.Length)
{
return false;
}
bool r = true;
for (int i = 0; i < bt1.Length; i++)
{
if (bt1[i] != bt2[i])
{
r = false;
break;
}
}
return r;
}
}
}