. Net (C #): Use the pbkdf2 algorithm to protect passwords

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.