C# DES加密解密

2020-12-18 09:53

阅读:665

标签:bst   static   mode   相同   密钥   orm   gb2312   要求   str   

干净的DES加密

/// 
/// DES加密字符串
/// 
/// 待加密的字符串
/// 加密密钥,要求为8位
/// 加密成功返回加密后的字符串,失败返回源串
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 = iv;
         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;
     }
}
/// 
/// DES解密字符串
/// 
/// 待解密的字符串
/// 解密密钥,要求为8位,和加密密钥相同
/// 解密成功返回解密后的字符串,失败返源串
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 

 

使用BASE64数组进行DES加密和解密

///   
        /// DES加密  
        ///   
        ///   
        ///   
        ///   
        public string DESEncrypt(string pToEncrypt, string sKey,string encode="UTF-8")
        {
            byte[] keyBytes = Encoding.UTF8.GetBytes(sKey.Substring(0, 8));
            byte[] keyIV = iv;
            byte[] inputByteArray = Encoding.GetEncoding(encode).GetBytes(pToEncrypt);
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }
        ///   
        /// DES解密  
        ///   
        ///   
        ///   
        ///   
        public string DESDecrypt(string pToDecrypt, string sKey,string encode= "GB2312")
        {
            try
            {
                byte[] keyBytes = Encoding.UTF8.GetBytes(sKey);
                byte[] keyIV = iv;
                byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.GetEncoding(encode).GetString(mStream.ToArray());
            }
            catch (Exception ex)
            {
                WriteLog("解密出错:" + ex.Message);
                return "error";
            }
        }

  

 

C# DES加密解密

标签:bst   static   mode   相同   密钥   orm   gb2312   要求   str   

原文地址:https://www.cnblogs.com/wanyu/p/12959215.html


评论


亲,登录后才可以留言!