加密,解密码,签名,验证签名
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security;
using System.Security.Cryptography;
using System.Text;
namespace Cryption
{
public class Cryption
{
#region 解释Convert.ToBase64String 与 Encoding.GetString 区别
/*
*
二者都是将 byte[] 转换成字符串,一个区别在于 Convert.ToBase64String 使用 base 64 数字编码,所以它生成的全部是 ASCII 字符。
而 Encoding.GetString 就是转换成我们平常所认识的字符串。
一些加密方法返回 byte[],此时我们要转换成字符串用哪个呢?
利用 Encoding.GetString 转换,可能得到很多方块或问号,这是由于某些整数序列无法对应于我们现实生活中的文字的原因,只能用方块或问号来代替。
所以我们一般还是用 Convert.ToBase64String 来进行。
那么从另一方面,字符串转换成 byte[] 用哪个呢?是不是一定要用 FromBase64String 呢?
不是的,我们普通的文字(如String类型)并不是 base 64 编码的,不能使用 FromBase64String 转换成 byte[],比如:FromBase64String("千一网络") 就会出错,因为“千一网络”并不是 base 64 编码的,此时应该用Encoding.GetBytes()。
*/
#endregion
/// <summary>
/// 得到公钥和私钥
/// </summary>
/// <param name="publicKEY"></param>
/// <param name="PrivateKEY"></param>
public static void GetKey(out string publicKEY, out string PrivateKEY)
{
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
publicKEY = rsaProvider.ToXmlString(false);
PrivateKEY = rsaProvider.ToXmlString(true);
}
/// <summary>
/// 加密码
/// </summary>
/// <param name="publicKEY"></param>
/// <param name="encryptText"></param>
/// <returns></returns>
public static string Encrypt(string publicKEY, string encryptText)
{
byte[] byteencryptText = new UnicodeEncoding().GetBytes(encryptText);
RSACryptoServiceProvider rsProvider = new RSACryptoServiceProvider();
rsProvider.FromXmlString(publicKEY);
return Convert.ToBase64String(rsProvider.Encrypt(byteencryptText, false));
}
/// <summary>
/// 解密
/// </summary>
/// <param name="PrivateKEY"></param>
/// <param name="decryptText"></param>
/// <returns></returns>
public static string Decrypt(string PrivateKEY, string decryptText)
{
UnicodeEncoding uni = new UnicodeEncoding();
byte[] byteencryptText = Convert.FromBase64String(decryptText);
RSACryptoServiceProvider rsProvider = new RSACryptoServiceProvider();
rsProvider.FromXmlString(PrivateKEY);
return uni.GetString(rsProvider.Decrypt(byteencryptText, false));
}
/// <summary>
/// 计算指定字节数组的哈希值
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string Gethash(string text)
{
byte[] bytetext = new UnicodeEncoding().GetBytes(text);
HashAlgorithm ha = HashAlgorithm.Create("md5");
return Convert.ToBase64String(ha.ComputeHash(bytetext));
}
/// <summary>
/// 创建签名
/// </summary>
/// <param name="publicKEY"></param>
/// <param name="text"></param>
/// <param name="signatureText">签名后的数据</param>
/// <returns></returns>
public static void SignatureFormatter(string privatekey, string text, ref string signatureText)
{
byte[] byteText = Convert.FromBase64String(text);
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
rsaProvider.FromXmlString(privatekey);
RSAPKCS1SignatureFormatter rsaSignatureFormatter = new RSAPKCS1SignatureFormatter(rsaProvider);
rsaSignatureFormatter.SetHashAlgorithm("MD5");
byte[] bytesignatureText = rsaSignatureFormatter.CreateSignature(byteText);
signatureText = Convert.ToBase64String(bytesignatureText);
}
/// <summary>
/// 验证签名
/// </summary>
/// <param name="key"></param>
/// <param name="text">原文本</param>
/// <param name="SignatureText">签名后文本</param>
/// <returns>true:正确,false:错误</returns>
public static bool SignatureDeFormatter(string publickey, string text, string SignatureText)
{
byte[] byteText = Convert.FromBase64String(text);
byte[] byteSignatureText = Convert.FromBase64String(SignatureText);
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
rsaProvider.FromXmlString(publickey);
RSAPKCS1SignatureDeformatter rsaSignatureDeFormatter = new RSAPKCS1SignatureDeformatter(rsaProvider);
rsaSignatureDeFormatter.SetHashAlgorithm("MD5");
return rsaSignatureDeFormatter.VerifySignature(byteText, byteSignatureText);
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security;
using System.Security.Cryptography;
using System.Text;
namespace Cryption
{
public class Cryption
{
#region 解释Convert.ToBase64String 与 Encoding.GetString 区别
/*
*
二者都是将 byte[] 转换成字符串,一个区别在于 Convert.ToBase64String 使用 base 64 数字编码,所以它生成的全部是 ASCII 字符。
而 Encoding.GetString 就是转换成我们平常所认识的字符串。
一些加密方法返回 byte[],此时我们要转换成字符串用哪个呢?
利用 Encoding.GetString 转换,可能得到很多方块或问号,这是由于某些整数序列无法对应于我们现实生活中的文字的原因,只能用方块或问号来代替。
所以我们一般还是用 Convert.ToBase64String 来进行。
那么从另一方面,字符串转换成 byte[] 用哪个呢?是不是一定要用 FromBase64String 呢?
不是的,我们普通的文字(如String类型)并不是 base 64 编码的,不能使用 FromBase64String 转换成 byte[],比如:FromBase64String("千一网络") 就会出错,因为“千一网络”并不是 base 64 编码的,此时应该用Encoding.GetBytes()。
*/
#endregion
/// <summary>
/// 得到公钥和私钥
/// </summary>
/// <param name="publicKEY"></param>
/// <param name="PrivateKEY"></param>
public static void GetKey(out string publicKEY, out string PrivateKEY)
{
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
publicKEY = rsaProvider.ToXmlString(false);
PrivateKEY = rsaProvider.ToXmlString(true);
}
/// <summary>
/// 加密码
/// </summary>
/// <param name="publicKEY"></param>
/// <param name="encryptText"></param>
/// <returns></returns>
public static string Encrypt(string publicKEY, string encryptText)
{
byte[] byteencryptText = new UnicodeEncoding().GetBytes(encryptText);
RSACryptoServiceProvider rsProvider = new RSACryptoServiceProvider();
rsProvider.FromXmlString(publicKEY);
return Convert.ToBase64String(rsProvider.Encrypt(byteencryptText, false));
}
/// <summary>
/// 解密
/// </summary>
/// <param name="PrivateKEY"></param>
/// <param name="decryptText"></param>
/// <returns></returns>
public static string Decrypt(string PrivateKEY, string decryptText)
{
UnicodeEncoding uni = new UnicodeEncoding();
byte[] byteencryptText = Convert.FromBase64String(decryptText);
RSACryptoServiceProvider rsProvider = new RSACryptoServiceProvider();
rsProvider.FromXmlString(PrivateKEY);
return uni.GetString(rsProvider.Decrypt(byteencryptText, false));
}
/// <summary>
/// 计算指定字节数组的哈希值
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string Gethash(string text)
{
byte[] bytetext = new UnicodeEncoding().GetBytes(text);
HashAlgorithm ha = HashAlgorithm.Create("md5");
return Convert.ToBase64String(ha.ComputeHash(bytetext));
}
/// <summary>
/// 创建签名
/// </summary>
/// <param name="publicKEY"></param>
/// <param name="text"></param>
/// <param name="signatureText">签名后的数据</param>
/// <returns></returns>
public static void SignatureFormatter(string privatekey, string text, ref string signatureText)
{
byte[] byteText = Convert.FromBase64String(text);
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
rsaProvider.FromXmlString(privatekey);
RSAPKCS1SignatureFormatter rsaSignatureFormatter = new RSAPKCS1SignatureFormatter(rsaProvider);
rsaSignatureFormatter.SetHashAlgorithm("MD5");
byte[] bytesignatureText = rsaSignatureFormatter.CreateSignature(byteText);
signatureText = Convert.ToBase64String(bytesignatureText);
}
/// <summary>
/// 验证签名
/// </summary>
/// <param name="key"></param>
/// <param name="text">原文本</param>
/// <param name="SignatureText">签名后文本</param>
/// <returns>true:正确,false:错误</returns>
public static bool SignatureDeFormatter(string publickey, string text, string SignatureText)
{
byte[] byteText = Convert.FromBase64String(text);
byte[] byteSignatureText = Convert.FromBase64String(SignatureText);
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();
rsaProvider.FromXmlString(publickey);
RSAPKCS1SignatureDeformatter rsaSignatureDeFormatter = new RSAPKCS1SignatureDeformatter(rsaProvider);
rsaSignatureDeFormatter.SetHashAlgorithm("MD5");
return rsaSignatureDeFormatter.VerifySignature(byteText, byteSignatureText);
}
}
}