Simple C# RSA Encryption Class
Well, you can figure this out yourself, use it if you like, tell me how much it sucks, what ever you want.
What it does is takes data and encrypts it using DES encryption and then encrypts the DES Keys using RSA and stores everything in an XML string to be saved for later decryption. There are also classes to hold the encrypted data and to create and save the RSA keys. This seems to be a pretty good solution for encrypting data or files.
I wrote this code and am now posting it because I did not find any simple snippets on the web for encrypting data in C#. Everything seemed too complicated or was not robust enough. I am sure that many people may have problems with this code or the lack of explaination in the code, but oh well... Its here for those who want to look it over. Remember, use at your own risk!
Code:

8 Comments:
Would you mind posting a small code showing how to use this classes?
Usage:
To Create the public and private key and save them to a file:
SimpleEncryption.RSAEncryptor.RSAKeyCreator rkc = new SimpleEncryption.RSAEncryptor.RSAKeyCreator();
rkc.SavePrivateKeyToFile(privateKeyFilePath);
rkc.SavePublicKeyToFile(publicKeyFilePath);
Note: private key can decrypt and encrypt, but the public key can only encrypt.
To use the class:
SimpleEncryption.RSAEncryptor rsa = new SimpleEncryption.RSAEncryptor(pathToKeyFile);
rsa.Encrypt(data);
rsa.Decrypt(data);
I hope this helps.
I thought one of uses of asymmetric encryption WAS the ability to encrypt with the private key and deccrypt with the associateed public key (which is what I need). The code for this scenario will run OK in .Net 1.1, but not .Net 2.0
ANy ideas on how to achieve the above? Fiddling with the array of properties changes the exception message at best.
I believe that you are incorrect, decrypting data with a public key would not provide any protection at all. The information needed to decrypt can also be used to encrypt the data. If this key is made public then the data will not be secure.
If two systems want to communicate with each other, they will trade public keys and use the recievers key to encrypt the data and their own private key to decrypt.
As far as this not working in 2.0, I don't know what to tell you. It's an old class and I have not tried it.
Traditionally you give out the Encryption key and keep the Decryption key private. Thats how you stop others reading the data.
However for digital sigunatures you simply give out the decryption key and keep the encryption one private. Enabling anyone to verify the signature, but only you can create it.
It all depeneds what you want to use it for, as to which key is the public nad which is the private.
Just remember you have to keep one of them secret. So if you need to encrypt the data and digitally sign it, u need two key pairs. Hope that helps.
One other thing. Due to recent advances in breaking DES, it is ragarded as past its usful life. If you have any very sensative data you should use AES.
AES is also avalible in the framework, although its 100% managed code so its not the fastest implementation on the planet.
DES will stop praying eyes, but any thing that needs more security should no longer use DES. AES can use upto 256 bit keys.
I remember reading somewhere that current top supper computers would take 4 billion years to try every combination of a 256bit AES key. So AES should be good for a few users more of the prossor performance wars.
can u help me out by posting a small code snippet to achieve multi-level encryption, ie; for eg, using the encrypted data produced by one algothirm as input for performing another level of encryption. take the order as RC4 alog' n RSA algo'
Thanks so much for posting this! It helped me out alot since i was having a hard time with the key generation and how to use them - this completely simplified everything and reduced a lot of stress for me! By the way, I was able to use this sample in asp.net 2.0 with no problems and updated your sample to use tripple DES for added security - works great!
Post a Comment
Links to this post:
Create a Link
<< Home