解码java服务器上的Base64编码数据

时间:2017-07-04 14:28:31

标签: java android base64

方法:我正在将简单数据编码到base64,然后将其发送到服务器。同样,我试图解码Java服务器上的base64编码数据,但总有一个例外是无法正确解码它。任何帮助表示赞赏。

客户端代码段(Android应用程序):

private String random(int size) {

  Random generator = new Random();
  StringBuilder randomStringBuilder = new StringBuilder();
  //int randomLength = generator.nextInt(size);
  char tempChar;
  for (int i = 0; i < size; i++){
      tempChar = (char) (generator.nextInt(90 - 65) + 65);
      randomStringBuilder.append(tempChar);
  }
  return randomStringBuilder.toString();
}


String data = random(packetSize);
Log.d(data, " Random String");

byte [] encodedBuffer = Base64.encode(data.getBytes("UTF-8"), 0);
DatagramPacket packet =
        new DatagramPacket(encodedBuffer, encodedBuffer.length, address, dstPort);
Log.d(String.valueOf(packetSize), "Sending packet Size");

SocketHandler.getSocket().send(packet);

服务器代码段:

private String decodeBase64(String encoded) {
  try
  {
    byte[] decodedBytes = Base64.getMimeDecoder().decode(encoded.getBytes("UTF-8"));
    String decodeData = new String(decodedBytes);
    return decodeData;
  }
  catch (Exception e) {
    System.out.println("Unable to decode");
    return null;
  }
}

public void run() {
  try {
    byte[] buf = new byte[1400];
    DatagramPacket packet = packet = new DatagramPacket(buf, buf.length);
    System.out.println("UDP Server waiting");
    while(true)
    {

      try {
        SocketHandler.getSocket().receive(packet);
        System.out.println("Received: " + decodeBase64(new String(packet.getData())));

      }
      catch (SocketException e1) {
        System.out.println("Socket closed " + e1);
      }
    }
  } 
  catch (Exception e) {
    e.printStackTrace();
  }
}

例外:

java.lang.IllegalArgumentException: Input byte array has incorrect ending byte at 147
    at java.util.Base64$Decoder.decode0(Base64.java:742)
    at java.util.Base64$Decoder.decode(Base64.java:526)

示例输入(生成随机字符串):

LVYAGHNVAUUEVNSEQHLHNNHGOHKFJQICSGXMXLJGBICOGJTWGBFLRSROJQWIWCETRXMTDWASGILXCDPRBIYAWFTBRRWN

2 个答案:

答案 0 :(得分:1)

尝试使用类javax.xml.bind.DatatypeConverter进行解码而不是Base64。对我来说它有帮助。 例如,替换:

byte[] decodedBytes = Base64.getMimeDecoder().decode(encoded.getBytes("UTF-8"));

byte[] decodedBytes = DatatypeConverter.parseBase64Binary(base64str)

答案 1 :(得分:0)

可以试试这个

import java.util.Base64;

public static void main(String[] args)
{
    String input = new String("FMKPARSQQTIENUDYWMRWXKGEEAOHFJBEXMKGWCUFBPICEGYNXEQWQDDOXWOR‌LVGNHNLALVUEWVXLHUKA‌MRHSPONMAMRB");
    System.out.println("input " + input);
    byte[] encodedBytes = Base64.getEncoder().encode(input.getBytes());
    System.out.println("encodedBytes " + new String(encodedBytes));
    byte[] decodedBytes = Base64.getDecoder().decode(encodedBytes);
    System.out.println("output " + new String(decodedBytes));

 }

在线编译并执行

输入FMKPARSQQTIENUDYWMRWXKGEEAOHFJBEXMKGWCUFBPICEGYNXEQWQDDOXWORâLVGNHNLALVUEWVXLHUKAâMRHSPONMAMRB

encodedBytes Rk1LUEFSU1FRVElFTlVEWVdNUldYS0dFRUFPSEZKQkVYTUtHV0NVRKJQSUNFR1lOWEVRV1FERE9YV09Sw6LCgMKMTFZHTkhOTEFMVVV1VYTEhVS0HDosKAwoxNUkhTUE9OTUFNUkI =

输出 FMKPARSQQTIENUDYWMRWXKGEEAOHFJBEXMKGWCUFBPICEGYNXEQWQDDOXWORâLVGNHNLALVUEWVXLHUKAâMRHSPONMAMRB