Cryptography RSA Algorithm of in VB.NET
In this article you will learn that how can you implement RSA Algorithm.
Introduction
Here in this article we are discussing about RSA Encryption algorithm of Cryptography. RSA Encryption was invented in 1977 by three guys by the names of Rivest,Shamir and Adlmn. This is where it get's its name. RSA is recommended for mainly Encrypting small amount of data,like password. The implementation of RSA needs to import the necessary namespaces then add controls and declare some variables these variables will used throughout our code. You will put these variables just under Public Class Form1.
Before we use RSA to Encrypt the data, you need to convert it into byte format with the UTF8 Encoder. So you are actually Encrypting the data twice. One from String to Byte and then 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. To get the original data you need to run the encrypted data through the RSA decryptor, Than through the UTF8 Encoder again, when you test this application, after clicking Encrypt Button to Encrypt the data, delete the contents of TextBox1. When you click Decrypt Button you should see the original text will appear in TextBox1.
Getting Started
- Simply create a new Windows Application.
- Drag two Buttons and TextBox on your form. Your form will look like below.
- Now add the below code for implementation.
Dim textbytes, encryptedtextbytes As Byte()
Dim rsa As New RSACryptoServiceProvider
Dim encoder As New UTF8Encoding
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim TexttoEncrypt As String = TextBox1.Text
textbytes = encoder.GetBytes(TexttoEncrypt)
encryptedtextbytes = rsa.Encrypt(textbytes, True)
TextBox2.Text = Convert.ToBase64String(encryptedtextbytes)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
textbytes = rsa.Decrypt(encryptedtextbytes, True)
TextBox1.Text = encoder.GetString(textbytes)
End Sub
- Now run your application.
Output:-
Summary
In this article you learned that how to implement RSA Algorithm.