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



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: RSA Encrypt/Decrypt Problems
    ... try my Goliath.NET obfuscator (pre-release version) & send me your opinion ... > I am trying to encrypt messages typed in a textbox by encrypting them ... > definite answer as to whether I can use RSA for what I am doing? ... > dim RSA as new RSACryptoServiceProvider() ...
    (microsoft.public.dotnet.security)
  • Re: Encrypting symmetric keys
    ... The RSACryptoServiceProvider class should not be used directly -- you should ... generic EncryptValue and DecryptValue methods in the descendants of the RSA ... instead [Encrypt returns padded data]. ... In the RSAPKCS1KeyExchangeFormatter they did something like this: ...
    (microsoft.public.dotnet.security)
  • Re: RSA encrypt/decrypt of byte array
    ... You can't encrypt this much data with RSA with a key the size you are using. ... You definitely want to use UTF8 to convert your string to a byte array ...
    (microsoft.public.dotnet.security)
  • Re: Strong encryption with RSACryptoServiceProvider..?
    ... It is unusual to directly encrypt more than 16 bytes of data with RSA, ... Please do not send email directly to this alias. ... > It appears to be a bug in the RSACryptoServiceProvider. ...
    (microsoft.public.platformsdk.security)
  • Re: Strong encryption with RSACryptoServiceProvider..?
    ... It is unusual to directly encrypt more than 16 bytes of data with RSA, ... Please do not send email directly to this alias. ... > It appears to be a bug in the RSACryptoServiceProvider. ...
    (microsoft.public.dotnet.security)