首页 > 其他 > 详细

DES 加密解密

时间:2014-03-21 03:21:50      阅读:532      评论:0      收藏:0      [点我收藏+]

【概念】 

数据加密算法(Data Encryption Algorithm,DEA)是一种对称加密算法,很可能是使用最广泛的密钥系统,特别是在保护金融数据的安全中,最初开发的DEA是嵌入硬件中的。通常,自动取款机(Automated Teller Machine,ATM)都使用DEA。它出自IBM的研究工作【from baidu】

 

【部分代码实现】

bubuko.com,布布扣
#region DES加密/解密字符串
    //默认密钥向量
    private static byte[] Keys = { 0x11, 0x22, 0x33, 0x44, 0x55, 0xAA, 0xBB, 0xCC };

    public static string EncryptDES(string encryptString)
    {
        string encryptKey = StringUtils.ConvertToStr(GetConfig("CryptDESKey"), "abcd");//GetConfig方法为从配置文件读区数据
        return EncryptDES(encryptString, encryptKey);
    }
    /// <summary>
    /// DES加密字符串
    /// </summary>
    /// <param name="encryptString">待加密的字符串</param>
    /// <param name="encryptKey">加密密钥,要求为8位</param>
    /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
    public static string EncryptDES(string encryptString, string encryptKey)
    {
        try
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
            DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray()).Replace("+", "%20");
        }
        catch (Exception ex)
        {
            return string.Empty;
        }
    }
    public static string DecryptDES(string decryptString)
    {
        string decryptKey = StringUtils.ConvertToStr(GetConfig("CryptDESKey"), "abcd");
        return DecryptDES(decryptString, decryptKey);
    }
    /// <summary>
    /// DES解密字符串
    /// </summary>
    /// <param name="decryptString">待解密的字符串</param>
    /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
    /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
    public static string DecryptDES(string decryptString, string decryptKey)
    {
        try
        {
            byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Convert.FromBase64String(decryptString.Replace( , +));
            DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }
        catch (Exception ex)
        {
            WrLogger logger = new WrLogger();
            logger.WriteException("App_Code.WebUtils.DecryptDES(string, string) Error, DecryptString:" + decryptString, ex);
            return string.Empty;
        }
    }

    #endregion
bubuko.com,布布扣

 注:此日志仅留作以后查询。

DES 加密解密,布布扣,bubuko.com

DES 加密解密

原文:http://www.cnblogs.com/fo0ol/p/3614296.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!