且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中填写RSAParameters值

更新时间:2023-02-15 15:18:43

您遇到的问题是客户提供给您的XML格式不符合反序列化为 RSAParameters

The problem that you have is that the XML that your customer has given you is not in the format required to deserialize to an object of type RSAParameters

我已经运行了这段代码,以显示XML序列化器生成的XML的样子

I've run this code to show what the XML generated by the XML serializer looks like

var provider = new RSACryptoServiceProvider();

var parameters = provider.ExportParameters(true);

var x = new XmlSerializer(parameters.GetType());
x.Serialize(Console.Out, parameters);
Console.WriteLine();

它生成的输出类似于:

<RSAParameters>
  <Exponent>AQAB</Exponent>
  <Modulus>ruCEpD3XnR...g/waE=</Modulus>
  <P>90amUU3dDazsqN9+...jJUQ==</P>
  <Q>tQv5hGehNLLmv4aC...NfUQ==</Q>
  <DP>azJiiZ6itPoBQph...zBcQ==</DP>
  <DQ>OmewiOw9bxi/o82...f44Q==</DQ>
  <InverseQ>wNohk0NNl...YDg==</InverseQ>
  <D>fNOOWp46FckcvtI+...PpXAE=</D>
</RSAParameters>

其中...被截断的输出.您的客户提供的内容看起来像是该内容的超集(密​​钥,文本和密码不在参数列表中),但格式略有不同.

where the ... is truncated output. What your customer has supplied looks like a superset of that (key, text and cipher are not in the parameter list) but the format is a bit different.

您可以要求他们以完全所需的格式提供数据,然后从中进行序列化;或者,您可以接受其格式,将其反序列化为XML,然后通过将XML内容映射到 RSAParameters 对象上的相应字段来手动构建 RSAParameters 对象.您还需要弄清楚他们想要使用密钥,文本和密码数据做什么,因为这些将在此过程中丢失.

You can either ask them to supply the data in exactly the required format then serialize from that; or you can accept their format, deserialize it to XML and build the RSAParameters object manually by mapping the XML content to the appropriate fields on the RSAParameters object. You also need to work out what it is that they want to do with the key, text and cipher data as these will be lost in this process.