Hashim's Blogs

Tuesday, July 20, 2004

Simple Encryption and Decryption Using VB.NET

Imports System.Security.Cryptography
Imports System.Text
******* Encrypt the Data *******
Public Function GetEncryptedData(ByVal Data As String) As String
Dim shaM As New SHA1Managed
Convert.ToBase64String(shaM.ComputeHash(Encoding.ASCII.GetBytes(Data)))
Dim eNC_data() As Byte = ASCIIEncoding.ASCII.GetBytes(Data)
Dim eNC_str As String = Convert.ToBase64String(eNC_data)
GetEncryptedData = eNC_str
End Function
******* Decrypt the Data *******
Public Function GetDecryptedData(ByVal Data As String) As String
Dim dEC_data() As Byte = Convert.FromBase64String(Data)
Dim dEC_Str As String = ASCIIEncoding.ASCII.GetString(dEC_data)
GetDecryptedData = dEC_Str
End Function
*********************************

The above code snippet demonstrates simple encryption/Decryption a given string very use full in password encryption.
The function uses SHA1 to Compute the SHA1 hash for the input data.
The hash is used as a unique value of fixed size representing a large amount of data.
The hash size for the SHA1 algorithm is 160 bits.
This function Imports the  System.Security.Cryptography and System.Text namespace for this.


0 Comments:

Post a Comment

<< Home