简体   繁体   English

Google Apps脚本和JavaScript中使用base64编码的结果不同

[英]Different results with base64 encoding in Google Apps Scripts and JavaScript

I have tested the following statements in Google Apps Scripts and were surprised that they yield different results 我在Google Apps脚本中测试了以下语句,并对它们产生不同结果感到惊讶

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的源代码(来自开源项目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? 可能是Utilities.base64Encode(密文)有一个10位数的系统?

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;
}

尝试使用以下库: http//jsbase64.codeplex.com/ ,它可能会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM