加密,解密

using System;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Globalization;


/// <summary> 
/// 对指定内容进行DES加密和解密处理     
/// </summary>     
public sealed class DESEncrypt
{
    /// <summary>     
    /// 对字符串进行DES加密  
    /// </summary>     
    /// <param name="sourcestring">待加密的字符串</param>  
    /// <param name="key">必须长度等于8的字符串</param>
    /// <param name="iv">必须长度大于等于8的字符串</param>
    /// <returns>加密后的BASE64编码的字符串</returns>     
    internal static string Encrypt(string src, string key, string iv)
    {
        byte[] byteKey = Encoding.Default.GetBytes(key);
        byte[] byteIv = Encoding.Default.GetBytes(iv);
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        using (MemoryStream ms = new MemoryStream())
        {

            try
            {
                byte[] data = Encoding.Default.GetBytes(src);
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byteKey, byteIv), CryptoStreamMode.Write))
                {
                    cs.Write(data, 0, data.Length);
                    cs.FlushFinalBlock();
                }
                return Convert.ToBase64String(ms.ToArray());
            }
            catch
            {
                throw;
            }
        }
    }

    /// <summary>  
    /// 对DES加密后的字符串进行解密     
    /// </summary>     
    /// <param name="encryptedString">待解密的字符串</param>     
    /// <param name="key">同加密时所采用的key</param>
    /// <param name="iv">同加密时所采用的iv</param>
    /// <returns>解密后的字符串</returns>     
    internal static string Decrypt(string src, string key, string iv)
    {
        byte[] byteKey = Encoding.Default.GetBytes(key);
        byte[] byteIv = Encoding.Default.GetBytes(iv);
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();

        using (MemoryStream ms = new MemoryStream())
        {
            try
            {
                byte[] data = Convert.FromBase64String(src);
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byteKey, byteIv), CryptoStreamMode.Write))
                {
                    cs.Write(data, 0, data.Length);
                    cs.FlushFinalBlock();
                }
                return Encoding.Default.GetString(ms.ToArray());
            }
            catch
            {
                throw;
            }
        }
    }

    /// <summary>  
    /// 对文件内容进行DES加密     
    /// </summary>     
    /// <param name="sourceFile">待加密的文件绝对路径</param>     
    /// <param name="destFile">加密后的文件保存的绝对路径</param>   
    /// <param name="key">必须长度等于8的字符串</param>
    /// <param name="iv">必须长度大于等于8的字符串</param>
    internal static void EncryptFile(string sourceFile, string destFile, string key, string iv)
    {
        if (!File.Exists(sourceFile))
            throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);

        byte[] byteKey = Encoding.Default.GetBytes(key);
        byte[] byteIV = Encoding.Default.GetBytes(iv);
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] byteFile = File.ReadAllBytes(sourceFile);

        using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
        {
            try
            {
                using (CryptoStream cs = new CryptoStream(fs, des.CreateEncryptor(byteKey, byteIV), CryptoStreamMode.Write))
                {
                    cs.Write(byteFile, 0, byteFile.Length);
                    cs.FlushFinalBlock();
                }
            }
            catch
            {
                throw;
            }

            finally
            {
                fs.Close();
            }
        }
    }

    /// <summary>     
    /// Encrypts the file.     
    /// 对文件内容进行DES加密,加密后覆盖掉原来的文件     
    /// </summary>     
    /// <param name="sourceFile">The source file.待加密的文件的绝对路径</param>     
    /// <param name="key">必须长度等于8的字符串</param>
    /// <param name="iv">必须长度大于等于8的字符串</param>
    internal static void EncryptFile(string sourceFile, string key, string iv)
    {
        EncryptFile(sourceFile, sourceFile, key, iv);
    }

    /// <summary> 
    /// 对文件内容进行DES解密     
    /// </summary>     
    /// <param name="sourceFile">待解密的文件绝对路径</param>     
    /// <param name="destFile">解密后的文件保存的绝对路径</param>
    /// <param name="key">同加密时所采用的key</param>
    /// <param name="iv">同加密时所采用的iv</param>
    internal static void DecryptFile(string sourceFile, string destFile, string key, string iv)
    {
        if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);
        byte[] byteKey = Encoding.Default.GetBytes(key);
        byte[] byteIV = Encoding.Default.GetBytes(iv);
        DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        byte[] byteFile = File.ReadAllBytes(sourceFile);

        using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
        {
            try
            {
                using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(byteKey, byteIV), CryptoStreamMode.Write))
                {
                    cs.Write(byteFile, 0, byteFile.Length);
                    cs.FlushFinalBlock();
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                fs.Close();
            }
        }
    }

    /// <summary> 
    /// 对文件内容进行DES解密,加密后覆盖掉原来的文件     
    /// </summary>     
    /// <param name="sourceFile">待解密的文件的绝对路径</param>     
    /// <param name="key">同加密时所采用的key</param>
    /// <param name="iv">同加密时所采用的iv</param>
    internal static void DecryptFile(string sourceFile, string key, string iv)
    {
        DecryptFile(sourceFile, sourceFile, key, iv);
    }
}
posted @ 2011-08-25 09:55  凌鸢  阅读(424)  评论(1编辑  收藏  举报