加密解密 加密ini文件

using System;
using System.Collections.Generic;
using System.Text;

using System.Security.Cryptography;

namespace Common
{
    /// <summary>
    /// DES加密类
    ///
    /// b)    可逆加密分为:(云盘有例子)
    /// i.    对称加密:用加密的钥匙来解密,比如DES,AES的加解密
    /// ii.    非对称加密:一个钥匙加密,用另一个钥匙解密,这个主要就是RSA比较成熟
    /// </summary>
    public class DESEncrypt
    {

        public DESEncrypt() { }
        #region 加密数据
        /// <summary>
        
/// 加密处理
        
/// </summary>
        
/// <param name="str">要加密的字符串</param>
        
/// <returns></returns>
        public static string Encrypt(string str)
        {
            return Encrypt(str,"bb");
        }
        /// <summary>
        
/// 加密处理
        
/// </summary>
        
/// <param name="str">要加密的字符串</param>
        
/// <param name="key">key值</param>
        
/// <returns></returns>
        public static string Encrypt(string str, string key)
        {
            //定义访问数据加密标准
            DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
            byte[] inputArray;
            inputArray = Encoding.Default.GetBytes(str);
            dsp.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key,"md5").Substring(0,8));
            dsp.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5").Substring(08));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms, dsp.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputArray,0,inputArray.Length);
            cs.FlushFinalBlock();
            StringBuilder sb = new StringBuilder();
            foreach(byte b in ms.ToArray())
            {
                sb.AppendFormat("{0:X2}",b);
            }
            return sb.ToString();
        }
        #endregion

        #region 解密数据
        /// <summary>
        
/// 解密处理
        
/// </summary>
        
/// <param name="str"></param>
        
/// <returns></returns>
        public static string Decrypt(string str)
        {
            return Decrypt(str,"bb");
        }
        /// <summary>
        
/// 解密处理
        
/// </summary>
        
/// <param name="str"></param>
        
/// <param name="key"></param>
        
/// <returns></returns>
        public static string Decrypt(string str, string key)
        {
            DESCryptoServiceProvider dsp = new DESCryptoServiceProvider();
            int len;
            len = str.Length / 2;
            byte[] inputArray=new byte[len];
            int i, j;
            for (i = 0; i < len; i++)
            {
                j = Convert.ToInt32(str.Substring(i*2,2),16);
                inputArray[i] = (byte)j;
            }
            dsp.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key,"md5").Substring(0,8));
            dsp.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5").Substring(08));
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            CryptoStream cs = new CryptoStream(ms,dsp.CreateDecryptor(),CryptoStreamMode.Write);
            cs.Write(inputArray,0,inputArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }
        #endregion
    }
}
posted @ 2012-06-27 14:24  丁晨  阅读(2222)  评论(0)    收藏  举报