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

huangapple 未分类评论59阅读模式
英文:

Packet Encoding in Java and Packet Decoding in C Failed

问题

这是要翻译的内容:

这是关于对数据包进行Base64编码的2个函数。
但是客户端无法与服务器建立连接。
可能是编码和解码出现了问题。
这两个程序可能无法正常配合工作。
这是将经过Base64编码的TCP数据包从Android Java代码发送并在C代码中接收和解码的过程。

我们正在使用ToyVPN Android客户端和ToyVPNServer。
链接在这里- https://github.com/LipiLee/ToyVpnhttps://github.com/LipiLee/ToyVpnServer

这是Java代码中的编码部分:

  1. public final static byte[] ALPHABET =
  2. {(byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F',
  3. (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K',
  4. // ... (省略部分内容)
  5. (byte) '8', (byte) '9', (byte) '+', (byte) '/'};
  6. public static byte[] encode(byte[] source, int off, int len, byte[] alphabet,
  7. int maxLineLength) {
  8. // ... (省略部分内容)
  9. return outBuff;
  10. }

这是C代码中的解码部分,使用与Java编码部分相同的字母表:

  1. unsigned char b64_chr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  2. unsigned int b64_int(unsigned int ch) {
  3. // ... (省略部分内容)
  4. return 0;
  5. }
  6. unsigned int b64_decode(unsigned char* in, unsigned int in_len, unsigned char* out) {
  7. // ... (省略部分内容)
  8. return k;
  9. }
英文:

Hi I have 2 functions to encode a packet in Base64.
But the client can not connect with the server.
May be the encoding and decoding is in problem.
These two programs might not working combinedly.
This is for sending a TCP Packet after Base64 Encoding from Android Java Code and receiving and decoded in C.

We are using ToyVPN Android Client and ToyVPNServer.
The links are here- https://github.com/LipiLee/ToyVpn and https://github.com/LipiLee/ToyVpnServer
Here is Java code for Encoding-

  1. public final static byte[] ALPHABET =
  2. {(byte) 'A', (byte) 'B', (byte) 'C', (byte) 'D', (byte) 'E', (byte) 'F',
  3. (byte) 'G', (byte) 'H', (byte) 'I', (byte) 'J', (byte) 'K',
  4. (byte) 'L', (byte) 'M', (byte) 'N', (byte) 'O', (byte) 'P',
  5. (byte) 'Q', (byte) 'R', (byte) 'S', (byte) 'T', (byte) 'U',
  6. (byte) 'V', (byte) 'W', (byte) 'X', (byte) 'Y', (byte) 'Z',
  7. (byte) 'a', (byte) 'b', (byte) 'c', (byte) 'd', (byte) 'e',
  8. (byte) 'f', (byte) 'g', (byte) 'h', (byte) 'i', (byte) 'j',
  9. (byte) 'k', (byte) 'l', (byte) 'm', (byte) 'n', (byte) 'o',
  10. (byte) 'p', (byte) 'q', (byte) 'r', (byte) 's', (byte) 't',
  11. (byte) 'u', (byte) 'v', (byte) 'w', (byte) 'x', (byte) 'y',
  12. (byte) 'z', (byte) '0', (byte) '1', (byte) '2', (byte) '3',
  13. (byte) '4', (byte) '5', (byte) '6', (byte) '7', (byte) '8',
  14. (byte) '9', (byte) '+', (byte) '/'};
  15. /**
  16. * The 64 valid web safe Base64 values.
  17. */
  18. public static byte[] encode(byte[] source, int off, int len, byte[] alphabet,
  19. int maxLineLength) {
  20. int lenDiv3 = (len + 2) / 3; // ceil(len / 3)
  21. int len43 = lenDiv3 * 4;
  22. byte[] outBuff = new byte[len43 // Main 4:3
  23. + (len43 / maxLineLength)]; // New lines
  24. int d = 0;
  25. int e = 0;
  26. int len2 = len - 2;
  27. int lineLength = 0;
  28. for (; d < len2; d += 3, e += 4) {
  29. // The following block of code is the same as
  30. // encode3to4( source, d + off, 3, outBuff, e, alphabet );
  31. // but inlined for faster encoding (~20% improvement)
  32. int inBuff =
  33. ((source[d + off] << 24) >>> 8)
  34. | ((source[d + 1 + off] << 24) >>> 16)
  35. | ((source[d + 2 + off] << 24) >>> 24);
  36. outBuff[e] = alphabet[(inBuff >>> 18)];
  37. outBuff[e + 1] = alphabet[(inBuff >>> 12) & 0x3f];
  38. outBuff[e + 2] = alphabet[(inBuff >>> 6) & 0x3f];
  39. outBuff[e + 3] = alphabet[(inBuff) & 0x3f];
  40. lineLength += 4;
  41. if (lineLength == maxLineLength) {
  42. outBuff[e + 4] = NEW_LINE;
  43. e++;
  44. lineLength = 0;
  45. } // end if: end of line
  46. } // end for: each piece of array
  47. if (d < len) {
  48. encode3to4(source, d + off, len - d, outBuff, e, alphabet);
  49. lineLength += 4;
  50. if (lineLength == maxLineLength) {
  51. // Add a last newline
  52. outBuff[e + 4] = NEW_LINE;
  53. e++;
  54. }
  55. e += 4;
  56. }
  57. assert (e == outBuff.length);
  58. return outBuff;
  59. }

Here is C Code for Decoding, this is using the same Decoding Alphabet like Java Encoding Alphabet.

  1. //Base64 char table - used internally for encoding
  2. unsigned char b64_chr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  3. unsigned int b64_int(unsigned int ch) {
  4. // ASCII to base64_int
  5. // 65-90 Upper Case >> 0-25
  6. // 97-122 Lower Case >> 26-51
  7. // 48-57 Numbers >> 52-61
  8. // 43 Plus (+) >> 62
  9. // 47 Slash (/) >> 63
  10. // 61 Equal (=) >> 64~
  11. if (ch==43)
  12. return 62;
  13. if (ch==47)
  14. return 63;
  15. if (ch==61)
  16. return 64;
  17. if ((ch>47) && (ch<58))
  18. return ch + 4;
  19. if ((ch>64) && (ch<91))
  20. return ch - 'A';
  21. if ((ch>96) && (ch<123))
  22. return (ch - 'a') + 26;
  23. return 0;
  24. }
  25. unsigned int b64_decode(unsigned char* in, unsigned int in_len, unsigned char* out) {
  26. unsigned int i=0, j=0, k=0, s[4];
  27. for (i=0;i<in_len;i++) {
  28. s[j++]=b64_int(*(in+i));
  29. if (j==4) {
  30. out[k+0] = ((s[0]&255)<<2)+((s[1]&0x30)>>4);
  31. if (s[2]!=64) {
  32. out[k+1] = ((s[1]&0x0F)<<4)+((s[2]&0x3C)>>2);
  33. if ((s[3]!=64)) {
  34. out[k+2] = ((s[2]&0x03)<<6)+(s[3]); k+=3;
  35. } else {
  36. k+=2;
  37. }
  38. } else {
  39. k+=1;
  40. }
  41. j=0;
  42. }
  43. }
  44. return k;
  45. }

huangapple
  • 本文由 发表于 2020年3月15日 22:32:29
  • 转载请务必保留本文链接:https://java.coder-hub.com/60693972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定