指引网

当前位置: 主页 > 编程开发 > .NET >

C#实现3DES加密24位密钥

来源:网络 作者:佚名 点击: 时间:2017-11-17 03:53
[摘要] using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Security.Cryptography; namespace ConsoleApplication1 { class Program { public static string Encrypt3DE

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

    namespace ConsoleApplication1
    {
        class Program
        {
            public static string Encrypt3DES(string a_strString, string a_strKey)
            {
                TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

                DES.Key = ASCIIEncoding.ASCII.GetBytes(a_strKey);
                DES.Mode = CipherMode.ECB;

                ICryptoTransform DESEncrypt = DES.CreateEncryptor();

                byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(a_strString);
                return Convert.ToBase64String(DESEncrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
            }

            public static string Decrypt3DES(string a_strString, string a_strKey)
            {
                TripleDESCryptoServiceProvider DES = new TripleDESCryptoServiceProvider();

                DES.Key = ASCIIEncoding.ASCII.GetBytes(a_strKey);
                DES.Mode = CipherMode.ECB;
                DES.Padding = System.Security.Cryptography.PaddingMode.PKCS7;

                ICryptoTransform DESDecrypt = DES.CreateDecryptor();

                string result = "";
                try
                {
                    byte[] Buffer = Convert.FromBase64String(a_strString);
                    result = ASCIIEncoding.ASCII.GetString(DESDecrypt.TransformFinalBlock(Buffer, 0, Buffer.Length));
                }
                catch (Exception e)
                {

                }
                return result;
            }

            static void Main(string[] args)
            {
                Console.WriteLine(Encrypt3DES("999999999", "#s^un2ye31<cn%|aoXpR,+vh");
                Console.ReadLine();
            }
        }
    }

------分隔线----------------------------