我已经使用 RSACryptoServiceProvider 加密了一个文本。我导出了公钥和私钥。显然我只是想在解码器应用程序中公开公钥,所以我编写了如下代码:
private const string PublicKey = "<RSAKeyValue><Modulus>sIzQmj4vqK0QPd7RXKigD7Oi4GKPwvIPoiUyiKJMGP0qcbUkRPioe2psE/d3c1a2NY9oj4Da2y1qetjvKKFad2QAhXuql/gPIb1WmI+f6q555GClvHWEjrJrD/ho7SLoHbWd6oY6fY609N28lWJUYO97RLVaeg2jfNAUSu5bGC8=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
private string Decrypt()
{
byte[] encryptedKeyAsBytes = Convert.FromBase64String(_encryptedKey);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(PublicKey);
// read ciphertext, decrypt it to plaintext
byte[] plainBytes = rsa.Decrypt(encryptedKeyAsBytes, false);
string plainText = System.Text.Encoding.ASCII.GetString(plainBytes);
return plainText;
}
但在“byte[] plainBytes = rsa.Decrypt(encryptedKeyAsBytes, false);”行抛出异常 并说“钥匙不存在”。但是,如果我公开整个私钥和公钥,那么它会运行得很愉快。那么如何仅使用公钥信息解密数据呢?