简体   繁体   中英

xpages base64 encode special characters

In an application where I make a base64 encoded Vcard, I have the problem that I can't encode special characters like in the string "Zone d'activité", which should become "Wm9uZSBkJ2FjdGl2aXTDqQ==' (encoded by using https://base64.guru/converter/encode )

I'm using the script I found on https://openntf.org/XSnippets.nsf/snippet.xsp?id=encode-decode-base-64 , which works fine for normal text to encode.

With this encoder I get "Wm9uZSBkJ2FjdGl2aXTp" for the above string,which isn't correct for my vcard. I guess I need to replace special characters like "'" or "é" with something else to get it encoded correctly, because base64 can only be normal characters...?

function base64_encode (data) {
// http://kevin.vanzonneveld.net
// +   original by: Tyler Akins (http://rumkin.com)
// +   improved by: Bayron Guevara
// +   improved by: Thunder.m
// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// +   bugfixed by: Pellentesque Malesuada
// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// +   improved by: Rafał Kukawski (http://kukawski.pl)
// *     example 1: base64_encode('Kevin van Zonneveld');
// *     returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
// mozilla has this native
// - but breaks in 2.0.0.12!
//if (typeof this.window['btoa'] == 'function') {
//    return btoa(data);
//}
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var o1, o2, o3, h1, h2, h3, h4, bits, i = 0,
ac = 0,
enc = "",
tmp_arr = [];

if (!data) {
return data;
}

do { // pack three octets into four hexets
o1 = data.charCodeAt(i++);
o2 = data.charCodeAt(i++);
o3 = data.charCodeAt(i++);

bits = o1 << 16 | o2 << 8 | o3;

h1 = bits >> 18 & 0x3f;
h2 = bits >> 12 & 0x3f;
h3 = bits >> 6 & 0x3f;
h4 = bits & 0x3f;

// use hexets to index into b64, and append result to encoded string
tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
} while (i < data.length);

enc = tmp_arr.join('');

var r = data.length % 3;

return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3);

}

var demo = "Zone d'activité"
var test = base64_encode(demo);
return test;

There is a solution and explanation for Unicode on MDN: https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem

function utf8_to_b64(str) {
  return window.btoa(unescape(encodeURIComponent(str)));
}

console.log(utf8_to_b64("Zone d'activité"));
//Wm9uZSBkJ2FjdGl2aXTDqQ==

I wouldn't start fiddling with the base64 encoding myself. I use code already made:-)

I have this simple code in Java:

import sun.misc.BASE64Encoder;
:
:
String txt = "xyz...";
BASE64Encoder base64encoder = new BASE64Encoder();
return base64encoder.encode(txt.getBytes());

If you really want to do it in SSJS then you can call Java classes directly. But I prefer to code such things in a Javaclass - and then you could just call that method directly if you need it in SSJS, like:

package com.myapp;
import sun.misc.BASE64Encoder;
public class Utils {
    public static String base64Encode(String txt) {
        BASE64Encoder base64encoder = new BASE64Encoder();
        return base64encoder.encode(txt.getBytes());
    }
}

And then you would call that from your SSJS like:

var txt = com.myapp.Utils.base64Encode("The text to encode...");

I haven't checked syntax of the samples - just written directly here, so you may need to correct it slightly:-)

/John

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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