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.