JS in the use of cookies to realize the password-remembering function

Source: Internet
Author: User
Tags decrypt setcookie

JS in the use of cookies to realize the password-remembering function

In the login interface to add the Remember password feature, I first think of the Java background to call the cookie store account password, roughly the following:

HttpServletRequest request  httpservletresponse responseNew Cookie ("username", "cookievalue");  New Cookie ("Password", "Cookievalue");  Response.addcookie (username); 6 Response.addcookie (password);      

But for the sake of security, we get the password in the background is mostly in JS through MD5 encrypted ciphertext, if the cipher is placed in the cookie, in the JS to get to also have no effect;

Then consider accessing the cookie in JS with the following code:

1//Set cookies2Varpasskey = ' 4c05c54d952b11e691d76c0b843ea7f9 ';3functionSetcookie (CNAME, cvalue, exdays) {4var d =NewDate ();5 D.settime (D.gettime () + (exdays*24*60*60*1000));6var expires = "expires=" +D.toutcstring ();7 Document.cookie = cname + "=" +EncryptEscape (Cvalue), passkey) + ";" +Expires8}9//Get cookies10functionGetCookie (CNAME) {11var name = cname + "=";12var ca = document.cookie.split ('; '));13Forvar i=0; i<ca.length; i++) {14var C =Ca[i];15while (C.charat (0) = = ") c = c.substring (1);16if (C.indexof (name)! =-1){17var cnamevalue = unescape (c.substring (Name.length, c.length)); 18 return  decrypt ( Cnamevalue, passkey); 19 } 20 }21 return" ";< Span style= "COLOR: #008080" >22 }23 // clear cookie 24  Function ClearCookie (CNAME) {25 setcookie (CNAME, "", -1< Span style= "color: #000000"); 26}             

Setcookie (CNAME, cvalue, exdays) Three parameters are the cookie name, cookie value, and the number of days the cookie is valid.

Since cookies cannot contain special characters such as equals, spaces, semicolons, and so on, I use the escape () function to encode the string when I set the cookie, and use the unescape () function to decode when I get the cookie. However, the escape () function does not encode ASCII letters and numbers, so the accounts and passwords stored in the cookie are stored in plaintext and are not secure. Then went online to find a string encryption and decryption algorithm, the algorithm needs to pass two parameters, a string that needs to be encrypted, a custom encryption key passkey. When setting a cookie using Encrypt (value, passkey) encryption, the cookie is read using decrypt (value, passkey) decryption, which is appended to the end of this article.

Call to access the cookie method:

1. Define a checkbox

<input type= "checkbox" id= "RememberMe" checked= "checked"/> Remember password

2, determine the account password input error after the call

if ($ (' #rememberMe '). Is (': Checked ')) {2             setcookie (' CustomerName ', $ (' #username '). Val (). Trim (), 7) 3 Setcookie (' Customerpass ', $ (' #password '). Val (). Trim (), 7)4}     

3, enter the login interface, determine whether there is an account password in the cookie, if there is automatically populated

$ (function() {  // Get cookie  var cusername = GetCookie (' customername 'var cpassword = GetCookie (' Customerpass 'if (cusername! = "" && Cpassword! = "") {$ ("#username"). Val (Cusername); $ ("#password"). Val (Cpassword);}}         

Finally, we enclose a string encryption and decryption algorithm

1 eval (function (p,a,c,k,e,r) {e=function (c) {Return (c<a? ": E (parseint (C/A))) + ((c=c%a) >35? String.fromCharCode (c+29): c.tostring (+))};if (! ". Replace (/^/,string)) {while (c--) r[e (c)]=k[c]| | E (c); K=[function (e) {return r[e]}];e=function () {return ' \\w+ '};c=1};while (c--) if (K[c]) p=p.replace (New RegExp (' \\b ') +e (c) + ' \\b ', ' G '), k[c]); return p} (' A g (A, B) {x (b==v| | b.7<=0) {d.y ("Z R P O"); t v}6 c= ""; s (6 i=0;i<b.7;i++) {c+=b.u (i). N ()}6 D=M.R (C.7/5); 6 e=l (C.9 (D) +c.9 (d*2) +c.9 (d*3) +c.9 (d*4) +c.9 (d*5)), 6 F=M.M (B.7/2), 6 g=m.b (2,c) -1;x (e<2) {d.y ("L K J z"); t v}6 h=m.f (M.H () *n)%i;c+=h;w (c.7>q) {c= (L (C.O (0,q)) +l (C.O (Q,c.7))). N ()}c= (e*c+f)%g;6 j= ""; 6 k= ""; s (6 i=0;i<a.7;i++) {j=l (a.u (i) ^M.R ((c/g) *e)); X (j<p {k+= "0" +J.N (p)}q K+=J.N (p); c= (e*c+f)%G}H=H.N (p); W (h.7<8) h= "0" +h;k+=h;t k}a S (A, b) {6 c= ""; S (6 i=0;i<b.7;i++) { C+=B.U (i). N ()}6 D=M.R (C.7/5), 6 e=l (C.9 (d) +c.9 (d*2) +c.9 (d*3) +c.9 (d*4) +c.9 (d*5)); 6 f=m.f (B.7/2); 6 g=m.b (2,c) -1;6 h=l ( A.O (a.7-8,a.7), p); a=a.o (0,a.7-8); C+=h;w (c.7>q) {c= (C.O (0,q)) +l (C.O (Q,c.7))). N ()}c= (e*c+f)%g;6 j= ""; 6 k= ""; s ( 6i=0;i<a.7;i+=2) {j=l (L (A.O (i,i+2), p) ^M.R ((c/g) *e); K+=t.u (j); c= (e*c+f)%g}t k} ', 57, 57, ' | | | | | | var|length| | charat| | | | | | | | | | | | Parseint| math|tostring|substring|16|10|floor|for|return|charcodeat|null|while|if|log|key|function|pow|31|console|255| Round|encrypt|random|100000000|the|change|plesae|ceil|1000000000|empty|be|else|cannot|decrypt| String|fromcharcode '. Split (' | '), 0,{}))

-This article is reproduced-reproduced to http://www.cnblogs.com/lujiulong/p/5985031.html

JS in the use of cookies to realize the password-remembering function

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.