itortu
asked on
encrypt / decrypt text string in vb.net - VS 2005
I would like to ask from some help related to encryption / decryption.
Is it feasilbe to get some sample code of a small application that has
a textbox and two radio buttons that does the following:
1. enter text in box
2. select radio button "encrypt"
3. show encrypted text in text box or msgbox
4. select radio button "decrypt"
5. show decrypted text in text box or msgbox
Any help with sample code would be great.
Thank you!
Is it feasilbe to get some sample code of a small application that has
a textbox and two radio buttons that does the following:
1. enter text in box
2. select radio button "encrypt"
3. show encrypted text in text box or msgbox
4. select radio button "decrypt"
5. show decrypted text in text box or msgbox
Any help with sample code would be great.
Thank you!
Here is a very simple Encryption/Decryption class that works with Strings. Simply pass in the string to encrypt/decrypt and the key that you want to use.
Imports System.IO
Imports System.Text
Imports System.Security.Cryptograp hy
Public Class Crypto
Private Shared DES As New TripleDESCryptoServiceProv ider
Private Shared MD5 As New MD5CryptoServiceProvider
Public Shared Function MD5Hash(ByVal value As String) As Byte()
Return MD5.ComputeHash(ASCIIEncod ing.ASCII. GetBytes(v alue))
End Function
Public Shared Function Encrypt(ByVal stringToEncrypt As String, ByVal key As String) As String
DES.Key = Crypto.MD5Hash(key)
DES.Mode = CipherMode.ECB
Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetByt es(stringT oEncrypt)
Return Convert.ToBase64String(DES .CreateEnc ryptor().T ransformFi nalBlock(B uffer, 0, Buffer.Length))
End Function
Public Shared Function Decrypt(ByVal encryptedString As String, ByVal key As String) As String
Try
DES.Key = Crypto.MD5Hash(key)
DES.Mode = CipherMode.ECB
Dim Buffer As Byte() = Convert.FromBase64String(e ncryptedSt ring)
Return ASCIIEncoding.ASCII.GetStr ing(DES.Cr eateDecryp tor().Tran sformFinal Block(Buff er, 0, Buffer.Length))
Catch ex As Exception
MessageBox.Show("Invalid Key", "Decryption Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation )
End Try
End Function
End Class
Imports System.IO
Imports System.Text
Imports System.Security.Cryptograp
Public Class Crypto
Private Shared DES As New TripleDESCryptoServiceProv
Private Shared MD5 As New MD5CryptoServiceProvider
Public Shared Function MD5Hash(ByVal value As String) As Byte()
Return MD5.ComputeHash(ASCIIEncod
End Function
Public Shared Function Encrypt(ByVal stringToEncrypt As String, ByVal key As String) As String
DES.Key = Crypto.MD5Hash(key)
DES.Mode = CipherMode.ECB
Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetByt
Return Convert.ToBase64String(DES
End Function
Public Shared Function Decrypt(ByVal encryptedString As String, ByVal key As String) As String
Try
DES.Key = Crypto.MD5Hash(key)
DES.Mode = CipherMode.ECB
Dim Buffer As Byte() = Convert.FromBase64String(e
Return ASCIIEncoding.ASCII.GetStr
Catch ex As Exception
MessageBox.Show("Invalid Key", "Decryption Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation
End Try
End Function
End Class
ASKER
how the key is defined?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
follow the below link
http://www.devarticles.com/c/a/VB.Net/String-Encryption-With-Visual-Basic-.NET/1/
http://ntcnet.wordpress.com/2007/11/27/encryptdecrypt-string-vbnet/
b u d d h a