• 常用加密解密类(含3des)


    using System;
    using System.Text;
    using System.Globalization;
    using System.Security.Cryptography;
    using CAPICOM;
    using System.Runtime.InteropServices;
    using System.IO;
    using System.Web.Security;

    namespace Dongle
    {
        /// <summary>
        /// Encoder 的摘要说明。
        /// </summary>
        public class Encoder
        {
            private static string m_defaultKey = "~!@#$%^1*()_+|[ SDGOW.CN ]|+_)(*1^%$#@!~";

            /// <summary>
            /// 3DES解密,使用缺省密钥
            /// </summary>
            /// <param name="original">明文</param>
            /// <returns>密文</returns>
            public static string CapicomEncrypt(string original)
            {
                return CapicomEncrypt(original, m_defaultKey);
            }

            /// <summary>
            /// 3DES解密,使用缺省密钥
            /// </summary>
            /// <param name="original">密文</param>
            /// <returns>明文</returns>
            public static string CapicomDecrypt(string original)
            {
                try
                {
                    return CapicomDecrypt(original, m_defaultKey);
                }
                catch
                {
                    return "";
                }
            }

            /// <summary>
            /// 3DES加密,使用给定密钥
            /// </summary>
            /// <param name="original"></param>
            /// <param name="key">密钥</param>
            /// <returns></returns>
            public static string CapicomEncrypt(string original, string key)
            {
                CAPICOM.EncryptedData ed = new EncryptedData();
                ed.Content = original;
                ed.Algorithm.Name = CAPICOM_ENCRYPTION_ALGORITHM.CAPICOM_ENCRYPTION_ALGORITHM_3DES;
                ed.Algorithm.KeyLength = CAPICOM_ENCRYPTION_KEY_LENGTH.CAPICOM_ENCRYPTION_KEY_LENGTH_128_BITS;
                ed.SetSecret(key, CAPICOM_SECRET_TYPE.CAPICOM_SECRET_PASSWORD);
                return ed.Encrypt(CAPICOM_ENCODING_TYPE.CAPICOM_ENCODE_BASE64);
            }

            /// <summary>
            /// 3DES解密,使用给定密钥
            /// </summary>
            /// <param name="encrypted"></param>
            /// <param name="key">密钥</param>
            /// <returns></returns>
            public static string CapicomDecrypt(string encrypted, string key)
            {
                CAPICOM.EncryptedData ed = new EncryptedData();

                ed.Algorithm.Name = CAPICOM_ENCRYPTION_ALGORITHM.CAPICOM_ENCRYPTION_ALGORITHM_3DES;
                ed.Algorithm.KeyLength = CAPICOM_ENCRYPTION_KEY_LENGTH.CAPICOM_ENCRYPTION_KEY_LENGTH_128_BITS;
                ed.SetSecret(key, CAPICOM_SECRET_TYPE.CAPICOM_SECRET_PASSWORD);
                ed.Decrypt(encrypted);
                return ed.Content;
            }


            /// <summary>
            /// DES加密,使用缺省密钥
            /// </summary>
            /// <param name="original">明文</param>
            /// <returns>密文</returns>
            public static string Encrypt(string original)
            {
                return Encrypt(original, m_defaultKey);
            }

            /// <summary>
            /// DES解密,使用缺省密钥
            /// </summary>
            /// <param name="encrypted">密文</param>
            /// <returns>明文</returns>
            public static string Decrypt(string encrypted)
            {
                try
                {
                    return Decrypt(encrypted, m_defaultKey, System.Text.Encoding.Default);
                }
                catch
                {
                    return "";
                }
            }
            /// <summary>
            ///  DES解密,使用给定密钥
            /// </summary>
            /// <param name="encrypted">密文</param>
            /// <param name="key">密钥</param>
            /// <returns>明文</returns>
            public static string Decrypt(string encrypted, string key)
            {
                return Decrypt(encrypted, key, System.Text.Encoding.Default);
            }
            /// <summary>
            /// 使用缺省密钥解密,返回指定编码方式明文
            /// </summary>
            /// <param name="encrypted">密文</param>
            /// <param name="encoding">编码方式</param>
            /// <returns>明文</returns>
            public static string Decrypt(string encrypted, Encoding encoding)
            {
                return Decrypt(encrypted, m_defaultKey, encoding);
            }

            /// <summary>
            /// DES加密,使用给定密钥
            /// </summary>
            /// <param name="original">原始文字</param>
            /// <param name="key">密钥</param>
            /// <param name="encoding">字符编码方案</param>
            /// <returns>密文</returns>
            public static string Encrypt(string original, string key)
            {
                byte[] buff = System.Text.Encoding.Default.GetBytes(original);
                byte[] kb = System.Text.Encoding.Default.GetBytes(key);
                return Convert.ToBase64String(Encrypt(buff, kb));
            }

            /// <summary>
            /// DES解密,使用给定密钥
            /// </summary>
            /// <param name="encrypted">密文</param>
            /// <param name="key">密钥</param>
            /// <param name="encoding">字符编码方案</param>
            /// <returns>明文</returns>
            public static string Decrypt(string encrypted, string key, Encoding encoding)
            {
                byte[] buff = Convert.FromBase64String(encrypted);
                byte[] kb = System.Text.Encoding.Default.GetBytes(key);
                return encoding.GetString(Decrypt(buff, kb));
            }
            /// <summary>
            /// 生成MD5摘要
            /// </summary>
            /// <param name="original">数据源</param>
            /// <returns>摘要</returns>
            public static byte[] MakeMD5(byte[] original)
            {
                MD5CryptoServiceProvider hashmd = new MD5CryptoServiceProvider();
                byte[] keyhash = hashmd.ComputeHash(original);
                hashmd = null;
                return keyhash;
            }

            /// <summary>
            /// 使用给定密钥加密
            /// </summary>
            /// <param name="original">明文</param>
            /// <param name="key">密钥</param>
            /// <returns>密文</returns>
            public static byte[] Encrypt(byte[] original, byte[] key)
            {
                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
                //RC2CryptoServiceProvider des = new RC2CryptoServiceProvider();
                des.Key = MakeMD5(key);
                des.Mode = CipherMode.ECB;

                return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
            }

            /// <summary>
            /// 使用给定密钥解密数据
            /// </summary>
            /// <param name="encrypted">密文</param>
            /// <param name="key">密钥</param>
            /// <returns>明文</returns>
            public static byte[] Decrypt(byte[] encrypted, byte[] key)
            {
                TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
                des.Key = MakeMD5(key);
                des.Mode = CipherMode.ECB;

                return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
            }

            /// <summary>
            /// 使用给定密钥加密
            /// </summary>
            /// <param name="original">原始数据</param>
            /// <param name="key">密钥</param>
            /// <returns>密文</returns>
            public static byte[] Encrypt(byte[] original)
            {
                byte[] key = System.Text.Encoding.Default.GetBytes(m_defaultKey);
                return Encrypt(original, key);
            }

            /// <summary>
            /// 使用缺省密钥解密数据
            /// </summary>
            /// <param name="encrypted">密文</param>
            /// <param name="key">密钥</param>
            /// <returns>明文</returns>
            public static byte[] Decrypt(byte[] encrypted)
            {
                byte[] key = System.Text.Encoding.Default.GetBytes(m_defaultKey);
                return Decrypt(encrypted, key);
            }


            /**/
            /// <summary>
            /// MD5 16位加密 加密后密码为大写
            /// </summary>
            /// <param name="ConvertString"></param>
            /// <returns></returns>
            public static string StrToMD5_16(string ConvertString)
            {
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString)), 4, 8);
                t2 = t2.Replace("-", "");
                return t2;
            }


            /// <summary>
            /// MD5 32位加密 加密后密码为小写
            /// </summary>
            /// <param name="inStr"></param>
            /// <returns></returns>
            public static string StrToMD5_32(string inStr)
            {
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                byte[] InBytes = Encoding.GetEncoding("GB2312").GetBytes(inStr);
                byte[] OutBytes = md5.ComputeHash(InBytes);
                string OutString = "";
                for (int i = 0; i < OutBytes.Length; i++)
                {
                    OutString += OutBytes[i].ToString("x2");
                }
                return OutString;
            }


            /// <summary>
            /// 储存密码时的字符串MD5/SHA1加密
            /// </summary>
            /// <param name="PasswordString"></param>
            /// <param name="PasswordFormat">“SHA1”或“MD5”</param>
            /// <returns></returns>
            public static string EncryptPassword(string PasswordString, string PasswordFormat)
            {
                string EncryptPassword = "";
                if (PasswordFormat == "SHA1")
                {
                    EncryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "SHA1");
                }
                else if (PasswordFormat == "MD5")
                {
                    EncryptPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(PasswordString, "MD5");
                }
                else
                {
                    EncryptPassword = PasswordString;//未加密
                }
                return EncryptPassword;
            }

        }
    }

  • 相关阅读:
    跟光磊学Python开发-面向对象入门
    插件调用下推操作
    K3Wise老单获取单据行数
    git 添加和删除 global 的 remote.origin.url
    CSV转Excel格式
    java 下载文件
    windows下安装redis并设置自启动
    linxu jdk安装
    Linux安装部署Redis
    CentOS上安装MySQL(使用国内源)
  • 原文地址:https://www.cnblogs.com/dashi/p/4034749.html
Copyright © 2020-2023  润新知