您的位置:首页 > Web前端 > JavaScript

js base32支持动态编码表,动态创建解码表,支持加密解密!

2015-03-30 13:55 267 查看
在网上找到的都不支持修改编码表,只能使用默认的编码表在使用的自由度和安全度是不够的!所以就特地弄了一个js base32支持动态编码表(当然也支持默认的编码表),动态创建解码表,支持加密解密,并且进行了扩展,解决编码字符类似产生相同的加密字符,以js的类实现!

//Base32加密,Created by Neko!
function Base32(Keys) {
if (!(this instanceof Base32)) {
return new Base32();
}
this.init(Keys);
return this;
}
Base32.prototype = {
alphabet:'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
alias:{ o:0, i:1, l:1, s:5 },
lookupDecode:{},
output:'',
bits:0,
skip:0,
byte:0,
init:function(alphabet){
if(alphabet!=undefined)
this.alphabet=alphabet;
this.lookupDecode=this.lookup();
},
lookup:function() {
var table = {};
// Invert 'alphabet'
for (var i = 0; i < this.alphabet.length; i++) {
table[this.alphabet[i]] = i;
}
// Splice in 'alias'
for (var key in this.alias) {
if (!this.alias.hasOwnProperty(key))
continue;
table[key] = table['' + this.alias[key]];
}
return table;
},
readByte:function(byte) {
// 读取字节
if (typeof byte == 'string')
byte = byte.charCodeAt(0);

if (this.skip < 0) {
this.bits |= (byte >> (-this.skip));
} else {
this.bits = (byte << this.skip) & 248;
}
if (this.skip > 3) {
this.skip -= 8;
return 1;
}
if (this.skip < 4) {
this.output += this.alphabet[this.bits >> 3];
this.skip += 5;
}
return 0;
},
finish:function(check) {
var output = this.output + (this.skip < 0 ? this.alphabet[this.bits >> 3] : '') + (check ? '$' : '')
this.output = '';
return output;
},
getEncodeString:function(input) {
input=this.enDeCodeString(input);
this.skip=0,this.bits=0,this.output='';
for (var i = 0; i < input.length; ) {
i += this.readByte(input[i]);
}
var output ;
output = this.finish();
return output;
},
enDeCodeString:function(srcString){
//加密解密,模拟随机
var szRet=srcString[0];//获取第一个字符
var nKey=srcString.charCodeAt(0);//得到第一个字符的数字
if(nKey<(srcString.length-1)){
nKey+=srcString.length;
}
for(var i=0;i<srcString.length;i++){
if(i==0)
continue;
szRet+=String.fromCharCode(srcString.charCodeAt(i)^nKey);
nKey--;
}
return szRet;
},
readChar:function(char) {
//读取字符
if (typeof char != 'string'){
if (typeof char == 'number') {
char = String.fromCharCode(char);
}
}
//char = char.toLowerCase()
var val = this.lookupDecode[char];
if (typeof val == 'undefined') {
return;
}
val <<= 3; // 移动到最高位
this.byte |= val >>> this.skip;
this.skip += 5;
if (this.skip >= 8) {
this.output += String.fromCharCode(this.byte);
this.skip -= 8;
if (this.skip > 0)
this.byte = (val << (5 - this.skip)) & 255;
else
this.byte = 0;
}
},
getDecodeData:function(input) {
//返回原始的数据
this.skip=0,this.byte=0,this.output='';
for (var i = 0; i < input.length; i++) {
this.readChar(input[i]);
}
var output ;
output = this.finish();
output=this.enDeCodeString(output);
return output;
},
getDecodeString:function(input) {
//返回字符串
var outputTemp,output='';
outputTemp = this.getDecodeData(input);
for(var i=0;i<outputTemp.length;i++){
var szTemp=outputTemp.charCodeAt(i).toString(16);
if(szTemp.length<2){
szTemp='0'+szTemp;

}
szTemp=szTemp.toUpperCase();
output+=szTemp;
}
return output;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐