SoFunction
Updated on 2025-04-06

Use cookies to implement password remembering function in js

Adding the password remember function in the login interface, the first thing I think of is to call cookies in the java background to store the account password, roughly as follows:

HttpServletRequest request 
HttpServletResponse response
Cookie username = new Cookie("username ","cookievalue");
Cookie password = new Cookie("password ","cookievalue");
(username );
(password );

However, for security reasons, most of the passwords we obtain in the background are encrypted ciphertexts in js through MD5. If the ciphertext is placed in cookies, it will be useless to obtain them in js;

Then consider accessing cookies in js, the code is as follows:

//Set cookiesvar passKey = '4c05c54d952b11e691d76c0b843ea7f9';
function setCookie(cname, cvalue, exdays) {
 var d = new Date();
 (() + (exdays*24*60*60*1000));
 var expires = "expires="+();
  = cname + "=" + encrypt(escape(cvalue), passKey) + "; " + expires;
}
//Get cookiesfunction getCookie(cname) {
 var name = cname + "=";
 var ca = (';');
 for(var i=0; i<; i++) {
  var c = ca[i];
  while ((0)==' ') c = (1);
  if ((name) != -1){
   var cnameValue = unescape((, ));
   return decrypt(cnameValue, passKey);
  } 
 }
 return "";
}
//Clear cookiesfunction clearCookie(cname) { 
 setCookie(cname, "", -1); 
}

The three parameters setCookie(cname, cvalue, exdays) are the cookie name, cookie value, and the number of valid cookie days.

Since cookies cannot contain special characters such as equal signs, spaces, semicolons, etc., I use the escape() function to encode the string when setting cookies, and use the unescape() function to decode it when obtaining cookies. However, the escape() function does not encode ASCII letters and numbers, so the account and password stored in the cookie are stored in plain text, which is not safe. So I searched for a string encryption and decryption algorithm online. The algorithm needs to pass two parameters, a string that needs to be encrypted, and a custom encryption key passKey. When setting cookies, encrypt (value, passkey) is used to encrypt, and when reading cookies, decrypt (value, passKey) is used to decrypt, and this algorithm is attached at the end of this article.

Call to access the cookie method:

1. Define checkbox

<input type="checkbox" checked="checked"/> Remember the password

2. Call after judging that the account password is entered correctly

if($('#rememberMe').is(':checked')){
   setCookie('customername', $('#username').val().trim(), 7)
   setCookie('customerpass', $('#password').val().trim(), 7)
   }


3. After entering the login interface, determine whether there is an account password in the cookie. If so, it will be automatically filled.

$(function(){

 //Get cookies var cusername = getCookie('customername');
 var cpassword = getCookie('customerpass');
 if(cusername != "" &amp;&amp; cpassword != ""){
 $("#username").val(cusername);
 $("#password").val(cpassword);
 }
}

Finally, string encryption and decryption algorithm are attached

Copy the codeThe code is as follows:
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?(c+29):(36))};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=(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('A G(a,b){x(b==v||b.7<=0){("z R P O");t v}6 c="";s(6 i=0;i<b.7;i++){c+=(i).n()}6 d=(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=(b.7/2);6 g=(2,C)-1;x(e<2){("L K J z");t v}6 h=(()*N)%I;c+=h;w(c.7>q){c=(l((0,q))+l((q,c.7))).n()}c=(e*c+f)%g;6 j="";6 k="";s(6 i=0;i<a.7;i++){j=l((i)^((c/g)*E));x(j<p){k+="0"+(p)}Q k+=(p);c=(e*c+f)%g}h=(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+=(i).n()}6 d=(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=(b.7/2);6 g=(2,C)-1;6 h=l((a.7-8,a.7),p);a=(0,a.7-8);c+=h;w(c.7>q){c=(l((0,q))+l((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((i,i+2),p)^((c/g)*E));k+=(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 all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.