欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C#使用DES和AES实现加密解密功能示例

时间:2022-03-02 09:07:20|栏目:.NET代码|点击:

本文实例讲述了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#程序设计有所帮助。

上一篇:如何给asp.net core写个简单的健康检查

栏    目:.NET代码

下一篇:C#数据库操作小结

本文标题:C#使用DES和AES实现加密解密功能示例

本文地址:http://www.codeinn.net/misctech/195032.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有