RE: RSA Encryption: Saving keys as files, and size of encrypted data
- From: Zemp Dominik <zemp.dominik@xxxxxxxxxxxxxxxxxxx>
- Date: Sat, 15 Jul 2006 00:29:02 -0700
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
- References:
- RSA Encryption: Saving keys as files, and size of encrypted data
- From: Gary Bond
- RSA Encryption: Saving keys as files, and size of encrypted data
- Prev by Date: PROTECT FROM DECOMPILERS
- Next by Date: How to ByPass Protected Storage Prompt
- Previous by thread: RSA Encryption: Saving keys as files, and size of encrypted data
- Next by thread: WindowsIdentity.GetCurrent().Token cannot be used when remoting?
- Index(es):
Relevant Pages
|