KimhillZhang  

下面的两种加密我已经忘记从哪个人的博客里拷下来的,如果有看到一样的请多多包含;

一:DES加密算法:

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.IO;
using System.Security;
using System.Security.Cryptography;
namespace SpaceName
{
    public static class EncryptionExtensions   
    {        
        #region 加密字符串扩展方法
        /// <summary>        
        /// 加密字符串        
        /// </summary>       
        /// <param name="sValue">字符串</param>        
        /// <returns></returns>        
        public static string EncodeString(this string sValue)        
        {
            string strEncoded = string.Empty;
            //string strEncoded = "";           
            //使用内存流和DES算法加密           
            MemoryStream ms = new MemoryStream();
            DES cr = new DESCryptoServiceProvider();
            //选用DES算法加密时要保证定义的字符串按Default编码(是为了可写字符串尽可能长)时不超过8个字节(中文一个汉字占用2个字节)
            byte[] Key = System.Text.Encoding.Default.GetBytes("19812155");
            byte[] IV = System.Text.Encoding.Default.GetBytes("18712515");
            //创建加密流 使用Default编码写入要加密的文本是为了加密结果尽可能小34        
            //将加密后的数据用Base64编码然后存入数据库字符串类型的字段中 原因是加密后的数据是非字符类型的二进制数据强行转换任何常规编码的字符串都会丢失数据35             //最终结果长度为 Math.Ceiling(要加密的文本的Default编码长度/8) * 8 * 1.5 比如 8个字节的原始数据加密后是8个字节 再转为Base64后为12个字节
            CryptoStream ecs = new CryptoStream(ms, cr.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
            StreamWriter sw = new StreamWriter(ecs, System.Text.Encoding.Default);
            sw.Write(sValue);
            sw.Flush();
            ecs.FlushFinalBlock();
            strEncoded = Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
            sw.Close();
            return strEncoded;
         }   
        #endregion
        #region 解密字符串扩展方法
        /// <summary>
        /// 解密字符串
        /// </summary>
        /// <param name="sValue">字符串</param>
        /// <returns></returns>
        public static  string DecodeString(this string sValue)
        {
            string strDecoded = string.Empty;
            //string strDecoded = "";
            byte[] Key = System.Text.Encoding.Default.GetBytes("19812155");
            byte[] IV = System.Text.Encoding.Default.GetBytes("18712515");
            //从Base64编码中还原出要解密的数据      
            DES cr = new DESCryptoServiceProvider();
            MemoryStream ms = new MemoryStream(Convert.FromBase64String(sValue));
            //创建解密数据流 使用与加密时相同的写入流编码格式读出原始文本        
            CryptoStream dcs = new CryptoStream(ms, cr.CreateDecryptor(Key, IV), CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(dcs, System.Text.Encoding.Default);
            strDecoded = sr.ReadToEnd();
            sr.Close();
            return strDecoded;
        }
       
        #endregion
    }
}

 

 

 

二:

using System;
using System.IO;
using System.Security.Cryptography;

using System.Text;

namespace DataCrypto
{
    /// <summary>   
    /// 对称加密算法类   
    /// </summary>   
    public class SymmetricMethod
    {

        private SymmetricAlgorithm mobjCryptoService;
        private string Key;
        /// <summary>   
        /// 对称加密类的构造函数   
        /// </summary>   
        public SymmetricMethod()
        {
            mobjCryptoService = new RijndaelManaged();
            Key = "Guz(%&hj7x89H$yuBI0456FtmaT5&fvHUFCy76*h%(HilJ$lhj!y6&(*jkP87jH7";
        }
        /// <summary>   
        /// 获得密钥   
        /// </summary>   
        /// <returns>密钥</returns>   
        private byte[] GetLegalKey()
        {
            string sTemp = Key;
            mobjCryptoService.GenerateKey();
            byte[] bytTemp = mobjCryptoService.Key;
            int KeyLength = bytTemp.Length;
            if (sTemp.Length > KeyLength)
                sTemp = sTemp.Substring(0, KeyLength);
            else if (sTemp.Length < KeyLength)
                sTemp = sTemp.PadRight(KeyLength, ' ');
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }
        /// <summary>   
        /// 获得初始向量IV   
        /// </summary>   
        /// <returns>初试向量IV</returns>   
        private byte[] GetLegalIV()
        {
            string sTemp = "E4ghj*Ghg7!rNIfb&95GUY86GfghUb#er57HBh(u%g6HJ($jhWk7&!hg4ui%$hjk";
            mobjCryptoService.GenerateIV();
            byte[] bytTemp = mobjCryptoService.IV;
            int IVLength = bytTemp.Length;
            if (sTemp.Length > IVLength)
                sTemp = sTemp.Substring(0, IVLength);
            else if (sTemp.Length < IVLength)
                sTemp = sTemp.PadRight(IVLength, ' ');
            return ASCIIEncoding.ASCII.GetBytes(sTemp);
        }
        /// <summary>   
        /// 加密方法   
        /// </summary>   
        /// <param name="Source">待加密的串</param>   
        /// <returns>经过加密的串</returns>   
        public string Encrypto(string Source)
        {
            byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
            MemoryStream ms = new MemoryStream();
            mobjCryptoService.Key = GetLegalKey();
            mobjCryptoService.IV = GetLegalIV();
            ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
            cs.Write(bytIn, 0, bytIn.Length);
            cs.FlushFinalBlock();
            ms.Close();
            byte[] bytOut = ms.ToArray();
            return Convert.ToBase64String(bytOut);
        }
        /// <summary>   
        /// 解密方法   
        /// </summary>   
        /// <param name="Source">待解密的串</param>   
        /// <returns>经过解密的串</returns>   
        public string Decrypto(string Source)
        {
            byte[] bytIn = Convert.FromBase64String(Source);
            MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
            mobjCryptoService.Key = GetLegalKey();
            mobjCryptoService.IV = GetLegalIV();
            ICryptoTransform encrypto = mobjCryptoService.CreateDecryptor();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
            StreamReader sr = new StreamReader(cs);
            return sr.ReadToEnd();
        }
    }
}

 

posted on 2010-05-15 14:53  KimhillZhang  阅读(738)  评论(0编辑  收藏  举报