C 3DES encryption and decryption algorithm instance code

  • 2020-05-24 05:59:04
  • OfStack

The C# class is as follows:


using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
    /// <summary>
    ///  Encryption class 
    /// </summary>
    public class EncryptHelper
    {
        
        // structure 1 Symmetric algorithm 
        private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
        #region  Encryption and decryption function 
        /// <summary>
        ///  Encryption of strings 
        /// </summary>
        /// <param name="Value"> The string to encrypt </param>
        /// <param name="sKey"> Key, must be 32 position </param>
        /// <param name="sIV"> Must be a vector 12 A character </param>
        /// <returns> An encrypted string </returns>
        public string EncryptString(string Value, string sKey,string sIV)
        {
            try
            {
                ICryptoTransform ct;
                MemoryStream ms;
                CryptoStream cs;
                byte[] byt;
                mCSP.Key = Convert.FromBase64String(sKey);
                mCSP.IV = Convert.FromBase64String(sIV);
                // Specifies the operation mode for encryption 
                mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
                // Gets or sets the population mode of the encryption algorithm 
                mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);// Creating an encrypted object 
                byt = Encoding.UTF8.GetBytes(Value);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();
                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, " abnormal ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return ("Error in Encrypting " + ex.Message);
            }
        }
        /// <summary>
        ///  Decrypt string 
        /// </summary>
        /// <param name="Value"> An encrypted string </param>
        /// <param name="sKey"> Key, must be 32 position </param>
        /// <param name="sIV"> Must be a vector 12 A character </param>
        /// <returns> The decrypted string </returns>
        public string DecryptString(string Value, string sKey, string sIV)
        {
            try
            {
                ICryptoTransform ct;// Encryption conversion operation 
                MemoryStream ms;// The memory stream 
                CryptoStream cs;// The data stream is connected to the data encryption conversion stream 
                byte[] byt;
                // will 3DES The key is converted to byte
                mCSP.Key = Convert.FromBase64String(sKey);
                // will 3DES I'm going to convert the vector theta to theta byte
                mCSP.IV = Convert.FromBase64String(sIV);
                mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
                mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);// Create a symmetric decryption object 
                byt = Convert.FromBase64String(Value);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
                cs.Write(byt, 0, byt.Length);
                cs.FlushFinalBlock();
                cs.Close();
                return Encoding.UTF8.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message, " abnormal ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return ("Error in Decrypting " + ex.Message);
            }
        }
        #endregion
    }
}

The method is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            EncryptHelper helper = new EncryptHelper();
            // encryption 
            string oldValue = "13800138000";
            // Encrypted result 
            // The key , Must be 32 position 
            string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM5";
            // Must be a vector 12 A character 
            string sIV = "andyliu1234=";
            //print
            string newValue =  helper.EncryptString(oldValue,sKey,sIV);
            Console.WriteLine(" encrypted :"+ newValue);
            // decryption 
            string desValue = helper.DecryptString(newValue,sKey,sIV);
            //
            Console.WriteLine(" decrypted :"+ desValue);
            Console.ReadLine();
        }
    }
}


Related articles: