在 c# 中使用 RSACryptoServiceProvider 查找公钥和私钥

编程入门 行业动态 更新时间:2024-10-23 13:22:39
本文介绍了在 c# 中使用 RSACryptoServiceProvider 查找公钥和私钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码.

RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(); //Save the public key information to an RSAParameters structure. RSAParameters RSAKeyInfo = RSA.ExportParameters(true); byte[] toEncryptData = Encoding.ASCII.GetBytes("hello world"); byte[] encryptedRSA = RSAEncrypt(toEncryptData, RSAKeyInfo, false); string EncryptedResult = System.Text.Encoding.Default.GetString(encryptedRSA); byte[] decryptedRSA = RSADecrypt(encryptedRSA, RSAKeyInfo, false); string originalResult = System.Text.Encoding.Default.GetString(decryptedRSA); return userDetails.ToString();

当我使用 RSAEncrypt 方法时,它采用参数RSAKeyInfo"(用于加密的公钥和用于解密的私钥).

When I use the RSAEncrypt method it takes the parameter "RSAKeyInfo" (Public key for encryption and Private key for decryption).

如何获取私钥和​​公钥的值,该方法用于加密和解密.

How can I get the value of private and public keys, which this method used for encryption and decryption.

谢谢,

推荐答案

您需要使用 RSA.ToXmlString

下面的代码使用两个不同的 RSA 实例,其中包含一个包含公钥和私钥的共享字符串.要只获取公钥,请使用false 参数,true 参数将返回公钥+私钥.

Code below uses two different RSA instances with a shared string containing public and private keys. To obtain only public key, use a false parameters, true parameter will return public + private key.

class Program { public static void Main(string[] args) { //Encrypt and export public and private keys var rsa1 = new RSACryptoServiceProvider(); string publicPrivateXml = rsa1.ToXmlString(true); // <<<<<<< HERE byte[] toEncryptData = Encoding.ASCII.GetBytes("hello world"); byte[] encryptedRSA = rsa1.Encrypt(toEncryptData, false); string EncryptedResult = Encoding.Default.GetString(encryptedRSA); //Decrypt using exported keys var rsa2 = new RSACryptoServiceProvider(); rsa2.FromXmlString(publicPrivateXml); byte[] decryptedRSA = rsa2.Decrypt(encryptedRSA, false); string originalResult = Encoding.Default.GetString(decryptedRSA); } }

更多推荐

在 c# 中使用 RSACryptoServiceProvider 查找公钥和私钥

本文发布于:2023-11-10 00:47:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1573909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:公钥   RSACryptoServiceProvider

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!