Question

I am looking to write an encode function in javacard (or even java would be fine) that would base 64 URL encode (where the '+' and '/' characters of standard Base64 are respectively replaced by '-' and '_') the given data with no "=" padding. Here is the code I have so far, but I suspect something to be wrong as instead of taking out the padding it is leaving an empty character. However when I use a decoder, it still works fine...

// Mapping table from 6-bit nibbles to Base64 characters.
private static final byte[] map1 = {(byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) '-', (byte) '_'};
private static final byte[] map2 = {(byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F', (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K', (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P', (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U', (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z', (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e', (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j', (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o', (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't', (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y', (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3', (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8', (byte) '9', (byte) '+', (byte) '/'};
public static final short URLENCODINGNOPADDING = 1;

 /**
 * Encodes a byte array into Base64 format. No blanks or line breaks are
 * inserted in the output.
 *
 * @param in An array containing the data bytes to be encoded.
 * @param iOff Offset of the first byte in <code>in</code> to be processed.
 * @param iLen Number of bytes to process in <code>in</code>, starting * *
 * at <code>iOff</code>.
 * @param type Type of Encoding to be done (e.g. URLNOPADDING)
 * @return A byte array containing the Base64 encoded data.
 */
public byte[] encode(byte[] in, short iOff, short iLen, short type) {
    short oDataLen = (short) ((iLen * 4 + 2) / 3);       // output length without padding
    short oLen = (short) (((iLen + 2) / 3) * 4);         // output length including padding
    byte[] out = new byte[oLen];
    short ip = iOff;
    short iEnd = (short) (iOff + iLen);
    short op = 0;
    while (ip < iEnd) {
        short i0 = (short) (in[ip++] & 0xff);
        short i1 = (short) (ip < iEnd ? in[ip++] & 0xff : 0);
        short i2 = (short) (ip < iEnd ? in[ip++] & 0xff : 0);
        short o0 = (short) (i0 >>> 2);
        short o1 = (short) (((i0 & 3) << 4) | (i1 >>> 4));
        short o2 = (short) (((i1 & 0xf) << 2) | (i2 >>> 6));
        short o3 = (short) (i2 & 0x3F);
        if (type == (short) 1) {
            out[op++] = map2[o0];
            out[op++] = map2[o1];
            out[op] = op < oDataLen ? (byte) map2[o2] : (byte) '=';
            op++;
            out[op] = op < oDataLen ? (byte) map2[o3] : (byte) '=';
            op++;
        } else {
            out[op++] = map1[o0];
            out[op++] = map1[o1];
            if (op < oDataLen) {
                out[op] = (byte) map1[o2];
                op++;
            }
            if (op < oDataLen) {
                out[op] = (byte) map1[o3];
                op++;
            }
        }
    }
    return out;
}

Thanks for the help

Was it helpful?

Solution

As Zapl stated above, the problem is that you were allocating extra bytes for your out array by using new byte[oLen] instead of new byte[oDataLen].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top