C#RSA在BouncyCastle的帮助下使用给定的公钥加密文本



这是我在Stackoverflow上的第一篇文章,因此我将感谢任何关于如何改进的建议。

我在这里尝试了以下内容因此,我试图在BouncyCastle的帮助下,使用Microsoft RSACryptoServiceProvider对字符串消息进行编码,让它进行"测试"。

我有一个给定的公钥,服务器提供商已经向我描述了公钥的结构。

公钥:48015250010000010100970348b03e911dcce5ed8f555c2116dbc4d7e96d4c1cdc4bbbaad26baa54b5c834f604f9dfb391459459772fb51d00afd0fe3a9b2da724e62113a9e8c95bef377cb5fcf7fe2e5282a0da50f01d5d2635d958f9836cfb4f8b616777c0cf67db9a553a679e321972e4d4f4f33ded057cb690417a3b42fbfce2ad9fdd80c815af1ec796d4e a2f17954e4fad08e3e0397fa34122ac5951d889b06359a401e5506e50fa176b5a77fab84e25cfcdbf2330aa173da1156c8b79d6db6bfae828b00811183e63f137648e1fc1786b52d815c248b添加了6a17c941414f67a23adf82fe76196b64b96e36f8604fa00e8e357f5e6c83b992d622d5e9cd9c1d00000003010001

公钥是一个六进制字符串,我提取了单独变量中的模和指数,并将它们保存为Base64字符串。模数和指数作为xml字符串保存在另一个变量中,用作RSAKeyValue。

string strModulusAndExponentAsXml = "<RSAKeyValue><Modulus>00970348B03E911DCCE5ED8F555C2116DBC4D7E96D4C1CDC4BBBAAD26BAA54B5C834F604F9DFB391459459772FB51D00AFD0FE3A9B2DA724E62113A9E8C95BEF377CB5FCF7FEBE42E5282A0DA50F01D5D2635DD958F9836CFB4F8B616777C0CF67DB9A5530AD679E321972E4D4F4F33DED057CB690417A3B42FBFCE2AD9FDD80C815AF1EC858C796D4EA2F17954E4BFAD08E3E0397FA34122AC5951D889B06359A401E5506E50FA176B5A77FAB84E25CFCDBF2330AA173DA1156C8B79D6DB6BFAE828B00811183E63F137648E1FC1786B52D815C248BCADDDF6A17C941414F67A23ADFE82FE76196B64B96E36F8604FA00E8E357F5AE6C83B992D622D5E9CD9C1D</Modulus><Exponent>010001</Exponent></RSAKeyValue>";
string strModulusAndExponentAsBase64 = Base64Encode(strModulusAndExponentAsXml);

现在我想创建一个Asn1Object并提供字节参数

Asn1Object obj = Asn1Object.FromByteArray(Convert.FromBase64String(strModulusAndExponentAsBase64));

在这里它失败了,我得到了错误:

System.IO.IOException:"遇到未知标记28">

有人知道我做错了什么吗,所以我可以用给定的指数和模量创建Asn1Ojbect?如果有什么不清楚的地方,请告诉我。

如果您想检查您的加密货币,请不要将其公开。你应该尝试一种不同的方法:

if (algValue != null)
{
algValue.Clear();
}
else
{
throw new Exception("No TripleDES key was found to clear.");
}
}
public void Encrypt(string Element)
{
// Find the element by name and create a new
// XmlElement object.
XmlElement inputElement = docValue.GetElementsByTagName(Element)[0] as XmlElement;
// If the element was not found, throw an exception.
if (inputElement == null)
{
throw new Exception("The element was not found.");
}
// Create a new EncryptedXml object.
EncryptedXml exml = new EncryptedXml(docValue);
// Encrypt the element using the symmetric key.
byte[] rgbOutput = exml.EncryptData(inputElement, algValue, false);

这是你应该尝试的方法。

最新更新