0
我期待在javacard中编写一个编码函数(甚至java会很好),它将基于64位URL编码(其中标准Base64的'+'和'/'字符是分别用' - '和'_')替换给定的数据,不加“=”填充。这是我迄今为止的代码,但我怀疑有些东西是错误的,因为不是取出填充而是留下空白字符。然而,当我使用的解码器,它仍然能正常...base64 URL在java/javacard中没有填充
// 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;
}
感谢您的帮助
太多太复杂了,看不出有什么问题。尝试使用调试器逐步执行代码。 – zapl
我其实没有写代码,我从[这里](http://www.source-code.biz/base64coder/java/Base64Coder.java.txt)得到它。我对它进行了一些修改以取出填充,并用' - '和'_'替换'+'和'/'。所以,由于我对base64encoding知之甚少,我以为有人可以帮助我。 – ItsANabhility
啊,你必须改变'byte [] out = new byte [oLen];'使用'oDataLen'。即使你不写最后的字节,他们仍然存在。我猜你可以简单地跳过你最后做的任何事情,当你做'oLen =(type == 1)? oDataLen:(简写)((((iLen + 2)/ 3)* 4);'在开头。 – zapl