不同的结果与在谷歌Apps脚本和JavaScript base64编码 [英] Different results with base64 encoding in Google Apps Scripts and JavaScript
本文介绍了不同的结果与在谷歌Apps脚本和JavaScript base64编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经测试在谷歌Apps脚本下面的语句,并感到惊讶的是,他们产生不同的结果。
VAR一个= Base64.en code(密文);
变种B = Utilities.base64En code(密文);
是什么原因?
这是源$ C $ C为Base64编码(从开源项目网址:)
/ * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * /
/ *的Base64类:Base 64编码/解码(三)克里斯俯伏2002-2012 * /
/ *注:取决于UTF8类* /
/ * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - * /VAR的Base64 = {}; //命名空间的Base64。Base64编码code =ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 + / =;/ **
*恩code字符串转换成Base64编码,由RFC 4648中定义[http://tools.ietf.org/html/rfc4648]
*(实例方法扩展String对象)。按照RFC 4648,不换行补充说。
*
* @参数{}字符串str中的字符串是EN codeD为基础-64
* @参数{}布尔[utf8en code =假]标志,指示海峡是否是单向code字符串是EN codeD
*转换为Base64之前为UTF8;否则串被假定为8位的字符
* @Returns {}字符串的Base64恩codeD字符串
* /
Base64.en code =功能(STR,utf8en code){// http://tools.ietf.org/html/rfc4648
utf8en code =(typeof运算utf8en code ==未定义)?假:utf8en code;
VAR O1,O2,O3,位,H1,H2,H3,H4,E = [],垫='',C,平原,codeD;
。VAR B64 =的Base64 code; 纯= utf8en code? str.en codeUTF8():STR; C = plain.length%3; //垫串3的倍数的长度
如果(c取代; 0){而(C ++ 3;){垫+ ='=';平原+ ='\\ 0'; }}
//注意:在这里做填充为我们节省了做特殊装箱尾随1或2个字符 为(C = 0;℃下plain.length; C + = 3){//包三个字节分为四个hexets
O1 = plain.char codeAT(C);
O2 = plain.char codeAT(C + 1);
O3 = plain.char codeAT(C + 2); 位= 01<< 16 | O2<< 8 | O3; H1 =比特GT;&GT 18安培; 0x3F的;
H2 =比特GT;&GT 12安培; 0x3F的;
H3 =比特GT;→6&放大器; 0x3F的;
H4 =比特和放大器; 0x3F的; //使用hextets索引到$ C $ C字符串
E [C / 3] = b64.charAt(H1)+ b64.charAt(H2)+ b64.charAt(H3)+ b64.charAt(H4);
}
codeD = e.join(''); // join()方法远比IE重复字符串连接速度更快 //替换从填充空值的与'=的
codeD = coded.slice(0,coded.length-pad.length)+垫; 返回codeD;
}
解决方案
我得到一个类似的问题。
可能是Utilities.base64En code(密文)有10数字系统?
函数test(){
VAR电子邮件='email@gmail.com';
VAR T = Utilities.base64En code(电子邮件,Utilities.Charset.UTF_8);
Logger.log(T);
Logger.log(Utilities.base64De code(T,Utilities.Charset.UTF_8));
Logger.log(bin2String(Utilities.base64De code(T,Utilities.Charset.UTF_8)));
}
功能bin2String(阵列){
VAR的结果=;
对于(VAR I = 0; I< array.length,我++){
Logger.log(数组[我]);
结果+ = String.fromChar code(parseInt函数(数组[我],10));
}
返回结果;
}
I have tested the following statements in Google Apps Scripts and were surprised that they yield different results
var a = Base64.encode(ciphertext);
var b = Utilities.base64Encode(ciphertext);
What is the reason for this?
This is the source code for Base64 (from open source project URL:)
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Base64 class: Base 64 encoding / decoding (c) Chris Veness 2002-2012 */
/* note: depends on Utf8 class */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
var Base64 = {}; // Base64 namespace
Base64.code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
/**
* Encode string into Base64, as defined by RFC 4648 [http://tools.ietf.org/html/rfc4648]
* (instance method extending String object). As per RFC 4648, no newlines are added.
*
* @param {String} str The string to be encoded as base-64
* @param {Boolean} [utf8encode=false] Flag to indicate whether str is Unicode string to be encoded
* to UTF8 before conversion to base64; otherwise string is assumed to be 8-bit characters
* @returns {String} Base64-encoded string
*/
Base64.encode = function(str, utf8encode) { // http://tools.ietf.org/html/rfc4648
utf8encode = (typeof utf8encode == 'undefined') ? false : utf8encode;
var o1, o2, o3, bits, h1, h2, h3, h4, e=[], pad = '', c, plain, coded;
var b64 = Base64.code;
plain = utf8encode ? str.encodeUTF8() : str;
c = plain.length % 3; // pad string to length of multiple of 3
if (c > 0) { while (c++ < 3) { pad += '='; plain += '\0'; } }
// note: doing padding here saves us doing special-case packing for trailing 1 or 2 chars
for (c=0; c<plain.length; c+=3) { // pack three octets into four hexets
o1 = plain.charCodeAt(c);
o2 = plain.charCodeAt(c+1);
o3 = plain.charCodeAt(c+2);
bits = o1<<16 | o2<<8 | o3;
h1 = bits>>18 & 0x3f;
h2 = bits>>12 & 0x3f;
h3 = bits>>6 & 0x3f;
h4 = bits & 0x3f;
// use hextets to index into code string
e[c/3] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
}
coded = e.join(''); // join() is far faster than repeated string concatenation in IE
// replace 'A's from padded nulls with '='s
coded = coded.slice(0, coded.length-pad.length) + pad;
return coded;
}
解决方案
I'm getting a like problem. May be Utilities.base64Encode(ciphertext) have a the 10-numeral system?
function test(){
var email = 'email@gmail.com';
var t = Utilities.base64Encode(email, Utilities.Charset.UTF_8);
Logger.log(t);
Logger.log(Utilities.base64Decode(t, Utilities.Charset.UTF_8));
Logger.log(bin2String(Utilities.base64Decode(t, Utilities.Charset.UTF_8)));
}
function bin2String(array) {
var result = "";
for (var i = 0; i < array.length; i++) {
Logger.log(array[i]);
result += String.fromCharCode(parseInt(array[i], 10));
}
return result;
}
这篇关于不同的结果与在谷歌Apps脚本和JavaScript base64编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文