Wednesday, September 8, 2010

Function to Encrypt String in C# .Net using SHA1 Algorithm

SHA stands for Secure Hash Algorithm. This hash algorithm are applied to encrpt the string and store in un-readable format. SHA-1 is the best established of the existing SHA hash functions, and is employed in several widely used security applications and protocols. SHA-1 is implemented in .Net using System.Security.Cryptography.SHA1 Namespace.

First the given string is converted to byte array using ASCIIEncoding class in System.Text Namespace. GetBytes method on ASCIIEncoding class converts given string to byte[]

ComputeHash method in SHA1 class create a hash with SHA-1 Algorithm. Resultant Hashed byte[] is convert to string using Convert.ToBase64String Method

Hashed on encrpted string cannot be decryted (for security reasons). Raw string can be compared with hashed string only by hashing the raw string using the same hash function.

No comments:

Post a Comment