var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, , -, -, -, ,
, , , , , , , , , , -, -, -, -, -, -,
-, , , , , , , , , , , , , , , ,
, , , , , , , , , , , -, -, -, -, -,
-, , , , , , , , , , , , , , , ,
, , , , , , , , , , , -, -, -, -, -); function base64encode(str) {
var out, i, len;
var c1, c2, c3; len = str.length;
i = ;
out = "";
while(i < len) {
c1 = str.charCodeAt(i++) & 0xff;
if(i == len)
{
out += base64EncodeChars.charAt(c1 >> );
out += base64EncodeChars.charAt((c1 & 0x3) << );
out += "==";
break;
}
c2 = str.charCodeAt(i++);
if(i == len)
{
out += base64EncodeChars.charAt(c1 >> );
out += base64EncodeChars.charAt(((c1 & 0x3)<< ) | ((c2 & 0xF0) >> ));
out += base64EncodeChars.charAt((c2 & 0xF) << );
out += "=";
break;
}
c3 = str.charCodeAt(i++);
out += base64EncodeChars.charAt(c1 >> );
out += base64EncodeChars.charAt(((c1 & 0x3)<< ) | ((c2 & 0xF0) >> ));
out += base64EncodeChars.charAt(((c2 & 0xF) << ) | ((c3 & 0xC0) >>));
out += base64EncodeChars.charAt(c3 & 0x3F);
}
return out;
} function base64decode(str) {
var c1, c2, c3, c4;
var i, len, out; len = str.length;
i = ;
out = "";
while(i < len) {
/* c1 */
do {
c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
} while(i < len && c1 == -);
if(c1 == -)
break; /* c2 */
do {
c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
} while(i < len && c2 == -);
if(c2 == -)
break; out += String.fromCharCode((c1 << ) | ((c2 & 0x30) >> )); /* c3 */
do {
c3 = str.charCodeAt(i++) & 0xff;
if(c3 == )
return out;
c3 = base64DecodeChars[c3];
} while(i < len && c3 == -);
if(c3 == -)
break; out += String.fromCharCode(((c2 & 0XF) << ) | ((c3 & 0x3C) >> )); /* c4 */
do {
c4 = str.charCodeAt(i++) & 0xff;
if(c4 == )
return out;
c4 = base64DecodeChars[c4];
} while(i < len && c4 == -);
if(c4 == -)
break;
out += String.fromCharCode(((c3 & 0x03) << ) | c4);
}
return out;
} function utf16to8(str) {
var out, i, len, c; out = "";
len = str.length;
for(i = ; i < len; i++) {
c = str.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += str.charAt(i);
} else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> ) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> ) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> ) & 0x3F));
} else {
out += String.fromCharCode(0xC0 | ((c >> ) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> ) & 0x3F));
}
}
return out;
} function utf8to16(str) {
var out, i, len, c;
var char2, char3; out = "";
len = str.length;
i = ;
while(i < len) {
c = str.charCodeAt(i++);
switch(c >> )
{
case : case : case : case : case : case : case : case :
// 0xxxxxxx
out += str.charAt(i-);
break;
case : case :
// 110x xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x1F) << ) | (char2 & 0x3F));
break;
case :
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = str.charCodeAt(i++);
char3 = str.charCodeAt(i++);
out += String.fromCharCode(((c & 0x0F) << ) |
((char2 & 0x3F) << ) |
((char3 & 0x3F) << ));
break;
}
} return out;
} function CharToHex(str) {
var out, i, len, c, h;
out = "";
len = str.length;
i = ;
while(i < len)
{
c = str.charCodeAt(i++);
h = c.toString();
if(h.length < )
h = "" + h; out += "\\x" + h + " ";
if(i > && i % == )
out += "\r\n";
} return out;
} function doEncode() {
var src = document.getElementById('src').value;
document.getElementById('dest').value = base64encode(utf16to8(src)); //调用这里加密
} function doDecode() {
var src = document.getElementById('src').value;
var opts = document.getElementById('opt'); if(opts.checked)
{
document.getElementById('dest').value = CharToHex(base64decode(src)); //调用这里解密
}
else
{
document.getElementById('dest').value = utf8to16(base64decode(src));
}
}
//window.BASE64 = __BASE64;
function ss(){
return '';
}

js  base64加密解密

js base64加密解密的更多相关文章

  1. js base64加密,后台解密

    这是为了解决页面发送post请求,传输密码,在页面的控制台可以看到密码的明文,所以先用base64把要传输的密码转换为非明文,然后在后台解密处理. base64encode.js // base64加 ...

  2. JS实现base64加密解密

    JS实现base64加密解密 转载自http://blog.csdn.net/fengzheng0306/archive/2006/04/25/676055.aspx 方法一: <HTML> ...

  3. 使用jframe编写一个base64加密解密工具

    该工具可以使用exe4j来打包成exe工具(如何打包自己百度) 先上截图功能 运行main方法后,会弹出如下窗口 输入密文 然后点击解密,在点格式化 代码分享 package tools;import ...

  4. Java 和JS Base64加密

    项目在登录.注册等场景实现时,经常会用到用户信息前端加密,然后项目后端二次解密,避免信息直接在浏览器上以明文显示. 本文主要介绍了base64加密的方式处理代码,不支持中文 源码如下: base64. ...

  5. 【代码笔记】iOS-3DES+Base64加密解密

    一,工程目录. 二,代码. RootViewController.m #import "RootViewController.h" #import "NSString+T ...

  6. 实现Base64加密解密

    using System; using System.Text;   namespace Common { /// <summary> /// 实现Base64加密解密 /// </ ...

  7. Java中使用BASE64加密&解密

    package com.bao.tools.encryption; import java.io.IOException; import org.junit.Test; import sun.misc ...

  8. Base64加密解密原理以及代码实现(VC++)

    Base64加密解密原理以及代码实现 转自:http://blog.csdn.net/jacky_dai/article/details/4698461 1. Base64使用A--Z,a--z,0- ...

  9. Java Base64 加密解密

    使用JDK的类 BASE64Decoder  BASE64Encoder package test; import sun.misc.BASE64Decoder; import sun.misc.BA ...

随机推荐

  1. redis 同步化操作

    异步化操作是很麻烦的的.不好控.下面介绍个同步化的库bluebird.用法很简单.看下你还子就知道了 const redis = require('redis'); const bluebird = ...

  2. 使用python操作Memcache、Redis、RabbitMQ、

    Memcache 简述: Memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要 ...

  3. idea tomcat debug 失效

    idea 开发神器 有时候遇到各种问题 这不 现在遇到了一个问题 启动容器时 debug断点不能进入 在网上找了老半天 终于找到答案了 原因是使用tomcat的时候 没有选择"pass en ...

  4. hdu3507 Print Article(斜率优化入门)(pascal)

    Problem Description Zero has an old printer that doesn't work well sometimes. As it is antique, he s ...

  5. Eclipse 保存代码时,不自动换行设置

    Eclipse在保存代码时,总是自动换行.尤其是注释,换行后的注释读起来就很混乱.后来发现是在保存文件时设置了自动格式化代码的原因. 关闭自动格式代码设置: windows-->Preferen ...

  6. Educational Codeforces Round 33 (Rated for Div. 2) 题解

    A.每个状态只有一种后续转移,判断每次转移是否都合法即可. #include <iostream> #include <cstdio> using namespace std; ...

  7. Sum of Consecutive Integers LightOJ - 1278(推公式 数学思维)

    原文地址:https://blog.csdn.net/qq_37632935/article/details/79465213 给你一个数n(n<=10^14),然后问n能用几个连续的数表示; ...

  8. 【刷题】BZOJ 1195 [HNOI2006]最短母串

    Description 给定n个字符串(S1,S2,„,Sn),要求找到一个最短的字符串T,使得这n个字符串(S1,S2,„,Sn)都是T的子串. Input 第一行是一个正整数n(n<=12) ...

  9. 洛谷P1268 树的重量 【构造 + 枚举】

    题目描述 树可以用来表示物种之间的进化关系.一棵"进化树"是一个带边权的树,其叶节点表示一个物种,两个叶节点之间的距离表示两个物种的差异.现在,一个重要的问题是,根据物种之间的距离 ...

  10. POJ.3894 迷宫问题 (BFS+记录路径)

    POJ.3894 迷宫问题 (BFS+记录路径) 题意分析 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, ...