How can I fill in RSAParameters value in c#

后端 未结 2 804
独厮守ぢ
独厮守ぢ 2021-01-05 05:55

I was using below code for encryption in my project and everything was working fine.

RSACryptoServiceProvider x_alg = new RSACryptoServiceProvider(  );

// e         


        
相关标签:
2条回答
  • 2021-01-05 06:26

    From the sample structure you've provided it looks like there is extra data there, which you may (or may not) be able to provide.

    • create a wrapper class
    • wrapper properties to call conversion function to transform the Base64 to Hex
    • XmlElement attributes to control the output format
    • Cipher and Text are not in RSAProperties, so client will have to spec them for you

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

      // replicate Key for Text and Cipher, subject to client's 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);
    
    0 讨论(0)
  • 2021-01-05 06:31

    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

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

    The output that it generates is something like:

    <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.

    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.

    0 讨论(0)
提交回复
热议问题