How can I populate RSAParameters value in C #

I used the encryption code below in my project and everything worked fine.

RSACryptoServiceProvider x_alg = new RSACryptoServiceProvider( ); // export only the public key RSAParameters x_public_params = x_alg.ExportParameters(false); // export the private key RSAParameters x_private_params = x_alg.ExportParameters(true); 

Now the client has changed this requirement, and he wants to save all the RSAParameters value in the configuration file and is presented below for demonstration

 <project name="netCard Server1"> <key length="256"></key> <D length="64">00000000000000000000000000000000000000000000000000000000000019C5</D> <DP length="32">00000000000000000000000000000061</DP> <DQ length="32">00000000000000000000000000000065</DQ> <Exponent length="6">000DCD</Exponent> <InverseQ length="32">0000000000000000000000000000003B</InverseQ> <Modulus length="64">0000000000000000000000000000000000000000000000000000000000002C95</Modulus> <P length="32">00000000000000000000000000000065</P> <Q length="32">00000000000000000000000000000071</Q> <text length ="64">0123456789ABCDEF111111111111111125FE2222222222222233333333334444</text> <cipher length ="64">0000000000000000000000000000000000000000000000000000000000000000</cipher> </project> 

Now the problem is that when I import the RSAParameters value, I get a Bad Data exception p>

+4
c # rsa public-key-encryption
source share
2 answers

The problem you have is that the XML provided by your client does not match the format required for deserialization for an object of type RSAParameters

I ran 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(); 

The result that it creates looks something like this:

 <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 ... is the truncated conclusion. What your client has provided looks like a superset (key, text and cipher are not included in the parameter list), but the format is slightly different.

You can either ask them to provide the data in the exact format you want, and then serialize; or you can accept their format, deserialize it in XML, and create the RSAParameters object manually by matching the XML contents with the corresponding fields in the RSAParameters object. You also need to figure out what they want to do with key, text, and encrypted data, as they will be lost in the process.

+5
source share

From the sample structure you provided, it looks like there is additional data that you can (or cannot) provide.

  • create wrapper class
  • Shell properties for invoking a transform function to convert Base64 to Hex
  • XmlElement Attributes for Output Format Control
  • Cipher and Text are not in RSAProperties, so the client will need to specify them for you

    [XmlRoot ("Project")] public class RSAWrapper {[XmlIgnore] public RSAParameters RsaWrap {get; set;}

     // replicate Key for Text and Cipher, subject to client specs private LenghtyValue _key = null; [XmlElement] public LenghtyValue Key{ get{ return (_key!=null) ? _key.Value : null;} set{ _key = (value!=null) ? new LenghtyValue { Value = value} : null;} } // replicate Exponent for D, DP, DQ, InverseQ, Modulus, P and Q [XmlElement] public LenghtyValue Exponent{ get{ return new LenghtyValue { Value = ToHexFromB64(RsaWrap.Exponent);} // look up how to convert this } set {} } public class LenghtyValue{ [XmlText] public string Value{get;set;} [XmlAttribute("length")] public int Length {get{ return (""+Value").Length;} set{}} } 

    }

// then use the class above as such: .... RSACryptoServiceProvider x_alg = new RSACryptoServiceProvider ();

 RSAParameters x_public_params = x_alg.ExportParameters(false); // or true RSAWrapper wrapForClient = new RSAWrapper { RsaWrap = x_public_params, Key = "1024", // or whatever size you have Cipher = "???", // whatever this field means per client specs Text = "???", // whatever this field means per client specs } // with simplifications.... XmlSerializer xser = new XmlSerializer(typeof(RSAWrapper)); xser.Serialize(File.Create(yourFileName), wrapForClient); 
+2
source share

All Articles