• C# DES加密、解密


    /// <summary>
    /// DES加密字符串
    /// </summary>
    /// <param name="pToEncrypt">待加密的字符串</param>
    /// <param name="sKey">加密密钥,要求为8位</param>
    /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
    public static string DesEncrypt(string pToEncrypt, string sKey)
    {
         StringBuilder ret = new StringBuilder();
         try
         {
             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
             byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
             des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
             des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
             MemoryStream ms = new MemoryStream();
             CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(),CryptoStreamMode.Write);
             cs.Write(inputByteArray, 0, inputByteArray.Length);
             cs.FlushFinalBlock();
             foreach (byte b in ms.ToArray())
             {
                 ret.AppendFormat("{0:X2}", b);
             }
             return ret.ToString();
         }
         catch
         {
             return pToEncrypt;
         }
    }
    /// <summary>
    /// DES解密字符串
    /// </summary>
    /// <param name="pToDecrypt">待解密的字符串</param>
    /// <param name="sKey">解密密钥,要求为8位,和加密密钥相同</param>
    /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
    public static string DesDecrypt(string pToDecrypt, string sKey)
    {
         MemoryStream ms = new MemoryStream();
         try
         {
             DESCryptoServiceProvider des = new DESCryptoServiceProvider();
             byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
             for (int x = 0; x < pToDecrypt.Length / 2; x++)
             {
                 int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                 inputByteArray[x] = (byte)i;
             }
             des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
             des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
             CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
             cs.Write(inputByteArray, 0, inputByteArray.Length);
             cs.FlushFinalBlock();
             return System.Text.Encoding.Default.GetString(ms.ToArray());
         }
         catch
         {
             return pToDecrypt;
         }
    }
  • 相关阅读:
    Flex 学习笔记 Remoting中的作用域(转)
    Flex 学习笔记 动态设置itemRenderer
    发现一个很好玩的网站个人漫画
    AjaxLoad动态生成加载图标的网站
    如何提高大字符串(是从文本文件读取出来的,有2M多)在网页中的显示速度
    CSS纵向居中问题
    用javascript进行xsl转换
    实现鼠标感应效果
    随笔写写
    execCommand指令集
  • 原文地址:https://www.cnblogs.com/flamegreat/p/12195795.html
Copyright © 2020-2023  润新知