SHA1 coding in C #

0

I need to code a password and save it in SQL , for the part of saving it I have no problem, but with the encryption part I do not know what to do. (I've never done anything similar)

I have the following code that I found in a forum:

public class Program
{
    public static string encriptar(string Cadena)
    {              
            SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
            byte[] vectoBytes = System.Text.Encoding.UTF8.GetBytes(Cadena);
            byte[] inArray = SHA1.ComputeHash(vectoBytes);
            SHA1.Clear();
            return Convert.ToBase64String(inArray);            
    }

    public static void Main(string[] args)
    {
        Console.WriteLine(encriptar("Hola Mundo"));
    }
}

But I do not understand it very well, can someone explain to me how it works?

I would also like to know if it is possible to reverse the encryption process, that is, take the resulting string and get the password.

    
asked by IVAN CABRERA 28.07.2018 в 21:53
source

1 answer

1

I'll explain it to you as best I can. I assume that the encryption method is the one you do not understand.

public static string encriptar(string Cadena)
{              
        SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();
        byte[] vectoBytes = System.Text.Encoding.UTF8.GetBytes(Cadena);
        byte[] inArray = SHA1.ComputeHash(vectoBytes);
        SHA1.Clear();
        return Convert.ToBase64String(inArray);            
}

Well, nothing, let's go step by step. It is a method that receives the string you want to transform (in this case it will be your password) and returns the result of applying the summary function (which in this case will be SHA1) to the password.

Well, going into the method:

SHA1CryptoServiceProvider SHA1 = new SHA1CryptoServiceProvider();

This line creates an instance of type SHA1CryptoServiceProvider. SHA1CryptoServiceProvider is the class that contains the SHA1 function. This function is called "ComputeHash". It requires an array of bytes to be transformed and returns another array of bytes with the transformation.

Although this is just my opinion, the instance operates with bytes for convenience, for efficiency and because aspects of text strings such as encoding have nothing to do with the problem to be addressed (.Net text strings have Unicode encoding)

Since the function needs an array of bytes, we will have to get the byte array of the text string.

byte[] vectoBytes = System.Text.Encoding.UTF8.GetBytes(Cadena);

If String takes the value "Boss", vectoBytes would take the value of an array with four values: [0x42, 0x6F, 0x73 0x73]

B - > 0x42 O - > 0x6F S - > 0x73 S - > 0x73

Now we have our password in the desired type (byte []), so we can now generate the hash:

byte[] inArray = SHA1.ComputeHash(vectoBytes);

Next, we will release the resources that SHA1 has used.

.Net manages the RAM we use during the execution of a program, but there are two fundamental problems:

  • This management is not perfect or efficient (I will not go into details, I think my answer is already extensive enough).

  • .Net can not manage the memory that has been reserved independently from the .Net memory manager (unmanaged memory, nor will I go into details).

  • For these two reasons certain objects have code that releases the resources that the memory manager can not release, either for reason 1, 2 or another that has escaped me.

    SHA1.Clear();
    

    Well, finally we return the results.

    As I said before, the ComputeHash function returns an array of bytes. Since our method must return a string of text, we generate a text string equivalent to the collection of bytes. this time we choose to transform the Bytes to Base64, I understand that this is due to the fact that the problems related to coding are minimized at the expense of increasing the length of the text string (yes, Base 64 multiplies the number of characters by one factor up to 4/3).

    return Convert.ToBase64String(inArray);
    

    This is all, I hope you find it useful.

        
    answered by 30.07.2018 / 10:55
    source