cookie记住密码/base64加密(js控制)
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控制)的更多相关文章
- js实现cookie记住密码
近来做记住密码时,用js的实现方式做了一下. login.jsp页面代码 <%@ page language="java" import="java.util.*& ...
- cookie记住密码功能
很多门户网站都提供了记住密码功能,虽然现在的浏览器都已经提供了相应的记住密码功能 效果就是你每次进入登录页面后就不需要再进行用户名和密码的输入: 记住密码功能基本都是使用cookie来进行实现的,因此 ...
- vue中使用cookies和crypto-js实现记住密码和加密
前端加密 使用crypto-js加解密 第一步,安装 npm install crypto-js 第二步,在你需要的vue组件内import import CryptoJS from "cr ...
- 锋利的Jquery之插件Cookie记住密码
先下载Jquery cookie js ,下载路径: http://plugins.jquery.com/cookie/ 记住,jquery的包要放在cookie的包前面,否则会产生异常 <!D ...
- django删除表重建&修改用户密码&base64加密解密字符串&ps aux参数说明&各种Error例子
1.django的queryset不支持负索引 AssertionError: Negative indexing is not supported. 2.django向前端JavaScript传递列 ...
- asp.net中用cookie记住密码上次不用登陆
------最佳解决方案--------------------写入CookieResponse.Cookies["UserName"].Value = "用户名&quo ...
- python+selenium利用cookie记住密码
先上代码 1 from selenium import webdriver 2 from time import sleep 3 4 dr = webdriver.Chrome() 5 dr.get( ...
- 通过js来设置cookie和读取cookie,实现登陆时记住密码的功能
function setCookie(){ //设置cookie var loginCode = $("#login_code").val(); //获取用户名信息 var pwd ...
- 【原创】js中利用cookie实现记住密码功能
在登录界面添加记住密码功能,我首先想到的是在java后台中调用cookie存放账号密码,大致如下: HttpServletRequest request HttpServletResponse res ...
随机推荐
- SQL Server中搜索特定的对象
一.注释中带某关键字的对象 主要用到 sys.tables .sys.columns .sys.procedures 系统对象表以及sys.extended_properties 扩展属性表 --查 ...
- 团队作业4(Alpha版本)
项目名称:音乐播放器 项目成员: 张慧敏(201421122032) 苏晓薇(201421031033) 欧阳时康(201421122050) 会议记录: 主要讨论任务的分配和实现过程中已实现和未实 ...
- markdownpad 2 pro版本 注册码
注册email: www.zixue.it 注册码: 4vuvQFtGkF0oH7by922v75FtaUGq7niFveCKDxqC2KSqYTfaSGzxzxKQXNhc2BG51N9URrF7 ...
- OOP——构造函数、析构函数
我们在创建和销毁对象时需要执行一些任务.例如,在创建对象时给属性赋值,在对象销毁时关闭数据连接等,这时就需要构造函数和析构函数. 在PHP中构造函数和析构函数是固定的,如下: // 构造函数 func ...
- P4053 [JSOI2007]建筑抢修
题目描述 小刚在玩JSOI提供的一个称之为“建筑抢修”的电脑游戏:经过了一场激烈的战斗,T部落消灭了所有z部落的入侵者.但是T部落的基地里已经有N个建筑设施受到了严重的损伤,如果不尽快修复的话,这些建 ...
- Hadoop学习之路(十三)MapReduce的初识
MapReduce是什么 首先让我们来重温一下 hadoop 的四大组件: HDFS:分布式存储系统 MapReduce:分布式计算系统 YARN:hadoop 的资源调度系统 Common:以上三大 ...
- Linux终端里的记录器
我们在调试程序的时候,免不了要去抓一些 log ,然后进行分析. 如果 log 量不是很大的话,那很简单,只需简单的复制粘贴就好. 但是如果做一些压力测试,产生大量 log ,而且系统内存又比较小(比 ...
- Python中乘法
1.numpy乘法运算中"*"或multiply(),是数组元素逐个计算,具体代码如下: import numpy as np # 2-D array: 2 x 3 two_dim ...
- Kafka设计解析(六)Kafka高性能架构之道
转载自 技术世界,原文链接 Kafka设计解析(六)- Kafka高性能架构之道 本文从宏观架构层面和微观实现层面分析了Kafka如何实现高性能.包含Kafka如何利用Partition实现并行处理和 ...
- 洛谷P3382 【模板】三分法(三分)
题目描述 如题,给出一个N次函数,保证在范围[l,r]内存在一点x,使得[l,x]上单调增,[x,r]上单调减.试求出x的值. 输入输出格式 输入格式: 第一行一次包含一个正整数N和两个实数l.r,含 ...