常用加密/解密方法整理

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

namespace IMS.BaseFramework
{
    /// <summary>
    /// 对常用加密和解密方法进行封装
    /// https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.des.create?view=netframework-4.6
    /// DES加密明文和密文的长度可能不一样,特点如下:
    /// 原文长度 6 字节 -> 密文长度 8 字节
    /// 原文长度 小于等于 7 字节时 -> 密文长度 8 字节
    /// 原文长度 8 字节 -> 密文长度 16 字节
    /// </summary>
    public class EncryptHelper
    {
        #region DES 加密和解密

        // DES 加密/解密密钥(长度必须是64bit,即8个字符)
        static string DesKey = "12345678";
        static string DesIV = "12345678";

        static EncryptHelper()
        {
        }

        public static string DesEncrypt(string Data, string desKey = "", string desIV = "")
        {
            byte[] KeyDefault = new ASCIIEncoding().GetBytes(DesKey);
            byte[] IVDefault = new ASCIIEncoding().GetBytes(DesIV);

            if (!string.IsNullOrEmpty(DesKey))
            {
                KeyDefault = new ASCIIEncoding().GetBytes(DesKey);
            }

            if (!string.IsNullOrEmpty(desIV))
            {
                IVDefault = new ASCIIEncoding().GetBytes(DesIV);
            }

            return DesEncrypt(Data, KeyDefault, IVDefault);
        }

        public static byte[] DesEncrypt(byte[] DataByte, string desKey = "", string desIV = "")
        {
            byte[] KeyDefault = new ASCIIEncoding().GetBytes(DesKey);
            byte[] IVDefault = new ASCIIEncoding().GetBytes(DesIV);

            if (!string.IsNullOrEmpty(DesKey))
            {
                KeyDefault = new ASCIIEncoding().GetBytes(DesKey);
            }

            if (!string.IsNullOrEmpty(desIV))
            {
                IVDefault = new ASCIIEncoding().GetBytes(DesIV);
            }

            return DesEncrypt(DataByte, KeyDefault, IVDefault);
        }

        static string DesEncrypt(string Data, byte[] Key, byte[] IV)
        {
            try
            {
                // 新建一个内存流对象
                MemoryStream mStream = new MemoryStream();

                // 新建一个DES对象
                DES DESalg = DES.Create();

                // 使用内存流对象新建一个加密流对象,并传入加密密钥和初始化向量
                CryptoStream cStream = new CryptoStream(mStream,
                    DESalg.CreateEncryptor(Key, IV),
                    CryptoStreamMode.Write);

                // 把传入的字符串转换成字节数组
                byte[] toEncrypt = Encoding.Default.GetBytes(Data);

                // 把字节数组写入加密对象中,并清空字节数组
                cStream.Write(toEncrypt, 0, toEncrypt.Length);
                cStream.FlushFinalBlock();

                // 从内存流对象中获取加密后的密文字节数组
                byte[] retByte = mStream.ToArray();

                // 释放流对象
                cStream.Close();
                mStream.Close();

                StringBuilder retStr = new StringBuilder();

                foreach (byte b in retByte)
                {
                    retStr.AppendFormat("{0:X2}", b);
                }

                return retStr.ToString();
            }
            catch (CryptographicException e)
            {
                return "";
            }
        }

        static byte[] DesEncrypt(byte[] Data, byte[] Key, byte[] IV)
        {
            try
            {
                MemoryStream mStream = new MemoryStream();

                DES DESalg = DES.Create();
                CryptoStream cStream = new CryptoStream(mStream,
                    DESalg.CreateEncryptor(Key, IV),
                    CryptoStreamMode.Write);

                byte[] toEncrypt = Data;
                cStream.Write(toEncrypt, 0, toEncrypt.Length);
                cStream.FlushFinalBlock();

                byte[] ret = mStream.ToArray();
                cStream.Close();
                mStream.Close();

                return ret;
            }
            catch (CryptographicException e)
            {
                return null;
            }
        }

        public static string DesDecrypt(string Data, string desKey = "", string desIV = "")
        {
            byte[] KeyDefault = new ASCIIEncoding().GetBytes(DesKey);
            byte[] IVDefault = new ASCIIEncoding().GetBytes(DesIV);

            if (!string.IsNullOrEmpty(DesKey))
            {
                KeyDefault = new ASCIIEncoding().GetBytes(DesKey);
            }

            if (!string.IsNullOrEmpty(desIV))
            {
                IVDefault = new ASCIIEncoding().GetBytes(DesIV);
            }

            return DesDecrypt(Data, KeyDefault, IVDefault);
        }

        public static byte[] DesDecrypt(byte[] DataByte, string desKey = "", string desIV = "")
        {
            byte[] KeyDefault = new ASCIIEncoding().GetBytes(DesKey);
            byte[] IVDefault = new ASCIIEncoding().GetBytes(DesIV);

            if (!string.IsNullOrEmpty(DesKey))
            {
                KeyDefault = new ASCIIEncoding().GetBytes(DesKey);
            }

            if (!string.IsNullOrEmpty(desIV))
            {
                IVDefault = new ASCIIEncoding().GetBytes(DesIV);
            }

            return DesDecryptByte(DataByte, KeyDefault, IVDefault);
        }

        static string DesDecrypt(string Data, byte[] Key, byte[] IV)
        {
            try
            {
                int len;
                len = Data.Length / 2;
                byte[] inputByteArray = new byte[len];
                int x, i;
                for (x = 0; x < len; x++)
                {
                    i = Convert.ToInt32(Data.Substring(x * 2, 2), 16);
                    inputByteArray[x] = (byte)i;
                }

                MemoryStream msDecrypt = new MemoryStream();

                DES DESalg = DES.Create();
                CryptoStream csDecrypt = new CryptoStream(msDecrypt,
                    DESalg.CreateDecryptor(Key, IV),
                    CryptoStreamMode.Write);

                csDecrypt.Write(inputByteArray, 0, inputByteArray.Length);
                csDecrypt.FlushFinalBlock();

                return Encoding.Default.GetString(msDecrypt.ToArray());
            }
            catch (CryptographicException e)
            {
                return "";
            }
        }

        static byte[] DesDecryptByte(byte[] Data, byte[] Key, byte[] IV)
        {
            try
            {
                // 新建一个内存流对象
                MemoryStream msDecrypt = new MemoryStream();

                // 新建一个DES对象
                DES DESalg = DES.Create();

                // 利用内存流对象来新建一个加密流对象,并传入加密密钥和初始化向量
                CryptoStream csDecrypt = new CryptoStream(msDecrypt,
                    DESalg.CreateDecryptor(Key, IV),
                    CryptoStreamMode.Write);

                csDecrypt.Write(Data, 0, Data.Length);
                csDecrypt.FlushFinalBlock();

                return msDecrypt.ToArray();
            }
            catch (CryptographicException e)
            {
                return null;
            }
        }

        static byte[] DesDecryptByteWithRead(byte[] Data, byte[] Key, byte[] IV)
        {
            try
            {
                MemoryStream msDecrypt = new MemoryStream(Data);

                DES DESalg = DES.Create();

                CryptoStream csDecrypt = new CryptoStream(msDecrypt,
                    DESalg.CreateDecryptor(Key, IV),
                    CryptoStreamMode.Read);

                byte[] fromEncrypt = new byte[Data.Length];

                // 从crypto对象中读出解密后的数据,并把它保存到临时缓冲区
                // 注意:这里指定了读取的字节数量 fromEncrypt.Length,由于DES明文和密文的字节数量可能不一样
                // 因此指定长度后可能导致返回的字节数组后面加了很多 \0 标记,这时需要在另外处理
                // 当然,由于解密返回的字节数只会变少或和明文字节数一样多,这里不会丢失字节,但是加密就可能会丢失字节
                csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

                return fromEncrypt;
            }
            catch (CryptographicException e)
            {
                return null;
            }
        }

        #endregion

        #region MD5 加密

        static string md5Begin = "start";
        static string md5End = "end";

        public static string GetMD5String(string str)
        {
            str = string.Concat(md5Begin, str, md5End);
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] fromData = Encoding.Unicode.GetBytes(str);
            byte[] targetData = md5.ComputeHash(fromData);
            string md5String = string.Empty;
            foreach (var b in targetData)
            {
                md5String += b.ToString("x2");
            }

            return md5String;
        }

        #endregion

        #region AES

        private const string saltString = "htrfid@abc";
        private const string keyString = "abc@htrfid";

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="sSource">需要解密的内容</param>
        /// <returns></returns>
        public static byte[] AESDecryptString(string strSource)
        {
            byte[] encryptBytes = Convert.FromBase64String(strSource);
            byte[] salt = Encoding.UTF8.GetBytes(saltString);
            //提供高级加密标准 (AES) 对称算法的托管实现。
            AesManaged aes = new AesManaged();
            //通过使用基于 System.Security.Cryptography.HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2)。
            Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(keyString, salt);
            // 获取或设置加密操作的块大小(以位为单位)。
            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            //获取或设置用于对称算法的密钥大小(以位为单位)。
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;
            //获取或设置用于对称算法的密钥。
            aes.Key = rfc.GetBytes(aes.KeySize / 8);
            //获取或设置用于对称算法的初始化向量 (IV)。
            aes.IV = rfc.GetBytes(aes.BlockSize / 8);

            // 用当前的 Key 属性和初始化向量 IV 创建对称解密器对象
            System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor();

            // 解密后的输出流
            MemoryStream decryptStream = new MemoryStream();

            // 将解密后的目标流(decryptStream)与解密转换(decryptTransform)相连接
            CryptoStream decryptor = new CryptoStream(
                decryptStream, decryptTransform, CryptoStreamMode.Write);

            // 将一个字节序列写入当前 CryptoStream (完成解密的过程)
            decryptor.Write(encryptBytes, 0, encryptBytes.Length);
            decryptor.Close();

            // 将解密后所得到的流转换为字符串
            return decryptStream.ToArray();

        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="sSource">需要加密的内容</param>
        /// <returns></returns>
        public static byte[] AESEncryptString(string strSource)
        {
            byte[] data = UTF8Encoding.UTF8.GetBytes(strSource);
            byte[] salt = UTF8Encoding.UTF8.GetBytes(saltString);

            // AesManaged - 高级加密标准(AES) 对称算法的管理类
            AesManaged aes = new AesManaged();

            // Rfc2898DeriveBytes - 通过使用基于 HMACSHA1 的伪随机数生成器,实现基于密码的密钥派生功能 (PBKDF2 - 一种基于密码的密钥派生函数)
            // 通过 密码 和 salt 派生密钥
            Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(keyString, salt);

            /*
            * AesManaged.BlockSize - 加密操作的块大小(单位:bit)
            * AesManaged.LegalBlockSizes - 对称算法支持的块大小(单位:bit)
            * AesManaged.KeySize - 对称算法的密钥大小(单位:bit)
            * AesManaged.LegalKeySizes - 对称算法支持的密钥大小(单位:bit)
            * AesManaged.Key - 对称算法的密钥
            * AesManaged.IV - 对称算法的密钥大小
            * Rfc2898DeriveBytes.GetBytes(int 需要生成的伪随机密钥字节数) - 生成密钥
            */

            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;
            aes.Key = rfc.GetBytes(aes.KeySize / 8);
            aes.IV = rfc.GetBytes(aes.BlockSize / 8);

            // 用当前的 Key 属性和初始化向量 IV 创建对称加密器对象
            ICryptoTransform encryptTransform = aes.CreateEncryptor();

            // 加密后的输出流
            MemoryStream encryptStream = new MemoryStream();

            // 将加密后的目标流(encryptStream)与加密转换(encryptTransform)相连接
            CryptoStream encryptor = new CryptoStream
                (encryptStream, encryptTransform, CryptoStreamMode.Write);

            // 将一个字节序列写入当前 CryptoStream (完成加密的过程)
            encryptor.Write(data, 0, data.Length);
            encryptor.Close();

            return encryptStream.ToArray();
        }

        #endregion
    }
}

 

posted on 2018-05-22 13:32  wangzhiliang  阅读(624)  评论(0编辑  收藏  举报

导航