Texts Encryption
vbnet | by: nemoload
posted: May, 9th 2012 | jump to bottom
Imports System.IO Imports System.Security Imports System.Security.Cryptography Public Class formMain Private Sub buttonEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonEncrypt.Click textResult.Text = Encrypt(textText.Text, textPassword.Text) End Sub Private Sub buttonDecrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonDecrypt.Click textResult.Text = Decrypt(textText.Text, textPassword.Text) End Sub Private Function Encrypt(ByVal sText As String, ByVal sPass As String) As String Return Crypt(sText, sPass, True) End Function Private Function Decrypt(ByVal sText As String, ByVal sPass As String) As String Return Crypt(sText, sPass, False) End Function Private Function Crypt(ByVal sText As String, ByVal sPass As String, ByVal bEnc As Boolean) As String Dim bSalt As Byte() = System.Text.Encoding.ASCII.GetBytes("nemo") Dim iPass As New PasswordDeriveBytes(sPass, bSalt) Dim Alg As New RijndaelManaged Dim iTrans As ICryptoTransform Dim iMem As New MemoryStream Dim newData As Byte() Alg.Key = iPass.GetBytes(32) Alg.IV = iPass.GetBytes(16) iTrans = IIf(bEnc = True, Alg.CreateEncryptor(), Alg.CreateDecryptor()) Dim iCrypt As New CryptoStream(iMem, iTrans, CryptoStreamMode.Write) If bEnc Then newData = System.Text.Encoding.Unicode.GetBytes(sText) Else newData = Convert.FromBase64String(sText) End If Try iCrypt.Write(newData, 0, newData.Length) iCrypt.Close() If bEnc Then Return Convert.ToBase64String(iMem.ToArray()) Else Return System.Text.Encoding.Unicode.GetString(iMem.ToArray()) End If Catch ex As Exception End Try Return "" End Function Private Sub buttonClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Me.Close() End Sub Private Sub formMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load End Sub Private Sub textPassword_TextChanged(sender As System.Object, e As System.EventArgs) Handles textPassword.TextChanged End Sub Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles Label1.Click End Sub End Class
65 views




