C# SHA256WithRSA加密帮助类
C# SHA256WithRSA加密帮助类
这篇文章接上一篇一起用,主要是实现SHA1/SHA256和RSA加解密算法。
class SHA256WithRSAHelper
{
//public string Sign(string contentForSign, string priKeyFile, string keyPwd)
//{
// var rsa = GetPrivateKey(priKeyFile, keyPwd);
// // Create a new RSACryptoServiceProvider
// var rsaClear = new RSACryptoServiceProvider();
// // Export RSA parameters from 'rsa' and import them into 'rsaClear'
// var paras = rsa.ExportParameters(true);
// rsaClear.ImportParameters(paras);
// using (var sha256 = new SHA256CryptoServiceProvider())
// {
// var signData = rsa.SignData(Encoding.UTF8.GetBytes(contentForSign), sha256);
// //Console.WriteLine(Convert.ToBase64String(signData));
// // return BytesToHex(signData);
// return HttpUtility.UrlEncode(Convert.ToBase64String(signData));
// }
//}
//public bool VerifySign(string contentForSign, string signedData,string pubKeyFile)
//{
// var rsa = GetPublicKey(pubKeyFile);
// using (var sha256 = new SHA256CryptoServiceProvider())
// {
// //var datas = HexToBytes(signedData);
// var datas = Convert.FromBase64String(signedData);
// return rsa.VerifyData(Encoding.UTF8.GetBytes(contentForSign), sha256, datas);
// }
//}
/ <summary>
/ 获取签名证书私钥
/ </summary>
/ <param name="priKeyFile"></param>
/ <param name="keyPwd"></param>
/ <returns></returns>
//private static RSACryptoServiceProvider GetPrivateKey(string priKeyFile, string keyPwd)
//{
// var pc = new X509Certificate2(priKeyFile, keyPwd, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet);
// return (RSACryptoServiceProvider)pc.PrivateKey;
//}
/ <summary>
/ 获取验签证书
/ </summary>
/ <param name="pubKeyFile"></param>
/ <returns></returns>
//private static RSACryptoServiceProvider GetPublicKey(string pubKeyFile)
//{
// var pc = new X509Certificate2(pubKeyFile);
// return (RSACryptoServiceProvider)pc.PublicKey.Key;
//}
//public static byte[] HexToBytes(string text)
//{
// if (text.Length % 2 != 0)
// throw new ArgumentException("text 长度为奇数。");
// List<byte> lstRet = new List<byte>();
// for (int i = 0; i < text.Length; i = i + 2)
// {
// lstRet.Add(Convert.ToByte(text.Substring(i, 2), 16));
// }
// return lstRet.ToArray();
//}
/ <summary>
/ bytes转换hex
/ </summary>
/ <param name="data">bytes</param>
/ <returns>转换后的hex字符串</returns>
//public static string BytesToHex(byte[] data)
//{
// StringBuilder sbRet = new StringBuilder(data.Length * 2);
// for (int i = 0; i < data.Length; i++)
// {
// sbRet.Append(Convert.ToString(data[i], 16).PadLeft(2, '0'));
// }
// return sbRet.ToString();
//}
public static string RsaSign(string data, string privatekey)
{
//转换成适用于.Net的秘钥
var netKey = RSAKeyConvert.RSAPrivateKeyJava2DotNet(privatekey);
CspParameters CspParameters = new CspParameters();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048, CspParameters);
byte[] bytes = Encoding.UTF8.GetBytes(data);
RSA.FromXmlString(netKey);
byte[] sign = RSA.SignData(bytes, "SHA1");
return Convert.ToBase64String(sign);
}
public static bool RsaVerifySign(string contentForSign, string signedData, string publickey)
{
//转换成适用于.Net的秘钥
var netKey = RSAKeyConvert.RSAPublicKeyJava2DotNet(publickey);
CspParameters CspParameters = new CspParameters();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048, CspParameters);
byte[] bytes = Encoding.UTF8.GetBytes(contentForSign);
RSA.FromXmlString(netKey);
var datas = Convert.FromBase64String(signedData);
var res = RSA.VerifyData(bytes,"SHA1",datas);
return res;
}
public static string RsaPubSign(string data, string pubkey)
{
//转换成适用于.Net的秘钥
var netKey = RSAKeyConvert.RSAPublicKeyJava2DotNet(pubkey);
CspParameters CspParameters = new CspParameters();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048, CspParameters);
byte[] bytes = Encoding.UTF8.GetBytes(data);
RSA.FromXmlString(netKey);
byte[] sign = RSA.SignData(bytes, "SHA256");
return Convert.ToBase64String(sign);
}
public static bool RsaPubVerifySign(string contentForSign, string signedData, string privatekey)
{
//转换成适用于.Net的秘钥
var netKey = RSAKeyConvert.RSAPrivateKeyJava2DotNet(privatekey);
CspParameters CspParameters = new CspParameters();
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048, CspParameters);
byte[] bytes = Encoding.UTF8.GetBytes(contentForSign);
RSA.FromXmlString(netKey);
var datas = Convert.FromBase64String(signedData);
var res = RSA.VerifyData(bytes, "SHA256", datas);
return res;
}
/ <summary>
/ 公钥签名
/ </summary>
/ <param name="contentForSign"></param>
/ <param name="PublicKey"></param>
/ <returns></returns>
//public static string SignsPublic(string contentForSign, string PublicKey)
//{
// //转换成适用于.Net的秘钥
// var netKey = RSAPublicKeyJava2DotNet(PublicKey);
// string encryptedContent = string.Empty;
// using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
// {
// rsa.FromXmlString(netKey);
// byte[] encryptedData = rsa.Encrypt(Encoding.Default.GetBytes(contentForSign), false);
// encryptedContent = Convert.ToBase64String(encryptedData);//注意
// }
// return encryptedContent;
//}
/// <summary>
/// 私钥签名
/// </summary>
/// <param name="contentForSign"></param>
/// <param name="privateKey"></param>
/// <returns></returns>
//public static string Signs(string contentForSign, string privateKey)
//{
// //转换成适用于.Net的秘钥
// var netKey = RSAPrivateKeyJava2DotNet(privateKey);
// var rsa = new RSACryptoServiceProvider();
// rsa.FromXmlString(netKey);
// //创建一个空对象
// var rsaClear = new RSACryptoServiceProvider();
// var paras = rsa.ExportParameters(true);
// rsaClear.ImportParameters(paras);
// //签名返回
// using (var sha256 = new SHA256CryptoServiceProvider())
// {
// var signData = rsa.SignData(Encoding.UTF8.GetBytes(contentForSign), sha256);
// return Convert.ToBase64String(signData);
// }
//}
//public static bool RsaVerifySign(string contentForSign, string signedData, string pubKey)
//{
// //转换成适用于.Net的秘钥
// var netKey = RSAPublicKeyJava2DotNet(pubKey);
// RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider();
// //私钥签名
// RSACryptoServiceProvider oRSA3 = new RSACryptoServiceProvider();
// oRSA3.FromXmlString(netKey);
// byte[] AOutput = oRSA3.SignData(messagebytes, "SHA1");
// //公钥验证
// RSACryptoServiceProvider oRSA4 = new RSACryptoServiceProvider();
// oRSA4.FromXmlString(publickey);
// bool bVerify = oRSA4.VerifyData(messagebytes, "SHA1", AOutput);
//}
}