TripleDES加密类

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;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

强_哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值