我已经在Google Apps脚本中测试了以下语句,但很惊讶它们产生了不同的结果

var a = Base64.encode(ciphertext);
var b = Utilities.base64Encode(ciphertext);


这是什么原因呢?

这是Base64的源代码(来自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;
}

最佳答案

我遇到了类似的问题。
可能是Utilities.base64Encode(ciphertext)具有10位数字的系统?

function test(){
  var email = '[email protected]';
  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;
}

关于javascript - 在Google Apps脚本和JavaScript中使用base64编码获得不同的结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20106482/

10-12 02:28