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:
'Import the cryptography namespaces Imports System.Security.Cryptography Imports System.Text
Now, on your form, add 4 TextBoxes, and 3 Buttons. Set the Multiline property for TextBoxes 2, 3 and 4 to True.
Next we will need to declare some variables. Since these will be used throughout our code, I decided to put them just under Public Class Form1, like so:
Public Class Form1 'Declare global variables Dim textbytes, encryptedtextbytes As Byte() Dim rsa As New RSACryptoServiceProvider Dim encoder As New UTF8Encoding
After this is ready, double-click on Button1 in your IDE and paste the following code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim TexttoEncrypt As String = TextBox1.Text 'Use UTF8 to convert the text string into a byte array textbytes = encoder.GetBytes(TexttoEncrypt) 'encrypt the text encryptedtextbytes = rsa.Encrypt(textbytes, True) 'Convert the encrypted byte array into a Base64 string for display purposes TextBox2.Text = Convert.ToBase64String(encryptedtextbytes) End Sub
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:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'get the decrypted clear text byte array textbytes = rsa.Decrypt(encryptedtextbytes, True) 'convert the byte array to text using the same encoding format that was used for encryption TextBox1.Text = encoder.GetString(textbytes) End Sub
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.