cookie记住密码/base64加密(js控制)

• 配置cookie

     //设置cookie
    function setCookie ( name, value, expdays )
{
var expdate = new Date();
//设置Cookie过期日期
expdate.setDate(expdate.getDate() + expdays) ;
//添加Cookie并转码
document.cookie = encodeURI(name) + "=" + escape(value) + ";expires=" + expdate.toUTCString(); } //得到cookie
function getCookie ( name )
{
//获取name在Cookie中起止位置
var start = document.cookie.indexOf(name+"=") ;
if ( start != -1 )
{
start = start + name.length + 1 ;
//获取value的终止位置
var end = document.cookie.indexOf(";", start) ;
if ( end == -1 )
end = document.cookie.length ;
//截获cookie的value值,并返回
return unescape(document.cookie.substring(start,end)) ;
}
return "" ;
} //删除cookie
function delCookie ( name )
{
setCookie ( name, "", -1 ) ;
}

• 写入cookie

        //提交表单时触发
       function trans() { //base64加密
loginForm.password.value = base64encode(loginForm.password.value); //获取表单输入:用户名,密码,是否保存密码
var username = document.getElementById("username").value.trim() ;
var password = document.getElementById("password").value.trim() ;
var isRmbPwd = document.getElementById("isRmbPwd").checked ; //判断用户名,密码是否为空(全空格也算空)
if ( username.length != 0 && password.length != 0 )
{
//若复选框勾选,则添加Cookie,记录密码
if ( isRmbPwd == true )
{
var name = getCookie("username") ;
if(name != username) {
delCookie ("username") ;
delCookie (name) ;
}
setCookie ( "username", username, 30 ) ;
setCookie ( username, password, 30 ) ;
}
//否则清除Cookie
else
{
delCookie ( "username" ) ;
delCookie ( username ) ;
}
return true ;
} }

• 读取cookie

        //从Cookie获取到用户名
var username = getCookie("username") ;
//如果用户名为空,则给表单元素赋空值
if ( username == "" )
{
document.getElementById("username").value="" ;
document.getElementById("password").value="" ;
document.getElementById("isRmbPwd").checked=false ;
}
//获取对应的密码,并把用户名,密码赋值给表单
else
{
var pwd= getCookie(encodeURI(username)) ;
//base64解密
password = base64decode(pwd); document.getElementById("username").value = username ;
document.getElementById("password").value = password ;
document.getElementById("isRmbPwd").checked = true ;
}

• base64加密/解密方法

      //参数设置
     var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1); //加密方法
function base64encode(str) {
var out, i, len;
var c1, c2, c3;
len = str.length;
i = 0;
out = "";
while(i < len) {
c1 = str.charCodeAt(i++) & 0xff;
if(i == len) {
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt((c1 & 0x3) << 4);
out += "==";
break;
}
c2 = str.charCodeAt(i++);
if(i == len) {
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
out += base64EncodeChars.charAt((c2 & 0xF) << 2);
out += "=";
break;
}
c3 = str.charCodeAt(i++);
out += base64EncodeChars.charAt(c1 >> 2);
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
out += base64EncodeChars.charAt(c3 & 0x3F);
}
return out;
} //解密方法
function base64decode(str){
var c1, c2, c3, c4;
var i, len, out;
len = str.length;
i = 0;
out = "";
while (i < len) {
/* c1 */
do {
c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
}
while (i < len && c1 == -1);
if (c1 == -1)
break;
/* c2 */
do {
c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
}
while (i < len && c2 == -1);
if (c2 == -1)
break;
out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
/* c3 */
do {
c3 = str.charCodeAt(i++) & 0xff;
if (c3 == 61)
return out;
c3 = base64DecodeChars[c3];
}
while (i < len && c3 == -1);
if (c3 == -1)
break;
out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
/* c4 */
do {
c4 = str.charCodeAt(i++) & 0xff;
if (c4 == 61)
return out;
c4 = base64DecodeChars[c4];
}
while (i < len && c4 == -1);
if (c4 == -1)
break;
out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
}
return out;
}

• form表单

 <input id="username" name="username" type="text" class="txt"
value="" placeholder="请输入用户名" value="${username}"/> <input id="password" name="password" type="password"
class="txt" value="" placeholder="请输入密码 " /> <input type="checkbox" id="isRmbPwd" name="isRmbPwd" checked="checked">
<span class="isRmbPwdText">记住密码</span> <input type="submit" class="login_btn" value="登录" onclick="trans()" />

cookie记住密码/base64加密(js控制)的更多相关文章

  1. js实现cookie记住密码

    近来做记住密码时,用js的实现方式做了一下. login.jsp页面代码 <%@ page language="java" import="java.util.*& ...

  2. cookie记住密码功能

    很多门户网站都提供了记住密码功能,虽然现在的浏览器都已经提供了相应的记住密码功能 效果就是你每次进入登录页面后就不需要再进行用户名和密码的输入: 记住密码功能基本都是使用cookie来进行实现的,因此 ...

  3. vue中使用cookies和crypto-js实现记住密码和加密

    前端加密 使用crypto-js加解密 第一步,安装 npm install crypto-js 第二步,在你需要的vue组件内import import CryptoJS from "cr ...

  4. 锋利的Jquery之插件Cookie记住密码

    先下载Jquery cookie js ,下载路径: http://plugins.jquery.com/cookie/ 记住,jquery的包要放在cookie的包前面,否则会产生异常 <!D ...

  5. django删除表重建&修改用户密码&base64加密解密字符串&ps aux参数说明&各种Error例子

    1.django的queryset不支持负索引 AssertionError: Negative indexing is not supported. 2.django向前端JavaScript传递列 ...

  6. asp.net中用cookie记住密码上次不用登陆

    ------最佳解决方案--------------------写入CookieResponse.Cookies["UserName"].Value = "用户名&quo ...

  7. python+selenium利用cookie记住密码

    先上代码 1 from selenium import webdriver 2 from time import sleep 3 4 dr = webdriver.Chrome() 5 dr.get( ...

  8. 通过js来设置cookie和读取cookie,实现登陆时记住密码的功能

    function setCookie(){ //设置cookie var loginCode = $("#login_code").val(); //获取用户名信息 var pwd ...

  9. 【原创】js中利用cookie实现记住密码功能

    在登录界面添加记住密码功能,我首先想到的是在java后台中调用cookie存放账号密码,大致如下: HttpServletRequest request HttpServletResponse res ...

随机推荐

  1. 微信小程序websocket

    微信小程序websocket 微信小程序带有websocket可以提供使用,但是官方文档写的东西很少,而且小程序后台能力弱这一点也是十分的坑爹,这就导致了socket长连接一切后台就会出现断开的情况, ...

  2. Alpha 冲刺报告(5/10)

    Alpha 冲刺报告(5/10) 队名:洛基小队 峻雄(组长) 已完成:修改角色的移动脚本 明日计划:完善此项脚本 剩余任务:角色的属性脚本 困难:没有时间,代码的编写时间太慢 ----------- ...

  3. Ubuntu eclipse安装

    apt-get install eclipse eclipse-cdt eclipse-jdt  # don't include eclipse if you have it already afte ...

  4. [咸恩静][Good Bye]

    歌词来源:http://music.163.com/#/song?id=35437298 作曲 : 安英民 [作曲 : 安英民] 作词 : 安英民/로코 [作词 : 安英民/lo-Ko] 나를 떠나버 ...

  5. css-table属性运用

    最近在工作中遇到了一些不常用的布局,很多使用 CSS table 属性,并结合 ::before,::after 伪元素完成了,使得 HTML 的结构相对更简单,更具有语义性.当 HTML 结构越清晰 ...

  6. 64. [Mcoi2018]终末之诗(上)

    Description 求出\(k^{k^{k^{k^{...}}}} \pmod p\) 的结果 扩展欧拉定理:\[a^x=a^{min(x,x\%\varphi(p)+\varphi(p))}(m ...

  7. 10、Android--技巧

    10.1.全局获取Context的技巧 在实践中有很多的地方都可以使用到Context 弹出Toast的时候需要,启动活动的时候需要.发送广播的时候需要. 操作数据库的时候需要.使用通知的时候需要.. ...

  8. 1、Orcal下载安装步骤图文详解

    1.Orcal官方下载地址: https://www.oracle.com/technetwork/cn/database/enterprise-edition/downloads/index.htm ...

  9. sql三表查询

    情景: student  id    stname    sex score   scoreid  stname  birth course id    coursename  age 简单说明  a ...

  10. ZOJ 3872 浙江2015年省赛试题

    D - Beauty of Array Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu S ...