There are many methods to save a user password as a key. For example, the simplest method is to directly convert a string into bytes.
StringPassword= "Mgen! ";
VaREncoding= New Unicodeencoding(False,False);
Byte[] Bytes=Encoding.Getbytes (password );
Console.Writeline (Encoding.Getstring (bytes ));
However, it is clear that attackers can directly obtain source text through byte.
The second method is to use HashAlgorithm:
// + Using system. Security. cryptography;
StringPassword= "Mgen! ";
VaREncoding= New Unicodeencoding(False,False);
Byte[] Bytes=Encoding.Getbytes (password );
Using(VaRHashalg= Sha1.Create ())
{
VaRHash=Hashalg.Computehash (bytes );
Console.Writeline (Bitconverter.Tostring (hash ));
}
However, the hash results of the same password text must be the same, so that attackers may create a hash ing dictionary to release the password.
Finally, let's look at pbkdf.
Note:
In. net, the execution of pbkdf1 is the passwordderivedbytes class. However, after. NET 2.0, the execution of pbkdf2: The getbytes method of rfc2898derivedbytes class replaces the getbytes method of passwordderivedbytes. However, the passwordderivedbytes class is not discarded because it not only provides pbkdf1 execution, but its cryptderivedkey method calls the corresponding functions of the Windows encryption function library CryptoAPI.
// + Using system. Security. cryptography;
// Password text
StringPassword= "Mgen! ";
// Enter the random password salt
Byte[] Salt= New Byte[20];
VaRRNG= Randomnumbergenerator.Create ();
RNG.Getbytes (SALT );
// Utf8 (without BOM) is used by default to obtain bytes. Hip hop
VaRKD= New Rfc2898derivebytes(Password, salt );
// Output key 1
Console.Writeline (Bitconverter.Tostring (KD.Getbytes (10)));
// Replace the salt
RNG.Getbytes (KD.Salt );
// Output key 2
Console.Writeline (Bitconverter.Tostring (KD.Getbytes (10)));
Output:
B2-46-B4-A4-EB-E2-A0-B8-B4-44
07-93-8d-06-9a-bb-ae-21-b8-91
OK. For the same password, the output key is also different because salt is different!