in the below code i need to get get the encoded value from textbox and need to convert it to byte array and then decrypt..
how can i do that
- Dim xmlKeys As String 'A combination of both the public and
'private keys
Dim xmlPublicKey
As String 'The public key only
'The plaintext message in a byte array
Dim PlainTextBArray
As Byte ()
'The cyphertext message in a byte array
Dim CypherTextBArray
As Byte ()
<LI>In the Form_Load event, create both the public and private keys:
Private Sub Form1_Load(
ByVal sender
As System.Object,
ByVal e
As
System.EventArgs)
Handles MyBase.Load
'This creates both the public and the private keys Dim rsa
As New RSACryptoServiceProvider
'Hold both keys in a global variable that will be used in the
'decryption procedure xmlKeys = rsa.ToXmlString(
True)
'Hold the public key to be used in the encryption procedure xmlPublicKey = rsa.ToXmlString(
False)
End Sub<LI>Type in the procedures that encrypt and decrypt, respectively:
Private Sub encrypt()
Dim rsa
As New RSACryptoServiceProvider
'get the public key so you can encrypt the message: rsa.FromXmlString(xmlPublicKey)
'get the message Dim message
As String = TextBox1.Text
If message.Length > 58
Then MsgBox("You must use fewer than
59 characters") :
Exit Sub 'transform message string into a byte array: PlainTextBArray = (
New UnicodeEncoding).GetBytes(message)
' Encrypt CypherTextBArray = rsa.Encrypt(PlainTextBArray,
False)
'view the cyphertext in TextBox2: TextBox2.Clear()
For i
As Integer = 0
To CypherTextBArray.Length - 1 TextBox2.Text &= Chr(CypherTextBArray(i))
Next i
'see the lengths of the plaintext, and the cyphertext Me.Text = "Plaintext Length (Unicode): " & PlainTextBArray.Length
& " Cyphertext Length: " & CypherTextBArray.Length
End Sub Private Sub decrypt()
Dim rsa
As New RSACryptoServiceProvider
'get the keys, thereby creating an RSA object that's identical ' to the one used in the Form_Load event when the keys were first built rsa.FromXmlString(xmlKeys)
'create a byte array and then put the decrypted plaintext into it Dim RestoredPlainText
As Byte () = rsa.Decrypt(CypherTextBArray,
False)
'Step through the two-byte unicode plaintext, displaying the restored
'plaintext message For i
As Integer = 0
To (RestoredPlainText.Length - 1)
Step 2 TextBox3.Text &= Chr(RestoredPlainText(i))
Next i
End SubTrigger the procedures by button clicks:
Private Sub Button1_Click(
ByVal sender
As System.Object,
ByVal e
As System.EventArgs)
Handles Button1.Click encrypt()
End Sub Private Sub Button2_Click(
ByVal sender
As System.Object,
ByVal e
As System.EventArgs)
Handles Button2.Click decrypt()
End Sub