C#使用DES和AES實現(xiàn)加密解密功能示例-創(chuàng)新互聯(lián)

本文實例講述了C#使用DES和AES實現(xiàn)加密解密功能。分享給大家供大家參考,具體如下:

為聶拉木等地區(qū)用戶提供了全套網(wǎng)頁設計制作服務,及聶拉木網(wǎng)站建設行業(yè)解決方案。主營業(yè)務為網(wǎng)站制作、成都網(wǎng)站設計、聶拉木網(wǎng)站設計,以傳統(tǒng)方式定制建設網(wǎng)站,并提供域名空間備案等一條龍服務,秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace MyCryptography
{
  /// <summary>
  /// DES加密解密
  /// </summary>
  public class DES
  {
    /// <summary>
    /// 獲取密鑰
    /// </summary>
    private static string Key
    {
      get { return @"P@+#wG+Z"; }
    }
    /// <summary>
    /// 獲取向量
    /// </summary>
    private static string IV
    {
      get { return @"L%n67}G\Mk@k%:~Y"; }
    }
    /// <summary>
    /// DES加密
    /// </summary>
    /// <param name="plainStr">明文字符串</param>
    /// <returns>密文</returns>
    public static string DESEncrypt(string plainStr)
    {
      byte[] bKey = Encoding.UTF8.GetBytes(Key);
      byte[] bIV = Encoding.UTF8.GetBytes(IV);
      byte[] byteArray = Encoding.UTF8.GetBytes(plainStr);
      string encrypt = null;
      DESCryptoServiceProvider des = new DESCryptoServiceProvider();
      try
      {
        using (MemoryStream mStream = new MemoryStream())
        {
          using (CryptoStream cStream = new CryptoStream(mStream, des.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
          {
            cStream.Write(byteArray, 0, byteArray.Length);
            cStream.FlushFinalBlock();
            encrypt = Convert.ToBase64String(mStream.ToArray());
          }
        }
      }
      catch { }
      des.Clear();
      return encrypt;
    }
    /// <summary>
    /// DES解密
    /// </summary>
    /// <param name="encryptStr">密文字符串</param>
    /// <returns>明文</returns>
    public static string DESDecrypt(string encryptStr)
    {
      byte[] bKey = Encoding.UTF8.GetBytes(Key);
      byte[] bIV = Encoding.UTF8.GetBytes(IV);
      byte[] byteArray = Convert.FromBase64String(encryptStr);
      string decrypt = null;
      DESCryptoServiceProvider des = new DESCryptoServiceProvider();
      try
      {
        using (MemoryStream mStream = new MemoryStream())
        {
          using (CryptoStream cStream = new CryptoStream(mStream, des.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
          {
            cStream.Write(byteArray, 0, byteArray.Length);
            cStream.FlushFinalBlock();
            decrypt = Encoding.UTF8.GetString(mStream.ToArray());
          }
        }
      }
      catch { }
      des.Clear();
      return decrypt;
    }
  }
  /// <summary>
  /// AES加密解密
  /// </summary>
  public class AES
  {
    /// <summary>
    /// 獲取密鑰
    /// </summary>
    private static string Key
    {
      get { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M"; }
    }
    /// <summary>
    /// 獲取向量
    /// </summary>
    private static string IV
    {
      get { return @"L+\~f4,Ir)b$=pkf"; }
    }
    /// <summary>
    /// AES加密
    /// </summary>
    /// <param name="plainStr">明文字符串</param>
    /// <returns>密文</returns>
    public static string AESEncrypt(string plainStr)
    {
      byte[] bKey = Encoding.UTF8.GetBytes(Key);
      byte[] bIV = Encoding.UTF8.GetBytes(IV);
      byte[] byteArray = Encoding.UTF8.GetBytes(plainStr);
      string encrypt = null;
      Rijndael aes = Rijndael.Create();
      try
      {
        using (MemoryStream mStream = new MemoryStream())
        {
          using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateEncryptor(bKey, bIV), CryptoStreamMode.Write))
          {
            cStream.Write(byteArray, 0, byteArray.Length);
            cStream.FlushFinalBlock();
            encrypt = Convert.ToBase64String(mStream.ToArray());
          }
        }
      }
      catch { }
      aes.Clear();
      return encrypt;
    }
    /// <summary>
    /// AES加密
    /// </summary>
    /// <param name="plainStr">明文字符串</param>
    /// <param name="returnNull">加密失敗時是否返回 null,false 返回 String.Empty</param>
    /// <returns>密文</returns>
    public static string AESEncrypt(string plainStr, bool returnNull)
    {
      string encrypt = AESEncrypt(plainStr);
      return returnNull ? encrypt : (encrypt == null ? String.Empty : encrypt);
    }
    /// <summary>
    /// AES解密
    /// </summary>
    /// <param name="encryptStr">密文字符串</param>
    /// <returns>明文</returns>
    public static string AESDecrypt(string encryptStr)
    {
      byte[] bKey = Encoding.UTF8.GetBytes(Key);
      byte[] bIV = Encoding.UTF8.GetBytes(IV);
      byte[] byteArray = Convert.FromBase64String(encryptStr);
      string decrypt = null;
      Rijndael aes = Rijndael.Create();
      try
      {
        using (MemoryStream mStream = new MemoryStream())
        {
          using (CryptoStream cStream = new CryptoStream(mStream, aes.CreateDecryptor(bKey, bIV), CryptoStreamMode.Write))
          {
            cStream.Write(byteArray, 0, byteArray.Length);
            cStream.FlushFinalBlock();
            decrypt = Encoding.UTF8.GetString(mStream.ToArray());
          }
        }
      }
      catch { }
      aes.Clear();
      return decrypt;
    }
    /// <summary>
    /// AES解密
    /// </summary>
    /// <param name="encryptStr">密文字符串</param>
    /// <param name="returnNull">解密失敗時是否返回 null,false 返回 String.Empty</param>
    /// <returns>明文</returns>
    public static string AESDecrypt(string encryptStr, bool returnNull)
    {
      string decrypt = AESDecrypt(encryptStr);
      return returnNull ? decrypt : (decrypt == null ? String.Empty : decrypt);
    }
  }
}

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站www.muchs.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

本文標題:C#使用DES和AES實現(xiàn)加密解密功能示例-創(chuàng)新互聯(lián)
轉載注明:http://www.muchs.cn/article24/coieje.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供響應式網(wǎng)站、標簽優(yōu)化、ChatGPT外貿(mào)建站、小程序開發(fā)、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

h5響應式網(wǎng)站建設