首页 > 其他 > 详细

EncryptionHelper.cs(20170223)

时间:2017-02-23 22:48:49      阅读:254      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace System.CRM.Common
{
    /// <summary>
    /// 加解密 助手类
    /// </summary>
    public class EncryptionHelper
    {


        #region AES 加密 解密

        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="str">要加密字符串</param>
        /// <param name="aeskey">要加密字符串的密钥(16位或32位)</param>
        /// <returns>返回加密后字符串</returns>
        public static String EncryptAES(String str, string aeskey)
        {
            Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(aeskey);
            Byte[] toEncryptArray = System.Text.UTF8Encoding.UTF8.GetBytes(str);
            System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
            rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateEncryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="str">要解密字符串</param>
        /// <param name="aeskey">要加密字符串的密钥(16位或32位)</param>
        /// <returns>返回解密后字符串</returns>
        public static String DecryptAES(String str, string aeskey)
        {
            Byte[] keyArray = System.Text.UTF8Encoding.UTF8.GetBytes(aeskey);
            Byte[] toEncryptArray = Convert.FromBase64String(str);
            System.Security.Cryptography.RijndaelManaged rDel = new System.Security.Cryptography.RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = System.Security.Cryptography.CipherMode.ECB;
            rDel.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            System.Security.Cryptography.ICryptoTransform cTransform = rDel.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return System.Text.UTF8Encoding.UTF8.GetString(resultArray);
        }

        #endregion

        #region  DES加解密

        /// <summary>
        /// 创建Key
        /// </summary>
        /// <returns></returns>
        public static string GenerateKey()
        {
            DESCryptoServiceProvider desCrypto = (DESCryptoServiceProvider)DESCryptoServiceProvider.Create();
            return ASCIIEncoding.ASCII.GetString(desCrypto.Key);
        }

        /// <summary>
        /// DES加密方法一
        /// </summary>
        /// <param name="pToEncrypt">要加密的文本</param>
        /// <param name="sKey">通过GenerateKey()获取(加解密必须一致)</param>
        /// <returns></returns>
        public static string DESEncrypt(string pToEncrypt, string sKey)
        {

            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            StringBuilder ret = new StringBuilder();
            foreach (byte b in ms.ToArray())
            {
                ret.AppendFormat("{0:X2}", b);
            }
            ret.ToString();
            return ret.ToString();


        }



        /// <summary>
        /// DES解密方法一
        /// </summary>
        /// <param name="pToDecrypt">要解密的文本</param>
        /// <param name="sKey">通过GenerateKey()获取(加解密必须一致)</param>
        /// <returns></returns>
        public static string DESDecrypt(string pToDecrypt, string sKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
            for (int x = 0; x < pToDecrypt.Length / 2; x++)
            {
                int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
                inputByteArray[x] = (byte)i;
            }

            des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();

            StringBuilder ret = new StringBuilder();

            return System.Text.Encoding.Default.GetString(ms.ToArray());
        }




        /// <summary>
        /// DES加密方法二
        /// </summary>
        /// <param name="sInputString">要加密的文本</param>
        /// <param name="sKey">通过GenerateKey()获取(加解密必须一致)</param>
        /// <returns></returns>
        public static string EncryptString(string sInputString, string sKey)
        {
            byte[] data = Encoding.UTF8.GetBytes(sInputString);
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            ICryptoTransform desencrypt = DES.CreateEncryptor();
            byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
            return BitConverter.ToString(result);
        }



        /// <summary>
        /// DES解密方法二
        /// </summary>
        /// <param name="sInputString">要解密的文本</param>
        /// <param name="sKey"></param>
        /// <returns>通过GenerateKey()获取(加解密必须一致)</returns>
        public static string DecryptString(string sInputString, string sKey)
        {
            string[] sInput = sInputString.Split("-".ToCharArray());
            byte[] data = new byte[sInput.Length];
            for (int i = 0; i < sInput.Length; i++)
            {
                data[i] = byte.Parse(sInput[i], NumberStyles.HexNumber);
            }
            DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
            DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
            DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
            ICryptoTransform desencrypt = DES.CreateDecryptor();
            byte[] result = desencrypt.TransformFinalBlock(data, 0, data.Length);
            return Encoding.UTF8.GetString(result);
        }

        #endregion

        #region base64加解密

        /// <summary>
        /// base64加密
        /// </summary>
        /// <param name="strdata">加密的明文</param>
        /// <returns></returns>
        public static string ToEncryptionBase64String(string strdata)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(strdata);
            string str = Convert.ToBase64String(bytes);
            return str;
        }

        /// <summary>
        /// base64解密
        /// </summary>
        /// <param name="strdata">解密的密文</param>
        /// <returns></returns>
        public static string ToDecryptBase64String(string strdata)
        {
            byte[] bytes = Convert.FromBase64String(strdata);
            string str = Encoding.UTF8.GetString(bytes);
            return str;
        }

        #endregion


        #region MD5加密

        /// <summary>
        /// MD5加密(32位大写)
        /// </summary>
        /// <param name="txt">明文</param>
        /// <returns></returns>
        public static string GetMD5(string txt)
        {
            byte[] result = Encoding.Default.GetBytes(txt.Trim());
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] output = md5.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "");
        }

        #endregion

    }
}

 

EncryptionHelper.cs(20170223)

原文:http://www.cnblogs.com/zyx321/p/6435908.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!