数据操作DES加密和解密

 #region 数据操作DES加密和解密
    /// <summary>
    /// DES加密字符串
    /// </summary>
    /// <param name="encryptString"> 待加??密??的??字??符??串?? </param>
    /// <param name="encryptKey"> 加??密??密??钥?,要?a求??为a8位? </param>
    /// <returns> 加??密??成??功|返???回?加??密??后??的??字??符??串??,??失???败???返???回?源??串?? </returns>
    public static string EncryptDES( string encryptString, string encryptKey)
    {
        byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        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());
        }
        catch
        {
            return encryptString;
        }
    }
 
 
 
    /// <summary>
    /// DES解a密??字??符??串??
    /// </summary>
    /// <param name="decryptString"> 待?y解a密??的??字??符??串?? </param>
    /// <param name="decryptKey"> 解a密??密??钥?,要?a求??为a8位?,和??加??密??密??钥?相??同?? </param>
    /// <returns> 解a密??成??功|返???回?解a密??后??的??字??符??串??,??失???败???返???源??串?? </returns>
    public static string DecryptDES( string decryptString, string decryptKey)
    {
        byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
 
        try
        {
            byte[] rgbKey = Encoding .UTF8.GetBytes(decryptKey);
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Convert .FromBase64String(decryptString);
            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
        {
            return decryptString;
        }
    }
 
    #endregion
 
 
 
  /// <summary>
    /// DES解a密??字??符??串??
    /// </summary>
    /// <param name="decryptString"> 待解密的字符串 </param>
    /// <param name="decryptKey"> 解密密钥要求为8位和加密密钥相同</param>
    /// <returns> 解a密成功|返回?解a密??后??的??字??符??串??,??失???败???返???源??串?? </returns>
    public static string DecryptDES( string decryptString, string decryptKey)
    {
        byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
 
        try
        {
            byte[] rgbKey = Encoding .UTF8.GetBytes(decryptKey);
            byte[] rgbIV = Keys;
            byte[] inputByteArray = Convert .FromBase64String(decryptString);
            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
        {
            return decryptString;
        }
    }
 
    #endregion
原文地址:https://www.cnblogs.com/BungeeJumping/p/3397300.html