15-02-14, 09:22 AM
RSA Encryption In VB.NET
كود :
Imports System.Security.Cryptography 'Import the cryptography namespaces
Imports System.Text
Public Class Form1
'Declare global variables
Dim textbytes, encryptedtextbytes As Byte()
Dim rsa As New RSACryptoServiceProvider
Dim encoder As New UTF8Encoding
Dim encrypted, TextToDecrypt, TextToEncrypt, decrypted As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextToEncrypt = TextBox1.Text
encrypt()
TextBox2.Text = encrypted
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextToDecrypt = TextBox2.Text
decrypt()
TextBox3.Text = decrypted
End Sub
Sub encrypt()
textbytes = encoder.GetBytes(TextToEncrypt) 'Use UTF8 to convert the text string into a byte array
encryptedtextbytes = rsa.Encrypt(textbytes, True) 'encrypt the text
encrypted = Convert.ToBase64String(encryptedtextbytes) 'Convert the encrypted byte array into a Base64 string for display purposes
End Sub
Sub decrypt()
encryptedtextbytes = Convert.FromBase64String(TextToDecrypt)
textbytes = rsa.Decrypt(encryptedtextbytes, True) 'get the decrypted clear text byte array
decrypted = encoder.GetString(textbytes) 'convert the byte array to text using the same encoding format that was used for encryption
End Sub
End Class


