Base32 编解码的代码,在Aardio中,目前没有现成的代码或库可以用(好像是这样吧……)
然后就仿着其他代码写了一段出来,直接放代码吧
Base32_encode = function(data){
var bin = {};
var len = 0;
for(i=1;#data;1){
bin[i] = string.format("%08b",data[i])
}
var res = string.join(bin,"");
len = #res % 5;
var resdata = "";
if(len > 0){
resdata = tostring(math.floor(10 ** (5 - len)))
resdata = string.right(resdata,#resdata - 1);
}
res = res ++ resdata;
var stan = "";
if(#res % 40 > 0){
var len2 = (40 - #res % 40) / 5;
for(i=1;len2;1){
stan = stan + "="
}
}
var data_str = "";
var len3 = #res / 5;
for(i=1;len3;1){
data_str = data_str ++ Base32Tab[tonumber(GetStrCenter(res,i * 5 - 4,5),2) + 1];
}
data_str = data_str ++ stan;
return data_str;
}
Base32_decode = function(data){
var restemp = "";
var intemp = string.replace(data,"=","");
var index = 0;
for(i=1;#intemp;1){
index = table.indexOf(Base32Tab,GetStrCenter(intemp,i,1)) - 1;
if(index == -2){
return "";
}
restemp = restemp ++ string.right(string.format("%08b",index),5)
}
var comlement = tostring(math.floor(10 ** (8 - #restemp % 8)));
restemp = restemp ++ string.right(comlement,#comlement - 1);
var restext = "";
var ilen = #restemp / 8;
for(i=1;ilen;1){
restext = restext ++ tonumber(GetStrCenter(restemp,i * 8 - 7,8),2) ++ ",";
}
restext = string.left(restext,#restext - 1);
restext = "{" ++ restext ++ "}"
return BinToStr(restext);
}
调用方式:
Base32_encode(str); //加密
Base32_decode(str); //解密