Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.IO
Imports System.Linq
Imports System.Security.Cryptography
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Shared Function EncryptStringToBytes(plainText As String, Key As Byte(), IV As Byte()) As Byte()
' Check arguments.
If plainText Is Nothing OrElse plainText.Length <= 0 Then
Throw New ArgumentNullException("plainText")
End If
If Key Is Nothing OrElse Key.Length <= 0 Then
Throw New ArgumentNullException("Key")
End If
If IV Is Nothing OrElse IV.Length <= 0 Then
Throw New ArgumentNullException("Key")
End If
Dim encrypted As Byte()
' Create an RijndaelManaged object
' with the specified key and IV.
Using rijAlg As New RijndaelManaged()
rijAlg.Key = Key
rijAlg.IV = IV
' Create a decrytor to perform the stream transform.
Dim encryptor As ICryptoTransform = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV)
' Create the streams used for encryption.
Using msEncrypt As New MemoryStream()
Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
Using swEncrypt As New StreamWriter(csEncrypt)
'Write all data to the stream.
swEncrypt.Write(plainText)
End Using
encrypted = msEncrypt.ToArray()
End Using
End Using
End Using
' Return the encrypted bytes from the memory stream.
Return encrypted
End Function
Private Shared Function DecryptStringFromBytes(cipherText As Byte(), Key As Byte(), IV As Byte()) As String
' Check arguments.
If cipherText Is Nothing OrElse cipherText.Length <= 0 Then
Throw New ArgumentNullException("cipherText")
End If
If Key Is Nothing OrElse Key.Length <= 0 Then
Throw New ArgumentNullException("Key")
End If
If IV Is Nothing OrElse IV.Length <= 0 Then
Throw New ArgumentNullException("Key")
End If
' Declare the string used to hold
' the decrypted text.
Dim plaintext As String = Nothing
' Create an RijndaelManaged object
' with the specified key and IV.
Using rijAlg As New RijndaelManaged()
rijAlg.Key = Key
rijAlg.IV = IV
' Create a decrytor to perform the stream transform.
Dim decryptor As ICryptoTransform = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV)
' Create the streams used for decryption.
Using msDecrypt As New MemoryStream(cipherText)
Using csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)
Using srDecrypt As New StreamReader(csDecrypt)
' Read the decrypted bytes from the decrypting stream
' and place them in a string.
plaintext = srDecrypt.ReadToEnd()
End Using
End Using
End Using
End Using
Return plaintext
End Function
Private Sub button1_Click(sender As Object, e As EventArgs)
Using myRijndael As New RijndaelManaged()
myRijndael.GenerateKey()
myRijndael.GenerateIV()
' Encrypt the string to an array of bytes.
textBox2.Text = Convert.ToBase64String(EncryptStringToBytes(textBox1.Text, myRijndael.Key, myRijndael.IV))
textBox3.Text = Convert.ToBase64String(myRijndael.Key).ToString()
textBox4.Text = Convert.ToBase64String(myRijndael.IV).ToString()
End Using
End Sub
Private Sub button2_Click(sender As Object, e As EventArgs)
Try
Using myRijndael As New RijndaelManaged()
Dim chiperText As Byte() = Convert.FromBase64String(textBox5.Text.Trim())
myRijndael.Key = Convert.FromBase64String(textBox6.Text.Trim())
myRijndael.IV = Convert.FromBase64String(textBox7.Text.Trim())
' Decrypt the bytes to a string.
Dim orginalText As String = DecryptStringFromBytes(chiperText, myRijndael.Key, myRijndael.IV)
textBox8.Text = orginalText
End Using
Catch
MessageBox.Show("لايمكن فك التشفيرة", "error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End Try
End Sub
End Class