真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

C#使用DES和AES實(shí)現(xiàn)加密解密功能示例

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

昭平網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)自2013年創(chuàng)立以來到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)。

using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace MyCryptography
{
  /// 
  /// DES加密解密
  /// 
  public class DES
  {
    /// 
    /// 獲取密鑰
    /// 
    private static string Key
    {
      get { return @"P@+#wG+Z"; }
    }
    /// 
    /// 獲取向量
    /// 
    private static string IV
    {
      get { return @"L%n67}G\Mk@k%:~Y"; }
    }
    /// 
    /// DES加密
    /// 
    /// 明文字符串
    /// 密文
    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;
    }
    /// 
    /// DES解密
    /// 
    /// 密文字符串
    /// 明文
    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;
    }
  }
  /// 
  /// AES加密解密
  /// 
  public class AES
  {
    /// 
    /// 獲取密鑰
    /// 
    private static string Key
    {
      get { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M"; }
    }
    /// 
    /// 獲取向量
    /// 
    private static string IV
    {
      get { return @"L+\~f4,Ir)b$=pkf"; }
    }
    /// 
    /// AES加密
    /// 
    /// 明文字符串
    /// 密文
    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;
    }
    /// 
    /// AES加密
    /// 
    /// 明文字符串
    /// 加密失敗時(shí)是否返回 null,false 返回 String.Empty
    /// 密文
    public static string AESEncrypt(string plainStr, bool returnNull)
    {
      string encrypt = AESEncrypt(plainStr);
      return returnNull ? encrypt : (encrypt == null ? String.Empty : encrypt);
    }
    /// 
    /// AES解密
    /// 
    /// 密文字符串
    /// 明文
    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;
    }
    /// 
    /// AES解密
    /// 
    /// 密文字符串
    /// 解密失敗時(shí)是否返回 null,false 返回 String.Empty
    /// 明文
    public static string AESDecrypt(string encryptStr, bool returnNull)
    {
      string decrypt = AESDecrypt(encryptStr);
      return returnNull ? decrypt : (decrypt == null ? String.Empty : decrypt);
    }
  }
}

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

文字在線加密解密工具(包含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

更多關(guān)于C#相關(guān)內(nèi)容還可查看本站專題:《C#加密與解密算法與技巧總結(jié)》、《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》

希望本文所述對大家C#程序設(shè)計(jì)有所幫助。


當(dāng)前題目:C#使用DES和AES實(shí)現(xiàn)加密解密功能示例
新聞來源:http://weahome.cn/article/ihgjpj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部