C#3 DES暗号解読アルゴリズムインスタンスコード

5231 ワード

C#の3 DES暗号解読アルゴリズムインスタンスコード
C#クラスは次のとおりです.

     using System; 
    
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace ConsoleApplication1
{
    ///


    ///
    ///

    public class EncryptHelper
    { /// www.jbxue.com       

        //
        private SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();

        #region

        ///


        ///
        ///

        ///
        /// , 32
        /// , 12
        ///
        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);
                //
                mCSP.Mode = System.Security.Cryptography.CipherMode.ECB;
                //
                mCSP.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
                ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);//
                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, " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return ("Error in Encrypting " + ex.Message);
            }
        }

        ///


        ///
        ///

        ///
        /// , 32
        /// , 12
        ///
        public string DecryptString(string Value, string sKey, string sIV)
        {
            try
            {
                ICryptoTransform ct;//
                MemoryStream ms;//
                CryptoStream cs;//
                byte[] byt;
                // 3DES byte
                mCSP.Key = Convert.FromBase64String(sKey);
                // 3DES 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);//
                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, " ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return ("Error in Decrypting " + ex.Message);
            }
        }

        #endregion
    }
}


呼び出し方法は次のとおりです.

     using System; 
    
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    { /// www.jbxue.com
        static void Main(string[] args)
        {
            EncryptHelper helper = new EncryptHelper();

            //
            string oldValue = "13800138000";
            //
            // , 32
            string sKey = "qJzGEh6hESZDVJeCnFPGuxzaiB7NLQM5";
            // , 12
            string sIV = "andyliu1234=";
            //print
            string newValue =  helper.EncryptString(oldValue,sKey,sIV);
            Console.WriteLine(" :"+ newValue);
            //
            string desValue = helper.DecryptString(newValue,sKey,sIV);
            //
            Console.WriteLine(" :"+ desValue);
            Console.ReadLine();

        }
    }
}