Reputation: 5634
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;
}
Upvotes: 2
Views: 3073
Reputation: 4979
I'm getting a like problem.
May be Utilities.base64Encode(ciphertext)
have a the 10-numeral system?
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;
}
Upvotes: 3
Reputation: 838
Try using the following library: http://jsbase64.codeplex.com/, it might solve your problem.
Upvotes: 1