I used this function to encode a string using base64 encoding for my java card applet.
public static String base64Encode(String s) {
String table="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ "abcdefghijklmnopqrstuvwxyz0123456789+/";
String b="";
String r="";
int pad;
for (int c : s.toCharArray()) {
b+=rjust(Integer.toBinaryString(c), 8, "0");
}
for (int i=0; i<b.length(); i+=6) {
r+=table.charAt(Integer.parseInt(ljust(b.substring(i, i+6), 6, "0"), 2));
pad=6-b.substring(i, i+6).length();
switch (pad) {
case 0: r+=""; break;
case 4: r+="=="; break;
case 2: r+="="; break; }
}
return r;
}
Both rjust and ljust that you can find in the function above are custom functions similar to python's rjust and ljust.
No comments:
Post a Comment