Showing posts with label System.Security.Cryptography. Show all posts
Showing posts with label System.Security.Cryptography. Show all posts

Tuesday, 21 August 2012

For security purpose there is a need to convert plain string in cryptographic string or encrypt password, so hackers did not understand the password.
You can create cryptographic string using seven different algorithms that provided in .Net Framework.
This is mainly use in encrypt a password and store.
This encrypted password is do not convert back to original string.

Now if you want to compare password for authentication at that time you need to encrypt entered password at that time of login and check that encrypted string with already stored encrypted string.
Same input string generate same encrypted hash code.

Here is example for this.
In this Example we use MD5 algorithm. Means Encrypt password using MD5 algorithm.

C# Example :
        string strPassword = "thisisdemo";
        string strAlgorigthm = "MD5";
        //You can also specify these algorights RIPEMD160 or RIPEMD-160 ,SHA or SHA1 ,
        //SHA256 or SHA-256 ,SHA384 or SHA-384,SHA512 or SHA-512
        //SHA1Managed algorithm you must be instantiated directly like this "objAlg = new SHA1Managed()"

        HashAlgorithm objAlg = null;
        objAlg = HashAlgorithm.Create(strAlgorigthm);

        // Convert string to bytes
        byte[] objData = System.Text.Encoding.Default.GetBytes(strPassword);

        // Generate the hash code of password
        byte[] objHashBytes = objAlg.ComputeHash(objData);

        Response.Write("<b>Hash Code : </b>");
        Response.Write(BitConverter.ToString(objHashBytes));

VB.net Example :
        Dim strPassword As String = "thisisdemo"
        Dim strAlgorigthm As String = "MD5"
        'You can also specify these algorights RIPEMD160 or RIPEMD-160 ,SHA or SHA1 ,
        'SHA256 or SHA-256 ,SHA384 or SHA-384,SHA512 or SHA-512
        'SHA1Managed algorithm you must be instantiated directly like this "objAlg = new SHA1Managed()"

        Dim objAlg As HashAlgorithm = Nothing
        objAlg = HashAlgorithm.Create(strAlgorigthm)

        ' Convert string to bytes
        Dim objData As Byte() = System.Text.Encoding.[Default].GetBytes(strPassword)

        ' Generate the hash code of password
        Dim objHashBytes As Byte() = objAlg.ComputeHash(objData)

        Response.Write("<b>Hash Code : </b>")
        Response.Write(BitConverter.ToString(objHashBytes))

Output :
(To view original image , click on image)

You can also use other algorithms like RIPEMD160 or RIPEMD-160 ,SHA or SHA1 ,SHA256 or SHA-256 ,SHA384 or SHA-384,SHA512 or SHA-512.

Here is details of hashing algorithms available in .Net Framework. Click Here...


Only SHA1Managed algorithm can not be implemented using the factory approach.
It must be instantiated directly like this "hashAlg = new SHA1Managed()".

This is very useful .Net Tips.

For Beginning .Net articles. Click Here...

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.

Monday, 20 August 2012

Hashing algorithms are one way cryptographic functions that accept plain text of any length and generate a numeric value. These are one-way because it’s almost impossible to get the original plain text from the hash code. Hashing algorithms are useful for encrypt the password.

Use the same hashing algorithm to a specific text always generates the same hash code. This makes hash codes useful for check if two blocks of plain text are the same.
Generating hash codes in the .NET Framework is possbile.

The abstract class "HashAlgorithm" provides a base from which all hashing algorithm implementations.
The .NET Framework has the seven hashing algorithm implementations.
This class is in the System.Security.Cryptography namespace.

Here is list of Algorithms. 

Algorithm Name Class Name Hash Code Size (in Bits)
MD5 MD5CryptoServiceProvider 128
RIPEMD160 or RIPEMD-160    RIPEMD160Managed 160
SHA or SHA1 SHA1CryptoServiceProvider   160
SHA1Managed SHA1Managed 160
SHA256 or SHA-256 SHA256Managed 256
SHA384 or SHA-384 SHA384Managed 384
SHA512 or SHA-512 SHA512Managed 512

Visit this link for C# Examples and VB.net Example on Hashing Algorithm Implementation. Click Here...

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.

Friday, 13 July 2012

Hash code of a file is useful for check that file contents is changed over the time or not. First time you calculate hash code of a file and store , after some time period you again calculate hash code of a file and compare with stored hash code, If hash code is changed that means contents of the file is changed.

You can create a cryptographic hash code of the file using the ComputeHash method of the System.Security.Cryptography.HashAlgorithm class. Store calculated hash code for future comparison against newly calculated hash code of the same file. Hashing algorithm will generate a very different hash code even if the file has been changed slightly, and the chances of two different files resulting in the same hash code are very small.

Here is example of this.
In this example we are calculating hash code of a file.
We are calculating hash code using "SHA1" Hashing algorithm.
If you calculate hash code of same file using another hashing algorithm like "MD5" or "RIPEMD-160" it will produce different hash code.

C# Example :
        string strFile = Server.MapPath("TextFile.txt");
        using (HashAlgorithm objHashAlg = HashAlgorithm.Create("SHA1"))
        {
            
            using (Stream objFile = new FileStream(strFile, FileMode.Open, FileAccess.Read))
            {
                // Calculate the hash code of the file.
                byte[] objHash = objHashAlg.ComputeHash(objFile);
                
                Response.Write("<b>Calculated Hash Code :</b> "+ BitConverter.ToString(objHash));
            }
            
        }

VB.net Example :
        Dim strFile As String = Server.MapPath("TextFile.txt")
        Using objHashAlg As HashAlgorithm = HashAlgorithm.Create("SHA1")

            Using objFile As Stream = New FileStream(strFile, FileMode.Open, FileAccess.Read)
                ' Calculate the hash code of the file.
                Dim objHash As Byte() = objHashAlg.ComputeHash(objFile)

                Response.Write("<b>Calculated Hash Code :</b> " & BitConverter.ToString(objHash))

            End Using
        End Using

Output : 
(To view original image , click on image)

This is very useful .Net Tips.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.


Friday, 6 July 2012

We can generate cryptographic random number using RNGCryptoServiceProvider class.
This class is available in System.Security.Cryptography namespace.
The System.Random class is a random number generator , the algorithm it uses is deterministic, meaning that you can always calculate what the next number will be based on the previously generated number. This means that numbers generated by the Random class are unsuitable for security is a priority, like generating encryption keys and passwords.

Important : The numbers generated by the RNGCryptoServiceProvider class are not truly random. However, they are sufficiently random to meet the requirements of cryptography and security applications in most government and commercial Applications.


C# Example :
        // Create a byte array to store the random data.
        byte[] objNumber = new byte[32];

        // Generate random number.
        System.Security.Cryptography.RandomNumberGenerator objRNG = System.Security.Cryptography.RandomNumberGenerator.Create();

        // Generate 32 bytes random data.
        objRNG.GetBytes(objNumber);

        // Get byte data into string
        string strRandomNumber = BitConverter.ToString(objNumber);

VB.net Example :
        ' Create a byte array to store the random data.
        Dim objNumber As Byte() = New Byte(31) {}

        ' Generate random number.
        Dim objRNG As System.Security.Cryptography.RandomNumberGenerator = System.Security.Cryptography.RandomNumberGenerator.Create()

        ' Generate 32 bytes random data.
        objRNG.GetBytes(objNumber)

        ' Get byte data into string
        Dim strRandomNumber As String = BitConverter.ToString(objNumber)

Output :

(To view original image , click on image)

Important Note : The computational effort required by RNGCryptoServiceProvider to generate a random number is greater than required by Random class. For everyday routine purposes, the use of RNGCryptoServiceProvider is not suitable. Excessive and unnecessary use of the RNGCryptoServiceProvider class could degrade the application performance.

This is very useful .Net Tips.

Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.