RE: RSA Encryption: Saving keys as files, and size of encrypted data



Hi,

1) Here's an easy example how you can generate a new key pair and save each
key in a xml file.

private static void GenerateKeys(string pubFile, string privateFile)
{
// Create a new RSA signing key and save it in the container.
RSA rsaKey = RSA.Create();

// Write the public and private key in files.
StringToFile(pubFile, rsaKey.ToXmlString(false));
StringToFile(privateFile, rsaKey.ToXmlString(true));
}

private static void StringToFile(string outfile, string data)
{
// Write the data into the file.
StreamWriter streamWriter = System.IO.File.CreateText(outfile);
streamWriter.Write(data);
streamWriter.Close();
}

2) The asymmetric encryption is not suitable for the encryption of large
data. Use the RSA key pair to encrypt a symmetric encryption key.

Regards,
Dominik
-----------------------------
http://blogs.ecreation.ch


"Gary Bond" wrote:

HI All,

Could I ask for some help with RSACryptoServiceProvider class. I am trying
to write 3 small apps to demonstrate RSA encryption, and giving out a public
key as an xml string. One app makes the xml key strings and saves them as
files, and the other 2 apps encrypt and decrypt, using those xml strings.

1) I make the keys like this, using code from MSDN

Dim cp As CspParameters = New CspParameters()
cp.Flags = CspProviderFlags.UseMachineKeyStore
Dim RSA As RSACryptoServiceProvider = New
RSACryptoServiceProvider(2048, cp)
Dim PubKey as string = RSA.ToXmlString(False)
Dim PrivateKey as string = RSA.ToXmlString(True)
'subsequently save these strings to file

Do I need to initialise the CspParameters and use it in the creation of the
RSACryptoServiceProvider? Could I just use the parameterless constructor on
the RSACryptoServiceProvider to give me a new instance, and then save the
public + private xml keystrings, like this

Dim RSA As RSACryptoServiceProvider = New
RSACryptoServiceProvider(2048)


If I have to use the CspParameters, since I am saving the xml strings for
later use, do I have to set the UseMachineKeyStore? Would this code work if I
move the exe's to another machine in either case. The idea of these apps is
that I give out the public key string file and the encryptor, someone else
encrypts some stuff for me at a remote location, and then I decrypt the file
they send me with my private key.


2) When encrypting, if I try to encrypt a large string of 'stuff', I get a
cryptographic error "Key not valid for use in specified state". Heres the
code from the function:

Dim RSA As New RSACryptoServiceProvider
RSA.FromXmlString(XMLKey)
Return RSA.Encrypt(DataToEncrypt, False)


where XMLKey is the public xml key string from point 1) above, previously
saved and re-opened on my hard drive.

If I encrypt a small amount of data all goes well and I can decrypt later on
with

Dim RSA As New RSACryptoServiceProvider()
RSA.FromXmlString(XMLKey)
ClearTxt = RSA.Decrypt(BytesToDecrypt, False)
Return Encoding.ASCII.GetString(ClearTxt)

As you can tell I am very inexperienced with encryption techniques, so I
apologise if these are trivial questions.

Thanks for any help.

regards,
Gary
.



Relevant Pages

  • Re: Encrytion causing error
    ... I am passing the encrypted text as a string. ... am assuming it can't convert it to XML. ... >> Here is my encryption function. ... >> Any ideas of how pass encrypted data as XML without XML illegal character ...
    (microsoft.public.dotnet.framework.aspnet)
  • webservice xsd / xml validator?
    ... I have a web method that doesvaildation but does not return just the xml ... Imports System.Web ... Private navigator As XPathNavigator ... Private strLine As String ...
    (microsoft.public.dotnet.xml)
  • Re: Encrytion causing error
    ... Not sure if an encrypted over Xml through a web service would break it. ... Just a thought - as web services are http based, maybe the encryption it ... Maybe give UrlEncoding the string a try. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Continually-running Applet has memory leaks - how do I plug them?
    ... same character and copying the whole string on every replace. ... * Add entity value to the map. ... * Expand known entities in an xml string. ... private static final Map standardEntities = new HashMap; ...
    (comp.lang.java.help)
  • Re: Continually-running Applet has memory leaks - how do I plug them?
    ... same character and copying the whole string on every replace. ... * Add entity value to the map. ... * Expand known entities in an xml string. ... private static final Map standardEntities = new HashMap; ...
    (comp.lang.java.programmer)