代码改变世界

DES文件加密

2007-11-20 11:15  迷路中的路人甲  阅读(1275)  评论(1编辑  收藏  举报
using System;
using System.IO;
using System.Security;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Text;

/// <summary>
/// FileEncryptDecrypt 的摘要说明
/// </summary>

public class FileEncryptDecrypt
{
    
byte[] desIV =0x010x020x030x040x050x060x070x08 };
    
byte[] desKey ={ };
    
public FileEncryptDecrypt()
    
{
        
//
        
// TODO: 在此处添加构造函数逻辑
        
//
    }


    
public void getKey(string keyString)
    
{
        
//根据密码算出密钥
        if (keyString.Length >= 8)
        
{
            desKey 
= new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
                                     (
byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
                                     (
byte)keyString[6],(byte)keyString[7]}
;
        }


        
if (keyString.Length == 6)
        
{
            desKey 
= new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
                                     (
byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
                                     
0x07,0x08}
;
        }

        
if (keyString.Length == 7)
        
{
            desKey 
= new byte[]{(byte)keyString[0],(byte)keyString[1],(byte)keyString[2],
                                     (
byte)keyString[3],(byte)keyString[4],(byte)keyString[5],
                                     (
byte)keyString[6],0x07}
;

        }

    }


    
    
public void DecryptFile(string inName,string outName)
    
{
        
try
        
{

            
//创建文件流分别指向输入和输出文件
            FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
            FileStream fout 
= new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
            fout.SetLength(
0);

            
//每次的中间流.
            byte[] bin = new byte[100];
            
//代表已经解密的流的大小
            int complete = 0;
            
//代表要解密文件总的大小
            long totlen = fin.Length;
            
//每次写入的大小
            int len;

            
//创建DES对象
            DES des = new DESCryptoServiceProvider();
            
//创建解密流
            CryptoStream decStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);


            
//从输入文件中读取流,然后解密到输出文件中
            while (complete < totlen)
            
{
                len 
= fin.Read(bin, 0100);
                decStream.Write(bin, 
0, len);
                complete 
= complete + len;
            }


            
//关闭流        
            decStream.Close();
            fout.Close();
            fin.Close();

            
        }

        
catch (Exception error)
        
{
            
//密码不正确的警告
            throw error;
        }

        
    }


    
public void EncryptFile(string inName, string outName)
    
{
        
//创建文件流分别指向输入和输出文件
        FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
        FileStream fout 
= new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
        fout.SetLength(
0);

        
//每次的中间流.
        byte[] bin = new byte[100];
        
//代表已经加密的流的大小
        int complete = 0;
        
//代表要加密文件总的大小
        long totlen = fin.Length;
        
//每次写入的大小
        int len;

        
//创建DES对象
        DES des = new DESCryptoServiceProvider();
        
//创建加密流    
        CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);

        
//从输入文件中读取流,然后加密到输出文件中
        while (complete < totlen)
        
{
            len 
= fin.Read(bin, 0100);
            encStream.Write(bin, 
0, len);
            complete 
= complete + len;
        }


        
//关闭流
        encStream.Close();
        fout.Close();
        fin.Close();                   

    }

}