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

在网上找到的都不支持修改编码表,只能使用默认的编码表在使用的自由度和安全度是不够的!所以就特地弄了一个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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值