C# RSA FromXmlString() BadData Exception[英] C# RSA FromXmlString() BadData Exception

本文是小编为大家收集整理的关于C# RSA FromXmlString() BadData Exception的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

我生成这样的键:

ssh -keygen -t rsa1 -b 768 -c test

我得到了一个看起来像:

的公钥

768 65537 1244818534536334574875801623177648392314095757825816788136360615069738317432684324135107640137961040160008388231420349935694006787032419425836353385388446738225464450963417153771331511902010734528761152834146019053540579969112124269 测试

我在导入公钥方面遇到问题.据我所知,以下应该有效. Baddata Crypto异常,对FrofXMlString()的呼吁失败.我不确定我在做什么错.

string rsaKeyValue = "<RSAKeyValue>";
rsaKeyValue += "<Modulus>";
rsaKeyValue += Convert.ToBase64String(Encoding.ASCII.GetBytes(openSSHKeySplit[2]));
rsaKeyValue += "</Modulus>";
rsaKeyValue += "<Exponent>";
rsaKeyValue += Convert.ToBase64String(Encoding.ASCII.GetBytes(openSSHKeySplit[1]));
rsaKeyValue += "</Exponent>";
rsaKeyValue += "</RSAKeyValue>";                
mRSAContext.FromXmlString(rsaKeyValue); // This throws a BadData Crypto Exception

推荐答案

您需要将数字解释为实际数字,而不是十进制ASCII数字流.例如,对于指数,您当前正在获取ASCII字节流的基本64(0x36 0x35 0x35 0x33 0x37),而您应该将其转换为具有int.parse(" 65537")的整数,然后获取在传递到base64编码器之前,使用bitConverter.getBytes()字节数组.模量比适合标准整数的要大一些.您可以尝试使用System.Numerics的BigInteger类.即,biginteger.parse("");

注意,您不必自己制作XML字符串.我相信您可以将rsacryptoserviceprovider和rsaparameters对象以及实现相同的目标使用.

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
RSAParameters parameters = new RSAParameters();
parameters.Modulus = mod; // Your calculated modulus
parameters.Exponent = exp; // Your calculated exponent
rsa.ImportParameters(parameters);

本文地址:https://www.itbaoku.cn/post/2352688.html

问题描述

I generate a key like so:

ssh-keygen -t rsa1 -b 768 -C Test

I get a public key that looks like:

768 65537 1244818534536334574875801623177648392314095757825816788136360615069738317432684324135107640137961040160008388231420349935694006787032419425836353385388446738225464450963417153771331511902010734528761152834146019053540579969112124269 Test

I'm having issues with importing a public key. As far as I'm aware the below should work. The call to FromXmlString() fails with BadData crypto exception. I'm not sure what I'm doing wrong.

string rsaKeyValue = "<RSAKeyValue>";
rsaKeyValue += "<Modulus>";
rsaKeyValue += Convert.ToBase64String(Encoding.ASCII.GetBytes(openSSHKeySplit[2]));
rsaKeyValue += "</Modulus>";
rsaKeyValue += "<Exponent>";
rsaKeyValue += Convert.ToBase64String(Encoding.ASCII.GetBytes(openSSHKeySplit[1]));
rsaKeyValue += "</Exponent>";
rsaKeyValue += "</RSAKeyValue>";                
mRSAContext.FromXmlString(rsaKeyValue); // This throws a BadData Crypto Exception

推荐答案

You need to interpret the numbers as actual numbers instead of a stream of decimal ascii digits. For instance, for the exponent, you're currently getting the base64 of the stream of ascii bytes (0x36 0x35 0x35 0x33 0x37), whereas you should convert it to an integer with int.Parse("65537"), and then get the byte array using BitConverter.GetBytes() before passing to the base64 encoder. The modulus is a bit trickier since it's larger than will fit into a standard integer. You could try the BigInteger class from System.Numerics. Ie, BigInteger.Parse("");

Note, you don't have to make your own XML string. I believe you can use the RSACryptoServiceProvider along with an RSAParameters object to accomplish the same goal.

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
RSAParameters parameters = new RSAParameters();
parameters.Modulus = mod; // Your calculated modulus
parameters.Exponent = exp; // Your calculated exponent
rsa.ImportParameters(parameters);