怎么样用C#实现RSA加密?UP有分

laojievip laojievip 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
流程是这样的,我先需要在证书里面读出公钥(已经实现),然后把需要加密的数据和公钥放到RSA里面加密。现在实现了在证书里提取公钥,但是我不知道怎么把这个公钥放到RSA里面去加密。以下是我在MSDN里看的代码,里面没有描述怎么用公钥对数据加密,请高手指点,如果有朋友做这个加密方面的可以加我QQ:1158281,大家一起讨论下。

static public byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo, bool DoOAEPPadding)
{
try
{
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

RSA.ImportParameters(RSAKeyInfo);



RijndaelManaged RM = new RijndaelManaged();
return RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
}

catch(CryptographicException e)
{
Console.WriteLine(e.Message);

return null;
}

}
static public byte[] RSADecrypt(byte[] DataToDecrypt, RSAParameters RSAKeyInfo,bool DoOAEPPadding)
{
try
{
//Create a new instance of RSACryptoServiceProvider.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

//Import the RSA Key information. This needs
//to include the private key information.
RSA.ImportParameters(RSAKeyInfo);

//Decrypt the passed byte array and specify OAEP padding.
//OAEP padding is only available on Microsoft Windows XP or
//later.
return RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}
//Catch and display a CryptographicException
//to the console.
catch(CryptographicException e)
{
Console.WriteLine(e.ToString());

return null;
}

}
private void button1_Click(object sender, System.EventArgs e)
{
string certfile = @"e:\srvcert.crt";

X509Certificate cert = X509Certificate.CreateFromCertFile(certfile);
byte[] PublicKey = cert.GetPublicKey();//证书公钥
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
byte[] privatekey = Encoding.Default.GetBytes("aaaaaa");

byte[] dataToEncrypt = Encoding.Default.GetBytes("Data to Encrypt");
byte[] encryptedData;
byte[] decryptedData;

encryptedData = RSAEncrypt(dataToEncrypt,RSA.ExportParameters(true), false);

decryptedData = RSADecrypt(encryptedData,RSA.ExportParameters(true), false);


}
...全文

相关推荐

国际财务报告准则袖珍手册(制度范本、PDF格式).pdf

国际财务报告准则袖珍手册(制度范本、PDF格式).pdf

成长之路514

袖珍焊工手册

介绍了关于袖珍焊工手册的详细说明,提供机械类的技术资料的下载。

weixin_38686041

pouchdb, PouchDB是一个袖珍大小的数据库.zip

pouchdb, PouchDB是一个袖珍大小的数据库 PouchDB同步同步的数据库 ! PouchDB是一个开源JavaScript数据库,受 Apache的启发,它被设计为在浏览器内部运行良好。PouchDB是为了帮助web开发人员建立在线工作的应用程序,以便在线

weixin_38743737

pouchdb:-PouchDB是一个袖珍数据库

–同步的数据库! PouchDB是一个受启发的开源JavaScript数据库,旨在在浏览器中良好运行。 PouchDB的创建是为了帮助Web开发人员构建脱机工作以及联机工作的应用程序。 使用PouchDB 要开始使用PouchDB,请查看和。 获得帮助 PouchDB社区 , 以及处于活动状态。 或者您可以 ! 如果您认为自己在PouchDB中发现了一个错误,请编写可复制的测试用例并提出。 您可以从的开始。 预发行版本 如果您希望生活在最前沿,可以使用以下步骤从源代码构建PouchDB: git clone https://github.com/pouchdb/pouchdb.git

log边缘

Dialog半导体有限公司打造全球最小的低功耗超极本适配器.pdf

Dialog半导体有限公司打造全球最小的低功耗超极本适配器.pdf

数据资源
评论 25
收藏 已收藏
分享
返回顶部
laojievip
C# 111,128 成员 642,536 内容