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);
}
}
}
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);
}
}
}