如何在C#中加载RSA公钥并使用它来加密某些东西?

时间:2015-05-05 13:46:47

标签: c# python-2.7 rsa public-key-encryption public-key

我创建了一个简单的脚本,它加载RSA公钥来加密Python中的字符串: (P.S.我使用this RSA库)

import rsa
rsa_key = "81474a6ab9a4b69aad47d8674e1de67130dd004eb532dcce7c1fc874e0b078ad2eeb88ae72066d018ad614e18f8e2d411e6ac0f483fb7b6877fe814583dfea02056f04db34185bc7ecfe31ef2297e5f282575300c3f7497823ace8e6240e6f611cf3b4c8bfabf9e4092dd00dc66d5fa35cdb14f3f0bcff2c6c0c9b4ecc3aea2822583fef359200580e8495a152e7cb7aca4de7da719f"
e = "10001"
pub_key = rsa.PublicKey(int(rsa_key,16), int(e,16))
crypto = rsa.encrypt("message", pub_key).encode("hex")

那么我怎样才能在C#中做同样的事情呢?

1 个答案:

答案 0 :(得分:0)

您可以使用System.Security.Cryptography中的RSACryptoServiceProvider。

var rsa = new RSACryptoServiceProvider();
var enc = new ASCIIEncoding();

byte[] data = enc.GetBytes("message");
byte[] encrypted = rsa.Encrypt(data, true);
byte[] decrypted = rsa.Decrypt(encrypted, true);

Console.WriteLine(enc.GetString(decrypted));

有关更多信息和示例设置公钥: https://social.msdn.microsoft.com/Forums/en-US/67b50472-b23f-4191-b8f4-e4a696fca5e4/how-to-use-a-public-key-with-rsacryptoserviceprovider-class?forum=csharplanguage