C strings are encrypted and decrypted using a key

  • 2021-11-01 04:24:42
  • OfStack

The first one to share is the encryption and decryption code of C # string using key, which is as follows


public class DesEncrypt
  {
    /// <summary>
    ///  Algorithm offset 
    /// </summary>
    const string m_IV = "12345678";
 
    /// <summary>
    ///  Functional description : Based on the key entered 8 Bit key 
    ///  Author :  Love to template net  2gei.cn
    ///  Creation date :2015-07-20 17:25:26
    /// </summary>
    /// <param name="strkey">strkey</param>
    /// <returns>8 Bit key </returns>
    private static string GetKey(string strkey)
    {
      if (string.IsNullOrEmpty(strkey))
      {
        strkey = "InfoColl";
      }
      if (strkey.Length % 8 == 0)
      {
        return strkey;
      }
      else
      {
        return GetKey(strkey + "0");
      }
    }
 
    /// <summary>
    ///  Functional description : Encrypted string 
    ///  Author :  Love to template net  2gei.cn
    ///  Creation date :2015-07-20 17:18:31
    ///  Task number :
    /// </summary>
    /// <param name="strSourceString"> Original string </param>
    /// <param name="strKey"> Key </param>
    /// <returns> Encrypted string </returns>
    public static string Encrypt(string strSourceString, string strKey)
    {
      strKey = GetKey(strKey);
      byte[] btKey = Encoding.UTF8.GetBytes(strKey);
 
      byte[] btIV = Encoding.UTF8.GetBytes(m_IV);
 
      DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 
      using (MemoryStream ms = new MemoryStream())
      {
        try
        {
          byte[] inData = Encoding.UTF8.GetBytes(strSourceString);
          using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
          {
            cs.Write(inData, 0, inData.Length);
 
            cs.FlushFinalBlock();
          }
 
          return Convert.ToBase64String(ms.ToArray());
        }
        catch
        {
          return strSourceString;
        }
      }
    }
 
    /// <summary>
    ///  Functional description : Decryption string 
    ///  Author :  Love to template net  2gei.cn
    ///  Creation date :2015-07-20 17:18:49
    ///  Task number :
    /// </summary>
    /// <param name="strEncryptedString"> Original string </param>
    /// <param name="strKey"> Key </param>
    /// <returns> Decrypted string </returns>
    public static string Decrypt(string strEncryptedString, string strKey)
    {
      strKey = GetKey(strKey);
      byte[] btKey = Encoding.UTF8.GetBytes(strKey);
 
      byte[] btIV = Encoding.UTF8.GetBytes(m_IV);
 
      DESCryptoServiceProvider des = new DESCryptoServiceProvider();
 
      using (MemoryStream ms = new MemoryStream())
      {
        try
        {
          byte[] inData = Convert.FromBase64String(strEncryptedString);
          using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
          {
            cs.Write(inData, 0, inData.Length);
 
            cs.FlushFinalBlock();
          }
 
          return Encoding.UTF8.GetString(ms.ToArray());
        }
        catch
        {
          return strEncryptedString;
        }
      }
    }
  }

C # String Encryption and Decryption


using System.Security.Cryptography;
using System.IO;
// Default key vector 
    private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

    /// <summary>
    /// DES Encrypted string 
    /// </summary>
    /// <param name="encryptString"> String to be encrypted </param>
    /// <param name="encryptKey"> Encryption key , Requirements are 8 Bit </param>
    /// <returns> Encryption successfully returns the encrypted string, and fails to return the source string  </returns>
    public static string EncryptDES(string encryptString, string encryptKey)
    {
      try
      {
        byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));// Convert to bytes 
        byte[] rgbIV = Keys;
        byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
        DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();// Instantiated data encryption standard 
        MemoryStream mStream = new MemoryStream();// Instantiate memory flow 
        // Linking data streams to cryptographic transformed streams 
        CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
        cStream.Write(inputByteArray, 0, inputByteArray.Length);
        cStream.FlushFinalBlock();
        return Convert.ToBase64String(mStream.ToArray());
      }
      catch
      {
        return encryptString;
      }
    }

    /// <summary>
    /// DES Decryption string 
    /// </summary>
    /// <param name="decryptString"> String to be decrypted </param>
    /// <param name="decryptKey"> Decryption key , Requirements are 8 Bit , Same as the encryption key </param>
    /// <returns> Successful decryption returns the decrypted string, failed to return to the source string </returns>
    public static string DecryptDES(string decryptString, string decryptKey)
    {
      try
      {
        byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
        byte[] rgbIV = Keys;
        byte[] inputByteArray = Convert.FromBase64String(decryptString);
        DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
        MemoryStream mStream = new MemoryStream();
        CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
        cStream.Write(inputByteArray, 0, inputByteArray.Length);
        cStream.FlushFinalBlock();
        return Encoding.UTF8.GetString(mStream.ToArray());
      }
      catch
      {
        return decryptString;
      }
    }



      string EncryptStr = EncryptDESString.EncryptDES("aaaaaaaaaa", "ssssssss"); // Returns an encrypted string 
      string DecryptStr = EncryptDESString.DecryptDES(EncryptStr, "ssssssss");// Decryption string 


Related articles: