SoFunction
Updated on 2025-03-07

C# Example of encryption and decryption using DES and AES

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

using System;
using ;
using ;
using ;
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>
    /// Get 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.(Key);
      byte[] bIV = Encoding.(IV);
      byte[] byteArray = Encoding.(plainStr);
      string encrypt = null;
      DESCryptoServiceProvider des = new DESCryptoServiceProvider();
      try
      {
        using (MemoryStream mStream = new MemoryStream())
        {
          using (CryptoStream cStream = new CryptoStream(mStream, (bKey, bIV), ))
          {
            (byteArray, 0, );
            ();
            encrypt = Convert.ToBase64String(());
          }
        }
      }
      catch { }
      ();
      return encrypt;
    }
    /// &lt;summary&gt;
    /// DES decryption    /// &lt;/summary&gt;
    /// <param name="encryptStr">Secret text string</param>    /// <returns>Plain text</returns>    public static string DESDecrypt(string encryptStr)
    {
      byte[] bKey = Encoding.(Key);
      byte[] bIV = Encoding.(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, (bKey, bIV), ))
          {
            (byteArray, 0, );
            ();
            decrypt = Encoding.(());
          }
        }
      }
      catch { }
      ();
      return decrypt;
    }
  }
  /// &lt;summary&gt;
  /// AES encryption and decryption  /// &lt;/summary&gt;
  public class AES
  {
    /// &lt;summary&gt;
    /// Get the key    /// &lt;/summary&gt;
    private static string Key
    {
      get { return @")O[NB]6,YF}+efcaj{+oESb9d8&gt;Z'e9M"; }
    }
    /// &lt;summary&gt;
    /// Get vector    /// &lt;/summary&gt;
    private static string IV
    {
      get { return @"L+\~f4,Ir)b$=pkf"; }
    }
    /// &lt;summary&gt;
    /// AES encryption    /// &lt;/summary&gt;
    /// <param name="plainStr">Plaintext string</param>    /// <returns>ciphertext</returns>    public static string AESEncrypt(string plainStr)
    {
      byte[] bKey = Encoding.(Key);
      byte[] bIV = Encoding.(IV);
      byte[] byteArray = Encoding.(plainStr);
      string encrypt = null;
      Rijndael aes = ();
      try
      {
        using (MemoryStream mStream = new MemoryStream())
        {
          using (CryptoStream cStream = new CryptoStream(mStream, (bKey, bIV), ))
          {
            (byteArray, 0, );
            ();
            encrypt = Convert.ToBase64String(());
          }
        }
      }
      catch { }
      ();
      return encrypt;
    }
    /// &lt;summary&gt;
    /// AES encryption    /// &lt;/summary&gt;
    /// <param name="plainStr">Plaintext string</param>    /// <param name="returnNull">Whether to return null when encryption fails, false return </param>    /// <returns>ciphertext</returns>    public static string AESEncrypt(string plainStr, bool returnNull)
    {
      string encrypt = AESEncrypt(plainStr);
      return returnNull ? encrypt : (encrypt == null ?  : encrypt);
    }
    /// &lt;summary&gt;
    /// AES decryption    /// &lt;/summary&gt;
    /// <param name="encryptStr">Secret text string</param>    /// <returns>Plain text</returns>    public static string AESDecrypt(string encryptStr)
    {
      byte[] bKey = Encoding.(Key);
      byte[] bIV = Encoding.(IV);
      byte[] byteArray = Convert.FromBase64String(encryptStr);
      string decrypt = null;
      Rijndael aes = ();
      try
      {
        using (MemoryStream mStream = new MemoryStream())
        {
          using (CryptoStream cStream = new CryptoStream(mStream, (bKey, bIV), ))
          {
            (byteArray, 0, );
            ();
            decrypt = Encoding.(());
          }
        }
      }
      catch { }
      ();
      return decrypt;
    }
    /// &lt;summary&gt;
    /// AES decryption    /// &lt;/summary&gt;
    /// <param name="encryptStr">Secret text string</param>    /// <param name="returnNull">Whether to return null when decryption fails, false return </param>    /// <returns>Plain text</returns>    public static string AESDecrypt(string encryptStr, bool returnNull)
    {
      string decrypt = AESDecrypt(encryptStr);
      return returnNull ? decrypt : (decrypt == null ?  : 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./password/txt_encode

MD5 online encryption tool:
http://tools./password/CreateMD5Password

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

Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tool:
http://tools./password/hash_md5_sha

Online sha1/sha224/sha256/sha384/sha512 encryption tool:
http://tools./password/sha_encode

For more information about C#, you can also view the special topic of this site: "Summary of C# Encryption and Decryption Algorithm and Skills》、《Summary of C# form operation skills》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial

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