C#RSA FromXmlString()BadData异常
我生成一个密钥,如下所示: ssh-keygen-trsa1-b768-C测试 我得到的公钥如下所示: 76865537 1244818534536334574875801623177648392314095757825816788136360615069738317432684324135107640137961040160008388231420349935694006787032419425836353385388446738225464450963417153771331511902010734528761152834146019053540579969112124269 试验 我在导入公钥时遇到问题。就我所知,下面的方法应该有效。调用FromXmlString()失败,BadData加密异常。我不确定我做错了什么C#RSA FromXmlString()BadData异常,c#,rsa,C#,Rsa,我生成一个密钥,如下所示: ssh-keygen-trsa1-b768-C测试 我得到的公钥如下所示: 76865537 124481853453633457487580162317764839231409575782581678813636061506973831743268432413510764013796104016000838823142034993569400678703241942583635338538844673822546445096341715377133151190201
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
字符串rsaKeyValue=”“;
rsaKeyValue+=“”;
rsaKeyValue+=Convert.ToBase64String(Encoding.ASCII.GetBytes(opensshkeyspilt[2]);
rsaKeyValue+=“”;
rsaKeyValue+=“”;
rsaKeyValue+=Convert.ToBase64String(Encoding.ASCII.GetBytes(opensshkeyspilt[1]);
rsaKeyValue+=“”;
rsaKeyValue+=“”;
mRSAContext.FromXmlString(rsaKeyValue);//这会引发BadData加密异常
您需要将数字解释为实际数字,而不是十进制ascii数字流。例如,对于指数,您当前正在获取ascii字节流的base64(0x36 0x35 0x35 0x33 0x37),而您应该使用int.Parse(“65537”)将其转换为整数,然后在传递到base64编码器之前使用BitConverter.GetBytes()获取字节数组。模数有点复杂,因为它比标准整数大。您可以尝试System.Numerics中的BigInteger类。Ie,BigInteger.Parse(“”)
注意,您不必创建自己的XML字符串。我相信您可以使用rsacryptserviceprovider和rsapameters对象来实现相同的目标
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
RSAParameters parameters = new RSAParameters();
parameters.Modulus = mod; // Your calculated modulus
parameters.Exponent = exp; // Your calculated exponent
rsa.ImportParameters(parameters);
我是否可以建议使用实际的Xml解析器/生成器,而不是创建自己的Xml字符串?建立自己的是一个可怕的想法,可能会导致许多软件错误。哇,我觉得自己像个白痴。我会尝试一下,然后再报告。就是这样,我不知道为什么我之前没有想到这一点。谢谢