95 lines
4.2 KiB
C#
95 lines
4.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AllBase
|
|
{
|
|
class RsaBase
|
|
{
|
|
|
|
/// <summary>
|
|
/// RSA签名
|
|
/// </summary>
|
|
/// <param name="privateKeyJava">私钥</param>
|
|
/// <param name="data">待签名的内容</param>
|
|
/// <returns></returns>
|
|
public static string RSASignJava(string data, string privateKeyJava, string hashAlgorithm = "MD5", string encoding = "UTF-8")
|
|
{
|
|
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
|
|
//rsa.FromXmlString();
|
|
//rsa.FromPrivateKeyJavaString(privateKeyJava);//加载私钥
|
|
//RSAPKCS1SignatureFormatter RSAFormatter = new RSAPKCS1SignatureFormatter(rsa);
|
|
////设置签名的算法为MD5 MD5withRSA 签名
|
|
//RSAFormatter.SetHashAlgorithm(hashAlgorithm);
|
|
|
|
|
|
var dataBytes = Encoding.GetEncoding(encoding).GetBytes(data);
|
|
var HashbyteSignature = rsa.SignData(dataBytes, hashAlgorithm);
|
|
return Convert.ToBase64String(HashbyteSignature);
|
|
|
|
//byte[] HashbyteSignature = ConvertToRgbHash(data, encoding);
|
|
|
|
//byte[] dataBytes =Encoding.GetEncoding(encoding).GetBytes(data);
|
|
//HashbyteSignature = rsa.SignData(dataBytes, hashAlgorithm);
|
|
//return Convert.ToBase64String(HashbyteSignature);
|
|
//执行签名
|
|
//EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
|
|
//return Convert.ToBase64String(RSAFormatter.CreateSignature(HashbyteSignature));
|
|
//return result.Replace("=", String.Empty).Replace('+', '-').Replace('/', '_');
|
|
}
|
|
//RSA加密,随机生成公私钥对并作为出参返回
|
|
static public string RSA_Encrypt(string str_Plain_Text, out string str_Public_Key, out string str_Private_Key)
|
|
{
|
|
str_Public_Key = "";
|
|
str_Private_Key = "";
|
|
UnicodeEncoding ByteConverter = new UnicodeEncoding();
|
|
byte[] DataToEncrypt = ByteConverter.GetBytes(str_Plain_Text);
|
|
try
|
|
{
|
|
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
|
|
str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
|
|
str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
|
|
|
|
//OAEP padding is only available on Microsoft Windows XP or later.
|
|
byte[] bytes_Cypher_Text = RSA.Encrypt(DataToEncrypt, false);
|
|
str_Public_Key = Convert.ToBase64String(RSA.ExportCspBlob(false));
|
|
str_Private_Key = Convert.ToBase64String(RSA.ExportCspBlob(true));
|
|
string str_Cypher_Text = Convert.ToBase64String(bytes_Cypher_Text);
|
|
return str_Cypher_Text;
|
|
}
|
|
catch (CryptographicException e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
//RSA解密
|
|
static public string RSA_Decrypt(string str_Cypher_Text, string str_Private_Key)
|
|
{
|
|
byte[] DataToDecrypt = Convert.FromBase64String(str_Cypher_Text);
|
|
try
|
|
{
|
|
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
|
|
//RSA.ImportParameters(RSAKeyInfo);
|
|
byte[] bytes_Public_Key = Convert.FromBase64String(str_Private_Key);
|
|
RSA.ImportCspBlob(bytes_Public_Key);
|
|
|
|
//OAEP padding is only available on Microsoft Windows XP or later.
|
|
byte[] bytes_Plain_Text = RSA.Decrypt(DataToDecrypt, false);
|
|
UnicodeEncoding ByteConverter = new UnicodeEncoding();
|
|
string str_Plain_Text = ByteConverter.GetString(bytes_Plain_Text);
|
|
return str_Plain_Text;
|
|
}
|
|
catch (CryptographicException e)
|
|
{
|
|
Console.WriteLine(e.ToString());
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|