RSA Encryption: Saving keys as files, and size of encrypted data
- From: Gary Bond <GaryB@xxxxxxxxxxxxxxxx>
- Date: Tue, 11 Jul 2006 13:43:01 -0700
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
.
- Follow-Ups:
- RE: RSA Encryption: Saving keys as files, and size of encrypted data
- From: Zemp Dominik
- RE: RSA Encryption: Saving keys as files, and size of encrypted data
- Prev by Date: Re: How to - PKCS#7 in c#
- Next by Date: WindowsIdentity.GetCurrent().Token cannot be used when remoting?
- Previous by thread: code access security across the network
- Next by thread: RE: RSA Encryption: Saving keys as files, and size of encrypted data
- Index(es):
Relevant Pages
|