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 {
        ///
        /// 计算对象的HASH数列
        ///

        /// 欲计算的对象
        /// HASH数列的BASE64编码
        public static string HashObject(object valueToHash) {
            return HashObject(valueToHash, "MD5");
        }

        ///
        /// 计算对象的HASH数列
        ///

        /// 欲计算的对象
        /// 欲计算的算法,如SHA1,MD5等
        /// HASH数列的BASE64编码
        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("加密算法不支持");
        }

        ///
        /// 使用DES加密算法对数据加密
        ///

        /// 欲加密的字节数组
        /// 密钥
        /// 加密后的字节数组
        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);
        }

        ///
        /// 使用DES加密算法对数据解密
        ///

        /// 欲解密的字节数组
        /// 密钥
        /// 解密后的字节数组
        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);
        }

        ///
        /// 使用DES加密算法对数据加密
        ///

        /// 欲加密的字符串
        /// 密钥
        /// 加密后的字符串
        public static string DESEncrypt(string data, string key) {
            byte[] buf = System.Text.UTF8Encoding.UTF8.GetBytes(data);
            return Convert.ToBase64String(DESEncrypt(buf, key));
        }

        ///
        /// 使用DES加密算法对数据解密
        ///

        /// 欲解密的字符串
        /// 密钥
        /// 解密后的字符串
        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);
        }
        

    }
}


你可能感兴趣的:(asp.net(C#))