'This function creates an object of TDES Class and uses the ENCRYPT method to actually encrypt your
'string in a required format.
'parameter to be send to this method is the string which is to be encrypted and the Encrypt Type
Private Function encryptText(ByVal text As String, ByVal encType As String)
Try
Dim utf8encoder As UTF8Encoding = New UTF8Encoding
TDES = New TripleDES
txtEnc = TDES.Encrypt(txtEncrypt.Text, encType)
txtData.Text = "Encrypted text: "
For i As Integer = 0 To txtEnc.Length - 1
txtData.AppendText(Chr(txtEnc(i)))
Next
txtData.AppendText(vbNewLine & vbNewLine)
txtData.AppendText("Decrypted Text after encryption: ")
txtData.AppendText(TDES.Decrypt(txtEnc, encType))
txtData.AppendText(vbNewLine & vbNewLine)
txtData.AppendText("Encrypted text length (in bits): " & txtEnc.LongLength.ToString())
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
If (txtEncrypt.Text.Length > 0) Then
txtData.Text = String.Empty
Select Case cmbEncType.Text
Case "RSA"
If (txtPublic.Text.Trim() <> String.Empty And txtPrivate.Text.Trim() <> String.Empty) Then
EncryptRSA(txtEncrypt.Text)
Else
MessageBox.Show("Keys not generated!", "Invalid Keys", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Case "DES"
encryptText(txtEncrypt.Text, cmbEncType.Text)
Case "3DES"
encryptText(txtEncrypt.Text, cmbEncType.Text)
Case Else
EncryptRijnDael(txtEncrypt.Text, cmbEncType.Text)
End Select
End If
End Sub
Private Sub btnDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If (txtData.Text.Trim().Length > 0) Then
txtData.Text = String.Empty
decryptText(txtData.Text.Trim())
End If
End Sub
'This function creates an object of TDES Class and uses the ENCRYPTRSA method to actually encrypt your string in a RSA format.
Private Function EncryptRSA(ByVal text As String)
Try
Dim key As String
Dim arrlis As ArrayList
TDES = New TripleDES
arrlis = TDES.EncryptRSA(text, txtPublic.Text)
'txtEnc = TDES.EncryptRSA(text)
txtData.Text = "Encrypted text: "
For j As Integer = 0 To arrlis.Count - 1
txtEnc = CType(arrlis(j), Byte())
For i As Integer = 0 To txtEnc.Length - 1
txtData.AppendText(Chr(txtEnc(i)))
Next
Next
txtData.AppendText(vbNewLine & vbNewLine)
txtData.AppendText("Encrypted text length (in bits): " & txtEnc.LongLength.ToString())
txtData.AppendText(vbNewLine & vbNewLine)
txtData.AppendText("Decrypted Text after encryption: ")
For j As Integer = 0 To arrlis.Count - 1
txtEnc = CType(arrlis(j), Byte())
txtEnc = TDES.DecryptRSA(txtEnc, txtPrivate.Text)
For ctr As Integer = 0 To (txtEnc.Length - 1)
txtData.AppendText(Chr(txtEnc(ctr)))
Next ctr
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
'This function creates an object of TDES Class and uses the ENCRYPT method to actually encrypt your string in a Rijindael format.
Private Function EncryptRijnDael(ByVal text As String, ByVal encType As String)
Try
TDES = New TripleDES
txtData.Text = "Encrypted string: "
Dim passPhrase As String = ConfigurationSettings.AppSettings("PassPhrase")
Dim saltValue As String = ConfigurationSettings.AppSettings("SaltValue")
Dim iterations As String = ConfigurationSettings.AppSettings("PasswordIterations")
Dim IV As String = ConfigurationSettings.AppSettings("InitVector")
Dim keySize As String = ConfigurationSettings.AppSettings("KeySize")
encType = ConfigurationSettings.AppSettings("EncType")
Dim encString As String = TDES.Encrypt(text, passPhrase, saltValue, encType, iterations, IV, Integer.Parse(keySize))
txtData.AppendText(encString & vbNewLine & vbNewLine)
txtData.AppendText("Decrypted string after encryption: ")
txtData.AppendText(TDES.Decrypt(encString, passPhrase, saltValue, encType, iterations, IV,
Integer.Parse(keySize)))
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
Private Sub btnEncRSA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncRSA.Click
txtData.Text = String.Empty
EncryptRSA(txtEncrypt.Text)
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Close()
End Sub
Private Sub cmbEncType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbEncType.SelectedIndexChanged
Select Case cmbEncType.Text
Case "RSA"
btnGetKey.Visible = True
lblPublic.Visible = True
lblPrivate.Visible = True
txtPublic.Visible = True
txtPrivate.Visible = True
txtPublic.Clear()
txtPrivate.Clear()
Case Else
btnGetKey.Visible = False
lblPublic.Visible = False
lblPrivate.Visible = False
txtPublic.Visible = False
txtPrivate.Visible = False
End Select
End Sub
'This function creates your Public Keys and Private Keys using GetKeysForRSA method From TripleDES Class
Private Sub GetKeys()
Try
pubKey = String.Empty
priKey = String.Empty
TDES = New TripleDES
TDES.GetKeysForRSA(pubKey, priKey)
txtData.Clear()
txtData.AppendText("Public Key: " & pubKey & vbNewLine & vbNewLine)
txtData.AppendText("Private Key: " & priKey & vbNewLine & vbNewLine)
txtPublic.Text = pubKey
txtPrivate.Text = priKey
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub btnGetKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetKey.Click
GetKeys()
End Sub