md5加密 密钥加密解密

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Security.Cryptography;
using System.Text;

namespace SoQi.Common.Cryptography {
    public class Cryptor {
        /// <summary>
        /// 计算对象的HASH数列
        /// </summary>
        /// <param name="valueToHash">欲计算的对象</param>
        /// <returns>HASH数列的BASE64编码</returns>
        public static string HashObject(object valueToHash) {
            return HashObject(valueToHash, "MD5");
        }

        /// <summary>
        /// 计算对象的HASH数列
        /// </summary>
        /// <param name="valueToHash">欲计算的对象</param>
        /// <param name="hashAlgorithm">欲计算的算法,如SHA1,MD5等</param>
        /// <returns>HASH数列的BASE64编码</returns>
        public static string HashObject(object valueToHash, string hashAlgorithm) {
            if (valueToHash == null) valueToHash = typeof(void);
            switch (hashAlgorithm.ToUpper()) {
                case "SHA1": {
                        SHA1 sha1 = new SHA1CryptoServiceProvider();
                        BinaryFormatter formatter = new BinaryFormatter();
                        using (MemoryStream stream = new MemoryStream()) {
                            formatter.Serialize(stream, valueToHash);
                            return Convert.ToBase64String(sha1.ComputeHash(stream.ToArray()), Base64FormattingOptions.None);
                        }
                    }
                case "MD5": {
                        MD5 md5 = new MD5CryptoServiceProvider();
                        BinaryFormatter formatter = new BinaryFormatter();
                        using (MemoryStream stream = new MemoryStream()) {
                            formatter.Serialize(stream, valueToHash);
                            return Convert.ToBase64String(md5.ComputeHash(stream.ToArray()), Base64FormattingOptions.None);
                        }
                    }
            }
            throw new NotSupportedException("加密算法不支持");
        }

        /// <summary>
        /// 使用DES加密算法对数据加密
        /// </summary>
        /// <param name="data">欲加密的字节数组</param>
        /// <param name="key">密钥</param>
        /// <returns>加密后的字节数组</returns>
        public static byte[] DESEncrypt(byte[] data, string key) {
            if (data == null) return null;
            if (data.Length == 0) return data;
            return new GenericSymmetricCryptor(DES.Create()).Encrypto(data, key);
        }

        /// <summary>
        /// 使用DES加密算法对数据解密
        /// </summary>
        /// <param name="data">欲解密的字节数组</param>
        /// <param name="key">密钥</param>
        /// <returns>解密后的字节数组</returns>
        public static byte[] DESDecrypt(byte[] data, string key) {
            if (data == null) return null;
            if (data.Length == 0) return data;
            return new GenericSymmetricCryptor(DES.Create()).Decrypto(data, key);
        }

        /// <summary>
        /// 使用DES加密算法对数据加密
        /// </summary>
        /// <param name="data">欲加密的字符串</param>
        /// <param name="key">密钥</param>
        /// <returns>加密后的字符串</returns>
        public static string DESEncrypt(string data, string key) {
            byte[] buf = System.Text.UTF8Encoding.UTF8.GetBytes(data);
            return Convert.ToBase64String(DESEncrypt(buf, key));
        }

        /// <summary>
        /// 使用DES加密算法对数据解密
        /// </summary>
        /// <param name="data">欲解密的字符串</param>
        /// <param name="key">密钥</param>
        /// <returns>解密后的字符串</returns>
        public static string DESDecrypt(string data, string key) {
            byte[] buf = Convert.FromBase64String(data);
            return UTF8Encoding.UTF8.GetString(DESDecrypt(buf, key));
        }

        public static string GenerateSalt() {
            byte[] buffer = new byte[16];
            new RNGCryptoServiceProvider().GetBytes(buffer);
            return Convert.ToBase64String(buffer);
        }

        public static string HashString(string pass, string salt) {
            byte[] buffer1 = Encoding.Unicode.GetBytes(pass);
            byte[] buffer2 = Convert.FromBase64String(salt);
            byte[] buffer3 = new byte[buffer2.Length + buffer1.Length];
            byte[] buffer4 = null;
            Buffer.BlockCopy(buffer2, 0, buffer3, 0, buffer2.Length);
            Buffer.BlockCopy(buffer1, 0, buffer3, buffer2.Length, buffer1.Length);
            HashAlgorithm algorithm = HashAlgorithm.Create(System.Web.Security.Membership.HashAlgorithmType);
            if (algorithm == null) {
                throw new Exception("配置项错误:无效的Hash算法,无法创建Hash算法");
            }
            buffer4 = algorithm.ComputeHash(buffer3);
            return Convert.ToBase64String(buffer4);
        }
        

    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值