EncryptHelper加密对象-工具类

 

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

namespace Common.Utility
{
    /// 
    /// Author:Kt
    /// Date Created:2011-04-01
    /// Description:加密对象-工具类
    /// 
    public class EncryptHelper
    {
        /// 
        /// AES 解密
        /// 
        /// 内容
        /// 私钥(长度: 16、24、32字节)
        /// 
        public static string AESDecrypt(string content, string secretKey)
        {
            if (string.IsNullOrEmpty(content)) return null;
            Byte[] toEncryptArray = Convert.FromBase64String(content);

            System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes(secretKey),
                Mode = System.Security.Cryptography.CipherMode.ECB,
                Padding = System.Security.Cryptography.PaddingMode.PKCS7
            };

            System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Encoding.UTF8.GetString(resultArray);
        }

        /// 
        /// AES 加密
        /// 
        /// 内容
        /// 私钥(长度: 16、24、32字节)
        /// 
        public static string AESEncrypt(string content, string secretKey)
        {
            byte[] keyArray = UTF8Encoding.UTF8.GetBytes(secretKey);
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(content);

            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;

            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);

            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// 
        /// MD5 加密
        /// 
        /// 内容
        /// 
        public static string Md5(string content)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(content, "MD5");
        }

        /// 
        /// MD5 加密
        /// 
        /// 内容
        /// 编码
        /// 签名字符串
        public static string Md5(string content, Encoding encode)
        {
            var result = new StringBuilder();
            var md5 = new MD5CryptoServiceProvider();
            var bytes = md5.ComputeHash(encode.GetBytes(content));
            foreach (var item in bytes)
                result.Append(item.ToString("x").PadLeft(2, '0'));

            return result.ToString();
        }

        /// 
        /// 3DES 解密
        /// 
        /// 内容
        /// 私钥
        /// 向量
        /// 
        public static string DESDecrypt(string content, string secretKey, string ivs)
        {
            SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
            mCSP.Mode = CipherMode.CBC;
            mCSP.Padding = PaddingMode.PKCS7;
            mCSP.Key = Encoding.UTF8.GetBytes(secretKey);
            mCSP.IV = Encoding.UTF8.GetBytes(ivs);
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;
            ct = mCSP.CreateDecryptor(mCSP.Key, mCSP.IV);
            byt = Convert.FromBase64String(content);
            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());
        }

        /// 
        /// 3DES 加密
        /// 
        /// 内容
        /// 私钥
        /// 向量
        /// 
        public static string DESEncrypt(string content, string secretKey, string ivs)
        {
            SymmetricAlgorithm mCSP = new TripleDESCryptoServiceProvider();
            mCSP.Mode = CipherMode.CBC;
            mCSP.Padding = PaddingMode.PKCS7;
            mCSP.Key = Encoding.UTF8.GetBytes(secretKey);
            mCSP.IV = Encoding.UTF8.GetBytes(ivs);
            ICryptoTransform ct;
            MemoryStream ms;
            CryptoStream cs;
            byte[] byt;
            ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
            byt = Encoding.UTF8.GetBytes(content);
            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());
        }
    }
}

 

using System;
using System.Text;

namespace Common.Utility.Tests
{
    /// 
    /// 运行示例
    /// 
    internal partial class Program
    {
        private static void Main_EncryptHelper(string[] args)
        {
            string str = "需要加密的字符串12345678";
            string md5_1 = EncryptHelper.Md5(str);
            string md5_2 = EncryptHelper.Md5(str, Encoding.UTF8);

            str = "10086";
            string secretKey_AES = Guid.NewGuid().ToString("N");
            string JiaMi = EncryptHelper.AESEncrypt(str, secretKey_AES);
            string JieMi = EncryptHelper.AESDecrypt(JiaMi, secretKey_AES);

            string secretKey_DES = "fsT7ObM1nEnrRAGO1djI2YBi";
            string ivs_DES = "GUGlYE1g";
            JiaMi = EncryptHelper.DESEncrypt(str, secretKey_DES, ivs_DES);
            JieMi = EncryptHelper.DESDecrypt(JiaMi, secretKey_DES, ivs_DES);
        }
    }
}

 

你可能感兴趣的:(EncryptHelper加密对象-工具类)