简体   繁体   English

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

[英]C# RSA Encrypting text using a given public Key with help of BouncyCastle

This is my first post on Stackoverflow so i will appreciate any suggestions on how to improve. 这是我关于Stackoverflow的第一篇文章,因此,我将感谢您对如何改进的任何建议。

I tried the following from here So im trying to encode a string message, let it be "test", with the Microsoft RSACryptoServiceProvider with the help of BouncyCastle. 我从这里尝试了以下操作,因此,我尝试在BouncyCastle的帮助下使用Microsoft RSACryptoServiceProvider对字符串消息进行编码,使其成为“测试”。

I've got a given public key where the server provider already described me how the public key is structured. 我有一个给定的公用密钥,服务器提供者已经在其中描述了公用密钥的结构。

public key: 公钥:

The public key is a hexstring and i extracted the modulus and exponent in seperate variables and saved them as Base64 Strings. 公钥是一个十六进制字符串,我提取了单独变量中的模数和指数,并将它们另存为Base64字符串。 The modulus and exponent are saved in another variable as a xml string for use as the RSAKeyValue. 模数和指数以xml字符串形式保存在另一个变量中,以用作RSAKeyValue。

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

Now i want to create a Asn1Object and provide the byte parameter 现在我想创建一个Asn1Object并提供byte参数

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

and here it fails, i get the error: 在这里失败了,我得到了错误:

System.IO.IOException: 'unknown tag 28 encountered' System.IO.IOException:'遇到未知标签28'

Does anyone know what im doing wrong, so i can create the Asn1Ojbect with the given exponent and modulus? 有谁知道我在做什么错,所以我可以用给定的指数和模数创建Asn1Ojbect吗? Let me know if anything is unclear. 让我知道是否有任何不清楚的地方。

Don't put it on public if you want to check your crypto. 如果要检查您的加密货币,请勿将其公开。 You should try a different method: 您应该尝试其他方法:

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);

this is the method you should try. 这是您应该尝试的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM