I created a tutorial covering MD5 encryption and how to apply it in your applications (here). This was to help create secure Trial Period code for my applications (see here). These were both coupled with a third tutorial about manipulating the Windows Registry (here).

When I sat down and started working out how they would work together, I realised that having all three parts working together was next to impossible, simply for the reason that MD5 encryption is one way only. Once some data has been encrypted, it cannot then be decrypted, and since the trial period code needed to store a date to be checked when the application starts up, not only did it need to encrypt it, but also decrypt it when it came to checking the date.

So, I started investigating two way encryption and how it works in VB.NET (Thanks for pointing me in the right direction PsychoCoder ). This is what I discovered.

First of all, there are no decent tutorials or even explanations of what RSA Encryption is and does on the internet. I found a few which explained or gave code for different aspects of RSA, but nothing that put it all together. Hopefully, this tutorial will correct this.

Secondly, as with the other tutorials I've written recently, once I figured out what the code here was doing, it all became rather easy.

Ok, so on with the show.

RSA Encryption was invented in 1977 by three guys by the names of Rivest, Shamir, and Adlman. This is where it gets it's name. It is recommended you use RSA (or some other type of asynchronous encryption) for encrypting small amounts of data, like passwords.

First of all, what you need to do, as with the MD5 encryption, is import the necessary namespaces:

As you can see, before we use RSA to encrypt the data, we need to convert it into Byte format with the UTF8 Encoder. So, in effect, we are actually encrypting the data twice. Once from String to Byte, and then from Byte, to another Byte. The final line converts the encrypted data from Byte format, to Base64 String format in order to display it in TextBox2.

Once more, return to your IDE, but this time double click Button2. Here is your code:

As you can see here, to get the original data, you need to run the encrypted data through the RSA decryptor, then through the UTF8 Encoder again. When you test this application, after clicking Button1 to encrypt the data, delete the contents of TextBox1. When you click Button2, you should see the original text appear in TextBox1.

Using RSA to encrypt and decrypt data is that simple.

There is one more aspect of RSA that I will look at here, more as a point of interest than any useful application. The way RSA works is that it uses 2 'keys' to encrypt and decrypt the data. The first is a public key (and in this tutorial is used to encrypt the data) , and the second is a private key (to decrypt the data). For encrypting text files and the like, one user could encrypt it using the public key, send the encrypted data to another user, who then uses the private key to unlock the file and decrypt it. These keys are stored in Byte format, which means that if we want to take a look at them, they will need to be converted into a Base64 String, like so:

'Create an RSAParameters type
Dim Parameters As New RSAParameters
'export the key and other algorithm values
Parameters = rsa.ExportParameters(True)
'display the private key. Base64 encode the text to make display easier
TextBox4.Text = Convert.ToBase64String(Parameters.D)
'display the public key. Base64 encode the text to make display easier
TextBox3.Text = Convert.ToBase64String(Parameters.Modulus)

Well, that's about all I've been able to discover about RSA Encryption, but hopefully it will be enough for you to work out how it works and how you can manipulate it to do what you want with it.

If you have any questions about it, or comments, please post them here, and I will look into it for you.

Alright, so this has been really getting on my tits for the last 6 hours.

Previously I came here for help and you pointed me towards the MD5 hash encryption. Which by the way was perfect, easy, and works like a beauty.

However now I have some data that im saving into a database (like before), but THIS time, I need to be able to display it again some time. So for this i need a two-way cryption. So, naturally, RSA being the most common cryption service, i tried that.
And i got it working a beauty... in every program ive used it in except this one which is my coursework.

So we are clear, i have tested the pulling of the data from the database by displaying the text stored in the variable TextToDecrypt and it is the right data.
And if i copy and paste that data into my test program into textbox 2 and click button 2 it decrypts like a charm.
SO, all i can think of is that my computer hates me.

Any and all help appreciated.
Thanks.
JD.

EDIT: I have done an encryption in my test program and decrypted it, it worked fine, but when i copied the encrypted data it gave me, stopped it, then started it again, i pasted the encrypted data into the correct textbox and told it to decrypt, it came up with the same error: Error occurred while decoding OAEP padding.

On this basis, my theory is, the cryption must have some sort of Key or something which changes each time, so i need to tell my decrypter what key it used to encrypt the data with as for it to be able to decrypt it.

second EDIT: From my research on the theory of assymetric cryption, i can see that my guess about what i need to do wasn't all that far off, i do infact need to define some keys.
For the encryption a priavte key and for the decryption a public key.
That said... i still have NO idea how to do it.
From some google results i get the feeling its going to include csp parameters ToXmlString/FromXmlString.
But i could not find anything all that helpful.

Anyways, researching your problem, since I've never seen that error myself, points to the problem lying with the functions encoder.GetBytes and encoder.GetString. It seems that even if you feed these the exact same string, you won't always get the same result.

Instead it suggests you use Convert.ToBase64String and Convert.FromBase64String in their place.

The word i passed to it was 'Joshua'
So i tested it with 'abc' which threw the same error.

EDIT: Okay, DUH! Answer to this problem is simple, i forgot, strings to be converted FromBase64String or ToBase64String must be multiple of 4's.

However, this still gives a different encryption string each time.
And when stopping the program, starting it again, pasting the encrypted data in and clicking decrypt button, it gives the same error:
Error occurred while decoding OAEP padding.

I assume this would be because the difference is actually in the .encrypt and .decrypt functions like i originally thought. =(

EDIT: This thing at the bottom of your post... i think i can do it with that.

'Create an RSAParameters type
Dim Parameters As New RSAParameters
'export the key and other algorithm values
Parameters = rsa.ExportParameters(True)
'display the private key. Base64 encode the text to make display easier
TextBox4.Text = Convert.ToBase64String(Parameters.D)
'display the public key. Base64 encode the text to make display easier
TextBox3.Text = Convert.ToBase64String(Parameters.Modulus)

That shows us how to get the keys, so now i just need to adjust that code to store the keys somewhere, which i think can be done with KeyContainer or something like that, i will have to look into it.

Okay, well, the internet was pretty much useless, so using a combination of code examples (that didn't actually work), a couple of hours, and a whole lot of trial and error guess work, i finally got the right code.

I will go through it now.

Step 1: I set this as a Global Variable

'Key variables
Dim cp As New CspParameters

Step 2: I gave the variable some meaning

'set cp container name
cp.Flags = CspProviderFlags.UseMachineKeyStore
cp.KeyContainerName = "CryptKeys" 'This name can be anything you choose it to be.

Step 3: I added this line to the very begining of BOTH the encrypt, and decrypt procedures

Dim rsa As New RSACryptoServiceProvider(cp)

And as if by magic, your program remembers.

Now, the key is stored in your computer and used every time.

So now, to test it: run it, encrypt it, decrypt it, copy the encrypted data in textbox2, close it, stop it, run it again, paste into textbox2 and click decrypt. It will decrypt it no problems. =D

I think this means that if you were to copy the program to a different PC, it would no longer remember the key, meaning you can't decrypt your decrypted data anymore, so be careful you dont move accross computers then realise your stuck with a load of encrypted data you can't decrypt. But, i haven't tested moving it to another PC yet.

Im not gonna give you my entire programs code just to explain this so i'll give you an example program using the code Bort gave us: