Monday, July 19, 2004

How to do Encryption and Decryption using NET SHA1Managed class


Imports System.Security.Cryptography

C# Source Code

/// This function encript the string value using SHA notation

/// string value
/// Encripted value
public static string GetEncryptedData(string data)
SHA1Managed shaManaged = new SHA1Managed();
string Enc_val =
byte[] enc_data = ASCIIEncoding.ASCII.GetBytes(data);
string enc_string = Convert.ToBase64String(enc_data);
return enc_string;

/// This function decript the encripted value using SHA notation

/// Encripted value
/// stirng value

public static string GetDecryptedData(string data)
byte[] dec_data = Convert.FromBase64String(data);
string dsc_string = ASCIIEncoding.ASCII.GetString(dec_data);
return dsc_string;

VB Source Code

Public Function GetEncryptedData(ByVal Data As String) As String
'Dim shaM As New SHA1Managed
'Dim Enc_Password As String =
'GetEncryptedData = Enc_Password
Dim eNC_data() As Byte =
Dim eNC_str As String = Convert.ToBase64String(eNC_data)
GetEncryptedData = eNC_str
End Function

Public Function GetDecryptedData(ByVal Data As String) As String
Dim dEC_data() As Byte = Convert.FromBase64String(Data)
Dim dEC_Str As String =
GetDecryptedData = dEC_Str
End Function

How to use

Using these function you will get Encrypted and Decrypted form of data.

//return value would be "UHJvZHVjdElkPTEyMzQ1JkZpbGU9X05vYy54bWw="
string enc_value = GetEncryptedData('ProductId=12345&File=_Noc.xml');
//return value would be "ProductId=12345&File=_Noc.xml"
string dcrp_value = GetDecryptedData(enc_value);

No comments: