JS in the use of cookies to remember the password function _javascript skills

Source: Internet
Author: User
Tags decrypt set cookie setcookie

In the login interface to add remember the password function, my first thought is to invoke cookies in the Java background to store account passwords, roughly as follows:

HttpServletRequest Request 
httpservletresponse response
Cookie username = new Cookie ("username", "cookievalue ");
Cookie password = new cookie ("Password", "Cookievalue");
Response.addcookie (username);
Response.addcookie (password);

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

Then consider accessing cookies in JS with the following code:

Set Cookie
var passkey = ' 4c05c54d952b11e691d76c0b843ea7f9 ';
function Setcookie (CNAME, cvalue, exdays) {
  var d = new Date ();
  D.settime (D.gettime () + (exdays*24*60*60*1000));
  var expires = "expires=" +d.toutcstring ();
  Document.cookie = cname + "=" + Encrypt (Escape (cvalue), passkey) + ";" + Expires;
}
Gets
the cookie function GetCookie (CNAME) {
  var name = cname + "=";
  var ca = Document.cookie.split (';');
  for (var i=0; i<ca.length; i++) {
    var c = ca[i];
    while (C.charat (0) = = ') c = c.substring (1);
    if (c.indexof (name)!=-1) {
     var cnamevalue = unescape (c.substring (Name.length, c.length));
     Return decrypt (Cnamevalue, passkey);
    }
  Return "";
}
Clears the cookie 
function ClearCookie (CNAME) { 
  Setcookie (CNAME, "",-1); 
}

Setcookie (CNAME, cvalue, exdays) Three parameters are stored cookie name, cookie value, cookie valid number of days

Since cookies cannot contain special characters such as equals, spaces, semicolons, and so on, I use the escape () function to encode a cookie when I set it, and I use the unescape () function to decode it when I get the cookie. But the escape () function does not encode ASCII letters and numbers, so the account, password, stored in the cookie, is stored in plaintext and is unsafe. So the internet to find a string encryption decryption algorithm, the algorithm needs to pass two parameters, a need to encrypt the string, a custom encryption key passkey. When you set up cookies using Encrypt (value, passkey) encryption, you use decrypt (value, passkey) to decrypt the cookie when you read it, which is attached at the end of this article.

Call to access Cookie method:

1. Define CheckBox

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

2, to determine the number of password input correct after the call

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


3, enter the login interface, to determine whether the cookie has an account password, if there will automatically fill

$ (function () {

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

Finally attach string encryption and decryption algorithm

Copy Code code as follows:
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= (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+=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,{})

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.