using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
namespace WinformGeneralDeveloperFrame.Commons
{
///
/// 非对称加密、解密、验证辅助类
///
public class RSASecurityHelper
{
///
/// 非对称加密生成的私钥和公钥
///
/// 私钥
/// 公钥
public static void GenerateRSAKey(out string privateKey, out string publicKey)
{
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
privateKey = rsa.ToXmlString(true);
publicKey = rsa.ToXmlString(false);
}
#region 非对称数据加密(公钥加密)
///
/// 非对称加密字符串数据,返回加密后的数据
///
/// 公钥
/// 待加密的字符串
/// 加密后的数据
public static string RSAEncrypt(string publicKey, string originalString)
{
byte[] PlainTextBArray;
byte[] CypherTextBArray;
string Result;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(publicKey);
PlainTextBArray = (new UnicodeEncoding()).GetBytes(originalString);
CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
Result = Convert.ToBase64String(CypherTextBArray);
return Result;
}
///
/// 非对称加密字节数组,返回加密后的数据
///
/// 公钥
/// 待加密的字节数组
/// 返回加密后的数据
public static string RSAEncrypt(string publicKey, byte[] originalBytes)
{
byte[] CypherTextBArray;
string Result;
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(publicKey);
CypherTextBArray = rsa.Encrypt(originalBytes, false);
Result = Convert.ToBase64String(CypherTextBArray);
return Result;
}
#endregion
#region 非对称解密(私钥解密)
///
/// 非对称解密字符串,返回解密后的数据
///
/// 私钥
/// 待解密数据
/// 返回解密后的数据
public static string RSADecrypt(string privateKey, string encryptedString)
{
byte[] PlainTextBArray;
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(privateKey);
PlainTextBArray = Convert.FromBase64String(encryptedString);
DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
///
/// 非对称解密字节数组,返回解密后的数据
///
/// 私钥
/// 待解密数据
///
public static string RSADecrypt(string privateKey, byte[] encryptedBytes)
{
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(privateKey);
DypherTextBArray = rsa.Decrypt(encryptedBytes, false);
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
#endregion
#region 非对称加密签名、验证
///
/// 使用非对称加密签名数据
///
/// 私钥
/// 待加密的字符串
/// 加密后的数据
public static string RSAEncrypSignature(string privateKey, string originalString)
{
string signature;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
rsa.FromXmlString(privateKey); //私钥
// 加密对象
RSAPKCS1SignatureFormatter f = new RSAPKCS1SignatureFormatter(rsa);
f.SetHashAlgorithm("SHA1");
byte[] source = ASCIIEncoding.ASCII.GetBytes(originalString);
SHA1Managed sha = new SHA1Managed();
byte[] result = sha.ComputeHash(source);
byte[] b = f.CreateSignature(result);
signature = Convert.ToBase64String(b);
}
return signature;
}
///
/// 对私钥加密签名的字符串,使用公钥对其进行验证
///
/// 未加密的文本,如机器码
/// 加密后的文本,如注册序列号
/// 如果验证成功返回True,否则为False
//public static bool Validate(string originalString, string encrytedString)
//{
// return Validate(originalString, encrytedString, UIConstants.PublicKey);
//}
///
/// 对私钥加密的字符串,使用公钥对其进行验证
///
/// 未加密的文本,如机器码
/// 加密后的文本,如注册序列号
/// 非对称加密的公钥
/// 如果验证成功返回True,否则为False
public static bool Validate(string originalString, string encrytedString, string publicKey)
{
bool bPassed = false;
using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
{
try
{
rsa.FromXmlString(publicKey); //公钥
RSAPKCS1SignatureDeformatter formatter = new RSAPKCS1SignatureDeformatter(rsa);
formatter.SetHashAlgorithm("SHA1");
byte[] key = Convert.FromBase64String(encrytedString); //验证
SHA1Managed sha = new SHA1Managed();
byte[] name = sha.ComputeHash(ASCIIEncoding.ASCII.GetBytes(originalString));
if (formatter.VerifySignature(name, key))
{
bPassed = true;
}
}
catch
{
}
}
return bPassed;
}
#endregion
#region Hash 加密
/// Hash 加密
///
///
public static int HashEncrypt(string str2Hash)
{
const int salt = 100716; // 盐值
str2Hash += "Commons"; // 增加一个常量字符串
int len = str2Hash.Length;
int result = (str2Hash[len - 1] - 31) * 95 + salt;
for (int i = 0; i < len - 1; i++)
{
result = (result * 95) + (str2Hash[i] - 32);
}
return result;
}
///
/// MD5加密
///
/// 待加密字串
/// 加密后的字串
public static string ComputeMD5(string str)
{
byte[] hashValue = ComputeMD5Data(str);
return BitConverter.ToString(hashValue).Replace("-", "");
}
///
/// MD5加密
///
/// 待加密字串
/// 加密后的字串
public static byte[] ComputeMD5Data(string input)
{
byte[] buffer = Encoding.UTF8.GetBytes(input);
return MD5.Create().ComputeHash(buffer);
}
///
/// MD5加密
///
/// 待加密数据
/// 加密后的字串
public static byte[] ComputeMD5Data(byte[] data)
{
return MD5.Create().ComputeHash(data);
}
///
/// MD5加密
///
/// 待加密流
/// 加密后的字串
public static byte[] ComputeMD5Data(Stream stream)
{
return MD5.Create().ComputeHash(stream);
}
#endregion
}
}