. Net-based encryption Summary (2) C # password encryption

Source: Internet
Author: User

Reference C # password encryption

Encryptpassword class:

Using system; using system. data; using system. configuration; using system. web; using system. web. security; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; using system. security. cryptography; using system. text;
Publicclass encryptpassword {// <summary> // obtain the key // </Summary> /// <returns> </returns> publicstaticstring createsalt () {byte [] DATA = newbyte [8]; new rngcryptoserviceprovider (). getbytes (data); Return convert. tobase64string (data );}
/// <Summary> /// encrypt the password /// </Summary> /// <Param name = "pwdstring"> </param> /// <Param name = "Salt"> </param> // <returns> </returns> publicstaticstring encryptpwd (string pwdstring, string salt) {If (salt = NULL | salt = "") {return pwdstring;} byte [] bytes = encoding. unicode. getbytes (salt. tolower (). trim () + pwdstring. trim (); Return bitconverter. tostring (hashalgorithm) cryptoconfig. createfromname ("sha1 ")). computehash (bytes ));}}

 

 

Desencrypt class:

Using system; using system. data; using system. configuration; using system. web; using system. web. security; using system. security. cryptography; using system. web. ui; using system. web. UI. webcontrols; using system. web. UI. webcontrols. webparts; using system. web. UI. htmlcontrols; using system. io; using system. text; // <summary> /// summary description for desencrypt // </Summary> publicclass desencrypt {privatestring IV = "12345678"; privatestring key = "12345678 "; private encoding = new unicodeencoding (); Private Des;
Public desencrypt () {des = new descryptoserviceprovider ();}
/// <Summary> /// set the encryption key /// </Summary> publicstring encryptkey {get {returnthis. Key ;}set {This. Key = value ;}}
/// <Summary> /// encoding mode of the characters to be encrypted /// </Summary> Public encoding encodingmode {get {returnthis. encoding;} set {This. encoding = value ;}}
/// <Summary> /// encrypted string and return the encrypted result /// </Summary> /// <Param name = "str"> </param>/ // <returns> </returns> publicstring encryptstring (string Str) {byte [] IVB = encoding. ASCII. getbytes (this. IV); byte [] keyb = encoding. ASCII. getbytes (this. encryptkey); // obtain the encryption key byte [] toencrypt = This. encodingmode. getbytes (STR); // get the content to be encrypted byte [] encrypted; icryptotransform encryptor = des. createencryptor (keyb, IVB); memorystream msencrypt = new memorystream (); cryptostream csencrypt = new cryptostream (msencrypt, encryptor, cryptostreammode. write); csencrypt. write (toencrypt, 0, toencrypt. length); csencrypt. flushfinalblock (); encrypted = msencrypt. toarray (); csencrypt. close (); msencrypt. close (); returnthis. encodingmode. getstring (encrypted );}}

 

1. principle: each time a random string is generated as the key, the user enters a password, and the password is encrypted with the key to get a string stored in the database... to verify the password, you must first obtain the key.

(1). Verify the code at login

 

// Able dt = wytweb. userdao. userlogin (username); If (Dt. Rows. Count = 0) {return-2; // the user does not exist}
Datarow ROW = DT. rows [0]; // obtain the key string salt = row ["salt"]. tostring (); // verify that the password is correct if (encryptpassword. encryptpwd (password, salt) = row ["password"]. tostring () {// login successful}

 

(2) when changing the password (the same as inserting a new password)

 

// Obtain the logon idint userid = loginuser_id from the base class; // obtain the key string salt = encryptpassword. createsalt (); // obtain the encrypted "password" string Password = encryptpassword. encryptpwd (txtpassword. text. trim (), salt); // modify the original data int result = wytweb. userdao. editpassword (userid, password, salt); If (result> 0) {wytweb. logdao. insertlog ("info", "wytweb", "user" + userid + "changed password", userid, this. request. userhostaddress. tostring (); showmessage ("password modified successfully"); // This. response. redirect ("companyinfo. aspx ");} else {wytweb. logdao. insertlog ("info", "wytweb", "user" + userid + "failed to Change Password", userid, this. request. userhostaddress. tostring (); showmessage ("failed to Change Password ");}

 

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.