• c# 加密工具类


    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web.Security;
    
    namespace HuaTong.General.Utility
    {
        /// <summary>
        /// 加密工具类
        /// </summary>
        public class EncryptHelper
        {
            //默认密钥
            private static string AESKey = "[45/*YUIdse..e;]";
            private static string DESKey = "[&HdN72]";
    
            /// <summary>  
            /// AES加密  
            /// </summary>
            public static string AESEncrypt(string value, string _aeskey = null)
            {
                if (string.IsNullOrEmpty(_aeskey))
                {
                    _aeskey = AESKey;
                }
    
                byte[] keyArray = Encoding.UTF8.GetBytes(_aeskey);
                byte[] toEncryptArray = Encoding.UTF8.GetBytes(value);
    
                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);
            }
    
            /// <summary>  
            /// AES解密  
            /// </summary>
            public static string AESDecrypt(string value, string _aeskey = null)
            {
                try
                {
                    if (string.IsNullOrEmpty(_aeskey))
                    {
                        _aeskey = AESKey;
                    }
                    byte[] keyArray = Encoding.UTF8.GetBytes(_aeskey);
                    byte[] toEncryptArray = Convert.FromBase64String(value);
    
                    RijndaelManaged rDel = new RijndaelManaged();
                    rDel.Key = keyArray;
                    rDel.Mode = CipherMode.ECB;
                    rDel.Padding = PaddingMode.PKCS7;
    
                    ICryptoTransform cTransform = rDel.CreateDecryptor();
                    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    
                    return Encoding.UTF8.GetString(resultArray);
                }
                catch
                {
                    return string.Empty;
                }
            }
    
            /// <summary>  
            /// DES加密  
            /// </summary>
            public static string DESEncrypt(string value, string _deskey = null)
            {
                if (string.IsNullOrEmpty(_deskey))
                {
                    _deskey = DESKey;
                }
    
                byte[] keyArray = Encoding.UTF8.GetBytes(_deskey);
                byte[] toEncryptArray = Encoding.UTF8.GetBytes(value);
    
                DESCryptoServiceProvider rDel = new DESCryptoServiceProvider();
                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);
            }
    
            /// <summary>  
            /// DES解密  
            /// </summary>
            public static string DESDecrypt(string value, string _deskey = null)
            {
                try
                {
                    if (string.IsNullOrEmpty(_deskey))
                    {
                        _deskey = DESKey;
                    }
                    byte[] keyArray = Encoding.UTF8.GetBytes(_deskey);
                    byte[] toEncryptArray = Convert.FromBase64String(value);
    
                    DESCryptoServiceProvider rDel = new DESCryptoServiceProvider();
                    rDel.Key = keyArray;
                    rDel.Mode = CipherMode.ECB;
                    rDel.Padding = PaddingMode.PKCS7;
    
                    ICryptoTransform cTransform = rDel.CreateDecryptor();
                    byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
    
                    return Encoding.UTF8.GetString(resultArray);
                }
                catch
                {
                    return string.Empty;
                }
            }
    
            public static string MD5(string value,Encoding encoding=null)
            {
                if (encoding == null)
                {
                    encoding = Encoding.UTF8;
                }
                byte[] result = encoding.GetBytes(value);
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] output = md5.ComputeHash(result);
    
                return BitConverter.ToString(output).Replace("-", "");
            }
    
            public static string HMACMD5(string value, string hmacKey, Encoding encoding = null)
            {
                if (encoding == null)
                {
                    encoding = Encoding.UTF8;
                }
                HMACMD5 hmacsha1 = new HMACMD5(encoding.GetBytes(hmacKey));
                byte[] result = encoding.GetBytes(value);
                byte[] output = hmacsha1.ComputeHash(result);
                
                return BitConverter.ToString(output).Replace("-", "");
            }
    
            /// <summary>
            /// base64编码
            /// </summary>
            /// <returns></returns>
            public static string Base64Encode(string value)
            {
                string result = Convert.ToBase64String(Encoding.Default.GetBytes(value));
                return result;
            }
            /// <summary>
            /// base64解码
            /// </summary>
            /// <returns></returns>
            public static string Base64Decode(string value)
            {
                string result = Encoding.Default.GetString(Convert.FromBase64String(value));
                return result;
            }
    
    
        }
    }
  • 相关阅读:
    文本框输入限制 [大全]
    JavaScript关于window.open()应用
    你还敢使用window.open弹广告吗?
    在HTML网页中插入ActiveX控件
    XMLDOC的常用方法和属性
    细说HTML元素的ID和Name属性的区别
    常用的40个网站制作技巧
    激活 ActiveX 控件
    laravel 配置 elasticsearch
    mac上运行ls 命名查看做面文件的出现“Operation not permitted”错误
  • 原文地址:https://www.cnblogs.com/password1/p/5870705.html
Copyright © 2020-2023  润新知