Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
base64 URL在java/javacard中无填充_Java_Base64_Urlencode_Javacard - Fatal编程技术网

base64 URL在java/javacard中无填充

base64 URL在java/javacard中无填充,java,base64,urlencode,javacard,Java,Base64,Urlencode,Javacard,我希望在javacard中编写一个encode函数(甚至java也可以),该函数将基于64 URL编码(其中标准Base64的“+”和“/”字符分别替换为“-”和“_”)给定数据,没有“=”填充。这是我到目前为止的代码,但我怀疑有什么地方出错了,因为它没有去掉填充,而是留下了一个空字符。然而,当我使用解码器时,它仍然可以正常工作 // Mapping table from 6-bit nibbles to Base64 characters. private static final byte[

我希望在javacard中编写一个encode函数(甚至java也可以),该函数将基于64 URL编码(其中标准Base64的“+”和“/”字符分别替换为“-”和“_”)给定数据,没有“=”填充。这是我到目前为止的代码,但我怀疑有什么地方出错了,因为它没有去掉填充,而是留下了一个空字符。然而,当我使用解码器时,它仍然可以正常工作

// 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上面所述,问题是您使用了
新字节[oLen]
而不是
新字节[oDataLen]
为out数组分配了额外的字节

太多太复杂了,看不出有什么不对。试着用调试器一步一步地检查你的代码。事实上,我并没有编写代码,而是从中获得的。我对它做了一些修改,去掉了填充,用“-”和“-”替换“+”和“/”。所以,由于我对Base64编码知之甚少,我想有人可以帮我。啊,你必须改变
byte[]out=new byte[oLen]
使用
oDataLen
。即使没有写入最后的字节,它们仍然存在。我想当你做
oLen=(type==1)的时候,你可以跳过你最后做的任何事情吗?oDataLen:(短)((iLen+2)/3)*4)