Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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
Java中的数据包编码和C中的数据包解码失败_Java_C_Encoding_Decoding_Android Vpn Service - Fatal编程技术网

Java中的数据包编码和C中的数据包解码失败

Java中的数据包编码和C中的数据包解码失败,java,c,encoding,decoding,android-vpn-service,Java,C,Encoding,Decoding,Android Vpn Service,嗨,我有两个函数在Base64中对数据包进行编码。 但客户端无法与服务器连接。 可能是编码和解码有问题。 这两个程序可能无法同时工作。 这是用于从Android Java代码中发送Base64编码后的TCP数据包,并用C进行接收和解码 我们正在使用ToyVPN Android客户端和ToyVPN服务器。 链接在这里-和 下面是用于编码的Java代码- public final static byte[] ALPHABET = {(byte) 'A', (byte) 'B

嗨,我有两个函数在Base64中对数据包进行编码。 但客户端无法与服务器连接。 可能是编码和解码有问题。 这两个程序可能无法同时工作。 这是用于从Android Java代码中发送Base64编码后的TCP数据包,并用C进行接收和解码

我们正在使用ToyVPN Android客户端和ToyVPN服务器。 链接在这里-和 下面是用于编码的Java代码-

 public final static byte[] ALPHABET =
            {(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) '/'};

    /**
     * The 64 valid web safe Base64 values.
     */
public static byte[] encode(byte[] source, int off, int len, byte[] alphabet,
                                int maxLineLength) {
        int lenDiv3 = (len + 2) / 3; // ceil(len / 3)
        int len43 = lenDiv3 * 4;
        byte[] outBuff = new byte[len43 // Main 4:3
                + (len43 / maxLineLength)]; // New lines

        int d = 0;
        int e = 0;
        int len2 = len - 2;
        int lineLength = 0;
        for (; d < len2; d += 3, e += 4) {

            // The following block of code is the same as
            // encode3to4( source, d + off, 3, outBuff, e, alphabet );
            // but inlined for faster encoding (~20% improvement)
            int inBuff =
                    ((source[d + off] << 24) >>> 8)
                            | ((source[d + 1 + off] << 24) >>> 16)
                            | ((source[d + 2 + off] << 24) >>> 24);
            outBuff[e] = alphabet[(inBuff >>> 18)];
            outBuff[e + 1] = alphabet[(inBuff >>> 12) & 0x3f];
            outBuff[e + 2] = alphabet[(inBuff >>> 6) & 0x3f];
            outBuff[e + 3] = alphabet[(inBuff) & 0x3f];

            lineLength += 4;
            if (lineLength == maxLineLength) {
                outBuff[e + 4] = NEW_LINE;
                e++;
                lineLength = 0;
            } // end if: end of line
        } // end for: each piece of array

        if (d < len) {
            encode3to4(source, d + off, len - d, outBuff, e, alphabet);

            lineLength += 4;
            if (lineLength == maxLineLength) {
                // Add a last newline
                outBuff[e + 4] = NEW_LINE;
                e++;
            }
            e += 4;
        }

        assert (e == outBuff.length);
        return outBuff;
    }
公共最终静态字节[]字母表=
{(字节)'A',(字节)'B',(字节)'C',(字节)'D',(字节)'E',(字节)'F',
(字节)G,(字节)H,(字节)I,(字节)J,(字节)K,,
(字节)'L',(字节)'M',(字节)'N',(字节)'O',(字节)'P',
(字节)'Q',(字节)'R',(字节)'S',(字节)'T',(字节)'U',
(字节)V,(字节)W,(字节)X,(字节)Y,(字节)Z,,
(字节)'a',(字节)'b',(字节)'c',(字节)'d',(字节)'e',
(字节)f,(字节)g,(字节)h,(字节)i,(字节)j,,
(字节)‘k’,(字节)‘l’,(字节)‘m’,(字节)‘n’,(字节)‘o’,
(字节)'p',(字节)'q',(字节)'r',(字节)'s',(字节)'t',
(字节)'u',(字节)'v',(字节)'w',(字节)'x',(字节)'y',
(字节)z,(字节)0,(字节)1,(字节)2,(字节)3,,
(字节)‘4’、(字节)‘5’、(字节)‘6’、(字节)‘7’、(字节)‘8’,
(字节)'9',(字节)'+',(字节)'/'};
/**
*64个有效的web安全Base64值。
*/
公共静态字节[]编码(字节[]源,int off,int len,字节[]字母,
int(最大线长度){
int lenDiv3=(len+2)/3;//ceil(len/3)
int len43=lenDiv3*4;
byte[]exputff=新字节[len43//Main 4:3
+(len43/maxLineLength);//新行
int d=0;
int e=0;
int len2=len-2;
int lineLength=0;
对于(;d>8)
|((源[d+1+off]>>16)
|((源[d+2+off]>>24);
突发事件[e]=字母表[(inBuff>>>18)];
突发[e+1]=字母表[(inBuff>>>12)和0x3f];
突发事件[e+2]=字母表[(inBuff>>>6)和0x3f];
突发事件[e+3]=字母表[(inBuff)&0x3f];
线宽+=4;
如果(lineLength==maxLineLength){
突发事件[e+4]=新的_线;
e++;
线宽=0;
}//如果结束:行结束
}//结束:每个数组块
if(d
这是用于解码的C代码,它使用与Java编码字母相同的解码字母表

//Base64 char table - used internally for encoding
unsigned char b64_chr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

unsigned int b64_int(unsigned int ch) {

    // ASCII to base64_int
    // 65-90  Upper Case  >>  0-25
    // 97-122 Lower Case  >>  26-51
    // 48-57  Numbers     >>  52-61
    // 43     Plus (+)    >>  62
    // 47     Slash (/)   >>  63
    // 61     Equal (=)   >>  64~
    if (ch==43)
    return 62;
    if (ch==47)
    return 63;
    if (ch==61)
    return 64;
    if ((ch>47) && (ch<58))
    return ch + 4;
    if ((ch>64) && (ch<91))
    return ch - 'A';
    if ((ch>96) && (ch<123))
    return (ch - 'a') + 26;
    return 0;
}


unsigned int b64_decode(unsigned char* in, unsigned int in_len, unsigned char* out) {

    unsigned int i=0, j=0, k=0, s[4];

    for (i=0;i<in_len;i++) {
        s[j++]=b64_int(*(in+i));
        if (j==4) {
            out[k+0] = ((s[0]&255)<<2)+((s[1]&0x30)>>4);
            if (s[2]!=64) {
                out[k+1] = ((s[1]&0x0F)<<4)+((s[2]&0x3C)>>2);
                if ((s[3]!=64)) {
                    out[k+2] = ((s[2]&0x03)<<6)+(s[3]); k+=3;
                } else {
                    k+=2;
                }
            } else {
                k+=1;
            }
            j=0;
        }
    }

    return k;
}
//Base64字符表-内部用于编码
无符号字符b64_chr[]=“abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz012456789+/”;
无符号整数b64_int(无符号整数ch){
//ASCII到base64_int
//65-90大写>>0-25
//97-122小写>>26-51
//48-57个数字>>52-61
//43加(+)>>62
//47斜杠(/)>>63
//61等于(=)>>64~
如果(ch==43)
返回62;
如果(ch==47)
返回63;
如果(ch==61)
返回64;

如果((ch>47)&&&(ch64)&&&(ch96)&&&(ch96),您是否尝试过在java部分对base64消息进行编码并尝试使用c代码对其进行解码?是的,我已经尝试过了。