//base64加密 解密

/* //1.加密
var result = Base.encode('125中文'); //--> "MTI15Lit5paH" //2.解密
var result2 = Base.decode(result); //--> '125中文'
*/ ~(function(root, factory) {
if (typeof define === "function" && define.amd) {
define([], factory);
} else if (typeof module === "object" && module.exports) {
module.exports = factory();
} else {
root.Base = factory();
}
}(this, function() {
'use strict'; function Base64() {
// private property
this._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
}
//public method for encoding
Base64.prototype.encode = function (input) {
var output = "", chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0;
input = this._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
}
return output;
} // public method for decoding
Base64.prototype.decode = function (input) {
var output = "", chr1, chr2, chr3, enc1, enc2, enc3, enc4, i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = this._utf8_decode(output);
return output;
} // private method for UTF-8 encoding
Base64.prototype._utf8_encode = function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
} }
return utftext;
} // private method for UTF-8 decoding
Base64.prototype._utf8_decode = function (utftext) {
var string = "", i = 0, c = 0, c1 = 0, c2 = 0, c3 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
} var Base = new Base64(); return Base;
}));

js中实现base64加密、解密的更多相关文章

  1. js中变量base64加密传输

    首先对base64进行定义: var Base64 = { _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01 ...

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

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

  3. .NET中进行Base64加密解密

    方法一: /// <summary> /// Base64加密 /// </summary> /// <param name="Message"> ...

  4. python中的base64加密解密

    介绍 Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法.可查看RFC2045-RFC2049,上面有MIME的详细规范. ...

  5. JS实现base64加密解密

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

  6. js base64加密解密

    var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ...

  7. c#中base64加密解密

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

  8. password学3——Java BASE64加密解密

    Base64是网络上最常见的用于传输8Bit字节代码的编码方式之中的一个,大家能够查看RFC2045-RFC2049.上面有MIME的具体规范.Base64编码可用于在HTTP环境下传递较长的标识信息 ...

  9. 转 关于Https协议中的ssl加密解密流程

    关于Https协议中的ssl加密解密流程 2016年09月28日 09:51:15 阅读数:14809 转载自:http://www.cnblogs.com/P_Chou/archive/2010/1 ...

随机推荐

  1. Greenplum 行存、列存,堆表、AO表的原理和选择

    转载自: https://github.com/digoal/blog/blob/master/201708/20170818_02.md?spm=a2c4e.11153940.blogcont179 ...

  2. zabbix的日志监控

    前提条件是该日志文件对于启动zabbix agent进程的用户开启了可读权限,而且该日志的路径对该用户开方x权限,让能取到这个日志文件 第一个参数可以不用引号,前提是zabbix用户可以进入文件路径, ...

  3. 二维bit模板

    #include<bits/stdc++.h> using namespace std; typedef long long ll; #define N 1100 const int mo ...

  4. leetcode解题报告(18):Contains Duplicate

    描述 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  5. [信息收集]Nmap命令详解

    0x00[介绍] Nmap,也就是Network Mapper,中文为"网络映射器". Nmap是一款开源的网络探测和安全审核的工具,它的设计目标是快速地扫描大型网络. 它是网络管 ...

  6. linux redhat 安装了jdk检查版本不是自己安装的版本的解决办法

    Linux下安装jdk java -version 不是自己所需要的版本 设置环境变量,这是最重要的 在etc/profile文件下添加 export JAVA_HOME=/usr/java/jdk1 ...

  7. HttpClient学习(一)—— 基本使用

    HttpClient是支持Http协议的客户端编程工具包. 一.简单使用 1.1 引入依赖 <dependency> <groupId>org.apache.httpcompo ...

  8. Java核心复习——CompletableFuture

    介绍 JDK1.8引入CompletableFuture类. 使用方法 public class CompletableFutureTest { private static ExecutorServ ...

  9. mybatis的maven配置

    <!-- mybatis依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId ...

  10. Oracle 存储过程—为数传递变量

    oracle 存储过程的基本语法create or replace procedure proc1( p_para1 varchar2, p_para2 out varchar2, p_para3 i ...