Serializing RSAParameters issues

E

Eric

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.WriteLine("testing serialization...");
Stream fs = new
FileStream("serialtest.xml",FileMode.Create,FileAccess.ReadWrite);
IFormatter formatter = new SoapFormatter();
RSAParameters rsap = BobRSA.ExportParameters(true); //we want the private
portions
formatter.Serialize(fs,rsap); //rsap.D is set, along with all private key
portions
fs.Close();

//deserialize
Stream nfs = new FileStream("serialtest.xml",FileMode.Open,FileAccess.Read);
IFormatter defmt = new SoapFormatter();
RSAParameters rp = (RSAParameters)defmt.Deserialize(nfs);
BobRSA.ImportParameters(rp); // rp.D is null, all private portions are.
public Exponet and modulus is set properly
nfs.Close();
 
R

Rob Teixeira [MVP]

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.RSACryptoServiceProvider(1024)

Dim r As System.Security.Cryptography.RSAParameters

r = rs.ExportParameters(True)

Dim ms As New System.IO.MemoryStream

Dim x As New Xml.Serialization.XmlSerializer(r.GetType)

x.Serialize(ms, r)

ms.Flush()

MsgBox(System.Text.Encoding.ASCII.GetString(ms.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.
 
E

Eric

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top