110,571
社区成员
发帖
与我相关
我的任务
分享
RSAParameters priPara ;
priPara = getPrivateKeyFromPem(priFileName);
RSA = new RSACryptoServiceProvider();
//RSA.FromXmlString(NET_PRIVATE_KEY);
RSAParameters RSAParams = RSA.ExportParameters(false);
RSACryptoServiceProvider RSA2 = new RSACryptoServiceProvider();
RSA2.ImportParameters(priPara);
byte[] data = Encoding.UTF8.GetBytes(dataToBeSigned);
byte[] endata = RSA2.SignData(data, "SHA1");
return Convert.ToBase64String(endata);
/// <summary>
/// RSA加解密
/// </summary>
public class RSA
{
/// <summary>
/// RSA加密
/// </summary>
/// <param name="xmlPublicKey">私钥</param>
/// <param name="encryptString">需要加密的数据</param>
/// <returns>RSA公钥加密后的数据</returns>
public static string RSAEncrypt(string xmlPublicKey, string encryptString)
{
string result;
try
{
RSACryptoServiceProvider.UseMachineKeyStore = true;
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(xmlPublicKey);
byte[] bytes = new UnicodeEncoding().GetBytes(encryptString);
result = Convert.ToBase64String(provider.Encrypt(bytes, false));
}
catch (Exception exception)
{
throw exception;
}
return result;
}
/// <summary>
/// RSA解密
/// </summary>
/// <param name="xmlPrivateKey">公钥</param>
/// <param name="decryptString">需要解密的数据</param>
/// <returns>解密后的数据</returns>
public static string RSADecrypt(string xmlPrivateKey, string decryptString)
{
string result;
try
{
RSACryptoServiceProvider.UseMachineKeyStore = true;
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
provider.FromXmlString(xmlPrivateKey);
byte[] rgb = Convert.FromBase64String(decryptString);
byte[] buffer2 = provider.Decrypt(rgb, false);
result = new UnicodeEncoding().GetString(buffer2);
}
catch (Exception exception)
{
throw exception;
}
return result;
}
/// <summary>
/// 生成公钥、私钥
/// </summary>
/// <param name="PrivateKeyPath">私钥文件保存路径,包含文件名</param>
/// <param name="PublicKeyPath">公钥文件保存路径,包含文件名</param>
public static void CreateKey(out string privateKey, out string publicKey)
{
RSACryptoServiceProvider.UseMachineKeyStore = true;
RSACryptoServiceProvider provider = new RSACryptoServiceProvider();
privateKey = provider.ToXmlString(true);
publicKey = provider.ToXmlString(false);
}
}