Serializing RSAParameters issues

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric

    #1

    Serializing RSAParameters issues

    Hello,

    I am wanting to serialize (for storage of an RSA Keypair) a RSAParameters
    class. It seems that even if i export the private portions of the Key, they
    are never serialized and stored in the file. Only the public portions of the
    key are (you can see this by opening up the serialtest.xml file written).

    My question is: Am I just missing something very simple here or do I have to
    write a custom class to turn a RSAParameters into a completely serializable
    one?

    I might have to do the second one anyways, so its not a big deal if I have
    to (gotta encrypt the private portions of the key, would make sense to just
    support this in its class) but if I really have to, id like to understand
    why its doing this the way it is.

    Ive been using C# for a while, but I have not used serialization yet. so I
    might just be missing something.

    Thank you for your time, and any ideas you can offer =]

    eric.h

    Example Code:
    Console.WriteLi ne("testing serialization.. .");
    Stream fs = new
    FileStream("ser ialtest.xml",Fi leMode.Create,F ileAccess.ReadW rite);
    IFormatter formatter = new SoapFormatter() ;
    RSAParameters rsap = BobRSA.ExportPa rameters(true); //we want the private
    portions
    formatter.Seria lize(fs,rsap); //rsap.D is set, along with all private key
    portions
    fs.Close();

    //deserialize
    Stream nfs = new FileStream("ser ialtest.xml",Fi leMode.Open,Fil eAccess.Read);
    IFormatter defmt = new SoapFormatter() ;
    RSAParameters rp = (RSAParameters) defmt.Deseriali ze(nfs);
    BobRSA.ImportPa rameters(rp); // rp.D is null, all private portions are.
    public Exponet and modulus is set properly
    nfs.Close();


  • Rob Teixeira [MVP]

    #2
    Re: Serializing RSAParameters issues

    Try the following, and you'll see that it is possible.
    I wrote the code in VB because I thought this question was in the VB group,
    but it should be easy enough to figure out the C# conversion.

    Dim rs As New System.Security .Cryptography.R SACryptoService Provider(1024)

    Dim r As System.Security .Cryptography.R SAParameters

    r = rs.ExportParame ters(True)

    Dim ms As New System.IO.Memor yStream

    Dim x As New Xml.Serializati on.XmlSerialize r(r.GetType)

    x.Serialize(ms, r)

    ms.Flush()

    MsgBox(System.T ext.Encoding.AS CII.GetString(m s.ToArray()))

    ms.Close()

    -Rob [MVP]
    Do not send questions to my email. There will be no response. Please post
    comments, responses, and requests to the newsgroup so everyone can benefit
    from the discussion.

    "Eric" <mrunknown@post mark.no.spam.pl z.net> wrote in message
    news:O0WvGvXVDH A.2192@TK2MSFTN GP10.phx.gbl...[color=blue]
    > Hello,
    >
    > I am wanting to serialize (for storage of an RSA Keypair) a RSAParameters
    > class. It seems that even if i export the private portions of the Key,[/color]
    they[color=blue]
    > are never serialized and stored in the file. Only the public portions of[/color]
    the[color=blue]
    > key are (you can see this by opening up the serialtest.xml file written).
    >
    > My question is: Am I just missing something very simple here or do I have[/color]
    to[color=blue]
    > write a custom class to turn a RSAParameters into a completely[/color]
    serializable[color=blue]
    > one?
    >
    > I might have to do the second one anyways, so its not a big deal if I have
    > to (gotta encrypt the private portions of the key, would make sense to[/color]
    just[color=blue]
    > support this in its class) but if I really have to, id like to understand
    > why its doing this the way it is.
    >
    > Ive been using C# for a while, but I have not used serialization yet. so I
    > might just be missing something.
    >
    > Thank you for your time, and any ideas you can offer =]
    >
    > eric.h
    >
    > Example Code:
    > Console.WriteLi ne("testing serialization.. .");
    > Stream fs = new
    > FileStream("ser ialtest.xml",Fi leMode.Create,F ileAccess.ReadW rite);
    > IFormatter formatter = new SoapFormatter() ;
    > RSAParameters rsap = BobRSA.ExportPa rameters(true); //we want the private
    > portions
    > formatter.Seria lize(fs,rsap); //rsap.D is set, along with all private key
    > portions
    > fs.Close();
    >
    > //deserialize
    > Stream nfs = new[/color]
    FileStream("ser ialtest.xml",Fi leMode.Open,Fil eAccess.Read);[color=blue]
    > IFormatter defmt = new SoapFormatter() ;
    > RSAParameters rp = (RSAParameters) defmt.Deseriali ze(nfs);
    > BobRSA.ImportPa rameters(rp); // rp.D is null, all private portions are.
    > public Exponet and modulus is set properly
    > nfs.Close();
    >
    >[/color]


    Comment

    • Eric

      #3
      Re: Serializing RSAParameters issues

      Hello Rob,

      Thank you for the code. I did convert it to C# and it does work.
      Unfortuneately, that doesnt help me as to why SoapFormatter refuses to work.
      Try my code out, you will see it will not work (at least it shouldn't). Is
      there something wrong with my code? I can not seem to find it.

      Thank you for your help.


      Comment

      Working...