avatar

目录
加密算法帮助类

集成了加密算法帮助类

c#
/// 
/// 加密算法封装
///

public class EncryptionHelper
{
#region MD5加密
#region MD516
///
/// 16位MD5加密
///

///
///
public static string MD5Encrypt16(string password)
{
var md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(password)), 4, 8);
t2 = t2.Replace("-", "");
return t2;
}
#endregion

#region MD532
///
/// 32位MD5加密
///

///
///
public static string MD5Encrypt32(string password)
{
string cl = password;
string pwd = "";
MD5 md5 = MD5.Create(); //实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
// 通过使用循环,将字节类型的数组转换为字符串,此字符串是常规字符格式化所得
for (int i = 0; i < s.Length; i++)
{
// 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
pwd = pwd + s[i].ToString("X");
}
return pwd;
}
#endregion

#region MD564
///
/// 64位MD5加密
///

///
///
public static string MD5Encrypt64(string password)
{
string cl = password;
//string pwd = "";
MD5 md5 = MD5.Create(); //实例化一个md5对像
// 加密后是一个字节类型的数组,这里要注意编码UTF8/Unicode等的选择 
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
return Convert.ToBase64String(s);
}
#endregion
#endregion

private static readonly byte[] IvBytes = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF };

#region 通用加密算法

///
/// 哈希加密算法
///

/// 所有加密哈希算法实现均必须从中派生的基类
/// 待加密的字符串
/// 字符编码
///
private static string HashEncrypt(HashAlgorithm hashAlgorithm, string input, Encoding encoding)
{
var data = hashAlgorithm.ComputeHash(encoding.GetBytes(input));

return BitConverter.ToString(data).Replace("-", "");
}

///
/// 验证哈希值
///

/// 所有加密哈希算法实现均必须从中派生的基类
/// 未加密的字符串
/// 经过加密的哈希值
/// 字符编码
///
private static bool VerifyHashValue(HashAlgorithm hashAlgorithm, string unhashedText, string hashedText,
Encoding encoding)
{
return string.Equals(HashEncrypt(hashAlgorithm, unhashedText, encoding), hashedText,
StringComparison.OrdinalIgnoreCase);
}

#endregion 通用加密算法

#region 哈希加密算法

#region MD5 算法

///
/// MD5 加密
///

/// 待加密的字符串
/// 字符编码
///
public static string MD5Encrypt(string input, Encoding encoding)
{
return HashEncrypt(MD5.Create(), input, encoding);
}

///
/// 验证 MD5 值
///

/// 未加密的字符串
/// 字符编码
///
public static bool VerifyMD5Value(string input, Encoding encoding)
{
return VerifyHashValue(MD5.Create(), input, MD5Encrypt(input, encoding), encoding);
}

#endregion MD5 算法

#region SHA1 算法

///
/// SHA1 加密
///

/// 要加密的字符串
/// 字符编码
///
public static string SHA1Encrypt(string input, Encoding encoding)
{
return HashEncrypt(SHA1.Create(), input, encoding);
}

///
/// 验证 SHA1 值
///

/// 未加密的字符串
/// 字符编码
///
public static bool VerifySHA1Value(string input, Encoding encoding)
{
return VerifyHashValue(SHA1.Create(), input, SHA1Encrypt(input, encoding), encoding);
}

#endregion SHA1 算法

#region SHA256 算法

///
/// SHA256 加密
///

/// 要加密的字符串
/// 字符编码
///
public static string SHA256Encrypt(string input, Encoding encoding)
{
return HashEncrypt(SHA256.Create(), input, encoding);
}

///
/// 验证 SHA256 值
///

/// 未加密的字符串
/// 字符编码
///
public static bool VerifySHA256Value(string input, Encoding encoding)
{
return VerifyHashValue(SHA256.Create(), input, SHA256Encrypt(input, encoding), encoding);
}

#endregion SHA256 算法

#region SHA384 算法

///
/// SHA384 加密
///

/// 要加密的字符串
/// 字符编码
///
public static string SHA384Encrypt(string input, Encoding encoding)
{
return HashEncrypt(SHA384.Create(), input, encoding);
}

///
/// 验证 SHA384 值
///

/// 未加密的字符串
/// 字符编码
///
public static bool VerifySHA384Value(string input, Encoding encoding)
{
return VerifyHashValue(SHA256.Create(), input, SHA384Encrypt(input, encoding), encoding);
}

#endregion SHA384 算法

#region SHA512 算法

///
/// SHA512 加密
///

/// 要加密的字符串
/// 字符编码
///
public static string SHA512Encrypt(string input, Encoding encoding)
{
return HashEncrypt(SHA512.Create(), input, encoding);
}

///
/// 验证 SHA512 值
///

/// 未加密的字符串
/// 字符编码
///
public static bool VerifySHA512Value(string input, Encoding encoding)
{
return VerifyHashValue(SHA512.Create(), input, SHA512Encrypt(input, encoding), encoding);
}

#endregion SHA512 算法

#region HMAC-MD5 加密

///
/// HMAC-MD5 加密
///

/// 要加密的字符串
/// 密钥
/// 字符编码
///
public static string HMACSMD5Encrypt(string input, string key, Encoding encoding)
{
return HashEncrypt(new HMACMD5(encoding.GetBytes(key)), input, encoding);
}

#endregion HMAC-MD5 加密

#region HMAC-SHA1 加密

///
/// HMAC-SHA1 加密
///

/// 要加密的字符串
/// 密钥
/// 字符编码
///
public static string HMACSHA1Encrypt(string input, string key, Encoding encoding)
{
return HashEncrypt(new HMACSHA1(encoding.GetBytes(key)), input, encoding);
}

#endregion HMAC-SHA1 加密

#region HMAC-SHA256 加密

///
/// HMAC-SHA256 加密
///

/// 要加密的字符串
/// 密钥
/// 字符编码
///
public static string HMACSHA256Encrypt(string input, string key, Encoding encoding)
{
return HashEncrypt(new HMACSHA256(encoding.GetBytes(key)), input, encoding);
}

#endregion HMAC-SHA256 加密

#region HMAC-SHA384 加密

///
/// HMAC-SHA384 加密
///

/// 要加密的字符串
/// 密钥
/// 字符编码
///
public static string HMACSHA384Encrypt(string input, string key, Encoding encoding)
{
return HashEncrypt(new HMACSHA384(encoding.GetBytes(key)), input, encoding);
}

#endregion HMAC-SHA384 加密

#region HMAC-SHA512 加密

///
/// HMAC-SHA512 加密
///

/// 要加密的字符串
/// 密钥
/// 字符编码
///
public static string HMACSHA512Encrypt(string input, string key, Encoding encoding)
{
return HashEncrypt(new HMACSHA512(encoding.GetBytes(key)), input, encoding);
}

#endregion HMAC-SHA512 加密

#endregion 哈希加密算法

#region 对称加密算法

#region Des 加解密

///
/// DES 加密
///

/// 待加密的字符串
/// 密钥
///
public static string DESEncrypt(string input, string key)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
byte[] keyIV = keyBytes;
byte[] inputByteArray = Encoding.UTF8.GetBytes(input);
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Convert.ToBase64String(mStream.ToArray());
}

///
/// DES 解密
///

/// 待解密的字符串
/// 密钥(8位)
///
public static string DESDecrypt(string input, string key)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
byte[] keyIV = keyBytes;
byte[] inputByteArray = Convert.FromBase64String(input);
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}

#endregion Des 加解密

#endregion 对称加密算法

#region 非对称加密算法

///
/// 生成 RSA 公钥和私钥
///

/// 公钥
/// 私钥
public static void GenerateRSAKeys(out string publicKey, out string privateKey)
{
using (var rsa = new RSACryptoServiceProvider())
{
publicKey = rsa.ToXmlString(false);
privateKey = rsa.ToXmlString(true);
}
}

///
/// RSA 加密
///

/// 公钥
/// 待加密的内容
/// 经过加密的字符串
public static string RSAEncrypt(string publickey, string content)
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(publickey);
var cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false);

return Convert.ToBase64String(cipherbytes);
}

///
/// RSA 解密
///

/// 私钥
/// 待解密的内容
/// 解密后的字符串
public static string RSADecrypt(string privatekey, string content)
{
var rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(privatekey);
var cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false);

return Encoding.UTF8.GetString(cipherbytes);
}

#endregion 非对称加密算法
}

评论