91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#使用DES和AES實現加密解密功能示例

發布時間:2020-10-10 03:24:28 來源:腳本之家 閱讀:165 作者:Kevin.Cheung 欄目:編程語言

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

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);
    }
  }
}

PS:關于加密解密感興趣的朋友還可以參考本站在線工具:

文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode

MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password

在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt

在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha

在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode

更多關于C#相關內容還可查看本站專題:《C#加密與解密算法與技巧總結》、《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結》、《C#數據結構與算法教程》、《C#數組操作技巧總結》及《C#面向對象程序設計入門教程》

希望本文所述對大家C#程序設計有所幫助。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

长乐市| 门源| 偏关县| 紫金县| 灯塔市| 荣昌县| 德清县| 永清县| 铁岭市| 双江| 大庆市| 巴东县| 凭祥市| 仙桃市| 永康市| 永嘉县| 枣强县| 睢宁县| 贵南县| 普洱| 新余市| 南涧| 临西县| 长兴县| 宣城市| 日照市| 凤台县| 崇义县| 洪泽县| 安化县| 宝应县| 科技| 揭阳市| 邵武市| 达拉特旗| 图片| 加查县| 施秉县| 安平县| 肥城市| 鹿泉市|