C Encryption and Decryption Using DES and AES Example

  • 2021-12-21 04:49:32
  • OfStack

This article describes the example of C # using DES and AES to achieve encryption and decryption functions. Share it for your reference, as follows:


using System;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace MyCryptography
{
  /// <summary>
  /// DES Encryption and decryption 
  /// </summary>
  public class DES
  {
    /// <summary>
    ///  Get the key 
    /// </summary>
    private static string Key
    {
      get { return @"P@+#wG+Z"; }
    }
    /// <summary>
    ///  Acquisition vector 
    /// </summary>
    private static string IV
    {
      get { return @"L%n67}G\Mk@k%:~Y"; }
    }
    /// <summary>
    /// DES Encryption 
    /// </summary>
    /// <param name="plainStr"> Plaintext string </param>
    /// <returns> Ciphertext </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 Decryption 
    /// </summary>
    /// <param name="encryptStr"> Ciphertext string </param>
    /// <returns> Cleartext </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 Encryption and decryption 
  /// </summary>
  public class AES
  {
    /// <summary>
    ///  Get the key 
    /// </summary>
    private static string Key
    {
      get { return @")O[NB]6,YF}+efcaj{+oESb9d8>Z'e9M"; }
    }
    /// <summary>
    ///  Acquisition vector 
    /// </summary>
    private static string IV
    {
      get { return @"L+\~f4,Ir)b$=pkf"; }
    }
    /// <summary>
    /// AES Encryption 
    /// </summary>
    /// <param name="plainStr"> Plaintext string </param>
    /// <returns> Ciphertext </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 Encryption 
    /// </summary>
    /// <param name="plainStr"> Plaintext string </param>
    /// <param name="returnNull"> Whether to return when encryption fails  null , false  Return  String.Empty</param>
    /// <returns> Ciphertext </returns>
    public static string AESEncrypt(string plainStr, bool returnNull)
    {
      string encrypt = AESEncrypt(plainStr);
      return returnNull ? encrypt : (encrypt == null ? String.Empty : encrypt);
    }
    /// <summary>
    /// AES Decryption 
    /// </summary>
    /// <param name="encryptStr"> Ciphertext string </param>
    /// <returns> Cleartext </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 Decryption 
    /// </summary>
    /// <param name="encryptStr"> Ciphertext string </param>
    /// <param name="returnNull"> Whether to return when decryption fails  null , false  Return  String.Empty</param>
    /// <returns> Cleartext </returns>
    public static string AESDecrypt(string encryptStr, bool returnNull)
    {
      string decrypt = AESDecrypt(encryptStr);
      return returnNull ? decrypt : (decrypt == null ? String.Empty : decrypt);
    }
  }
}

PS: Friends who are interested in encryption and decryption can also refer to the online tools of this site:

Text online encryption and decryption tools (including AES, DES, RC4, etc.):
http://tools.ofstack.com/password/txt_encode

MD5 Online Encryption Tool:
http://tools.ofstack.com/password/CreateMD5Password

Online hash/hash algorithm encryption tool:
http://tools.ofstack.com/password/hash_encrypt

Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 Encryption Tool:
http://tools.ofstack.com/password/hash_md5_sha

Online sha1/sha224/sha256/sha384/sha512 Encryption Tool:
http://tools.ofstack.com/password/sha_encode

For more information about C #, you can also see the topics on this site: "Summary of C # Encryption and Decryption Algorithms and Skills", "Summary of C # Form Operation Skills", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Array Operation Skills Summary" and "C # Object-Oriented Programming Introduction Tutorial"

I hope this article is helpful to everyone's C # programming.


Related articles: