zoukankan      html  css  js  c++  java
  • C# DES 加密

    代码
    /// <summary>
    /// DES加密
    /// </summary>
    /// <param name="toEncryptString">要加密的字符串</param>
    /// <param name="keyString">密钥(必须为8位)</param>
    /// <returns>以Base64格式返回的加密字符串</returns>
    public static string DESEncrypt(string toEncryptString, string keyString)
    {
        
    using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
        {
            
    byte[] toEncryptBytes = Encoding.UTF8.GetBytes(toEncryptString);
            des.Key 
    = ASCIIEncoding.ASCII.GetBytes(keyString);
            des.IV 
    = ASCIIEncoding.ASCII.GetBytes(keyString);
            System.IO.MemoryStream ms 
    = new System.IO.MemoryStream();
            
    using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
            {
                cs.Write(toEncryptBytes, 
    0, toEncryptBytes.Length);
                cs.FlushFinalBlock();
                cs.Close();
            }
            
    string encryptedString = Convert.ToBase64String(ms.ToArray());
            ms.Close();
            
    return encryptedString;
        }
    }

    /// <summary>
    /// DES解密
    /// </summary>
    /// <param name="toDecryptString">要解密的Base64字符串</param>
    /// <param name="keyString">密钥(必须为8位)</param>
    /// <returns>已解密的字符串</returns>
    public static string DESDecrypt(string toDecryptString, string keyString)
    {
        
    byte[] toDecryptBytes = Convert.FromBase64String(toDecryptString);
        
    using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
        {
            des.Key 
    = ASCIIEncoding.ASCII.GetBytes(keyString);
            des.IV 
    = ASCIIEncoding.ASCII.GetBytes(keyString);
            System.IO.MemoryStream ms 
    = new System.IO.MemoryStream();
            
    using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
            {
                cs.Write(toDecryptBytes, 
    0, toDecryptBytes.Length);
                cs.FlushFinalBlock();
                cs.Close();
            }
            
    string decryptedString = Encoding.UTF8.GetString(ms.ToArray());
            ms.Close();
            
    return decryptedString;
        }

    要引用 System.Security.Cryptography 命名空间。

  • 相关阅读:
    PHP+ajaxfileupload与jcrop插件结合 完成头像上传
    MySQL字符集设置及字符转换(latin1转utf8)
    sysbench的安装和做性能测试
    MySQL字符集的一个坑
    MySQL执行计划解读
    启动InnoDB引擎的方法
    查询当前使用的默认的存储引擎
    Mysql技术内幕——InnoDB存储引擎
    Oracle Golden Gate原理简介
    在系统启动时,Windows Vista 中、 在 Windows 7 中,Windows Server 2008 中和在 Windows Server 2008 R2 中的 497 天后未关闭 TIME_WAIT 状态的所有 TCP/IP 端口
  • 原文地址:https://www.cnblogs.com/anjou/p/1821799.html
Copyright © 2011-2022 走看看