简体   繁体   English

解码和编码问题。 android.util。*和java.util。*中Base64 Class的不同实现?

[英]Decoding and Encoding issue. Different implementations of Base64 Class in android.util.* and java.util.*?

I am writing an App which does the following with a given String str: 我正在编写一个App,它使用给定的String str执行以下操作:

encode(encrypt(encode(stringToBytearray(str))));

The Server receives the Base64 encoded String, which is then decoded -> decrypted -> decoded, to get the sent String str from the App. 服务器接收Base64编码的字符串,然后解码 - >解密 - >解码,从App获取发送的字符串str。

Unfortunately it doesnt work for all Strings, long Strings lead to a long Base64 String and my Server throws the following Exception: 不幸的是,它不适用于所有字符串,长字符串导致长Base64字符串,我的服务器抛出以下异常:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal base64 character 5b
 at java.util.Base64$Decoder.decode0(Base64.java:714)
 at java.util.Base64$Decoder.decode(Base64.java:526)
 at Main.decode(Main.java:113)
 at Main.main(ain.java:33)

The String has the format "[string, string, ..., string]" without "s. String的格式为“[string,string,...,string]”,不带“s”。

Like I mentioned above, for Strings which are not too long (sorry I cant quantify the length yet), this works. 就像我上面提到的,对于不太长的字符串(抱歉我无法量化长度),这是有效的。 So I think I implemented it right. 所以我认为我是正确的。

Whats weird is, that if I dont send it, but decode(decrypt(decode(stringToBytearray(str)))); 奇怪的是,如果我不发送它,但解码(decrypt(decode(stringToBytearray(str)))); the String on the Device itself, it all works perfectly. 设备本身的字符串,一切都很完美。

My Setup: JDK 7, eclipse (adt-bundle, android development) (Windows 7) 我的设置:JDK 7,eclipse(adt-bundle,android开发)(Windows 7)

JDK 8, eclipse ('normal' java) (Linux (xubuntu)) JDK 8,eclipse('normal'java)(Linux(xubuntu))

Is it because both Classes (Base64) are implemented diffrently? 是因为两个类(Base64)都是差异实现的吗? If so, how can I make it work? 如果是这样,我怎样才能使它工作?

Here are the Encoding/Decoding Methods: 以下是编码/解码方法:

Encoding (Device: on Windows 7, adt-bundle, Android-Dev, jdk 7): 编码(设备:在Windows 7上,adt-bundle,Android-Dev,jdk 7):

import android.util.Base64

public byte[] encode(byte[] bytearrayToEncode){
   return Base64.encode(bytearrayToEncode, Base64.NO_WRAP|Base64.URL_SAFE);
}

Decoding (Server: on Linux, jdk 8): 解码(服务器:Linux,jdk 8):

import java.util.Base64

public byte[] decode(byte[] bytearrayToEncode){
   return Base64.getUrlDecoder().decode(bytearrayToDecode);
}

Strings are all De-/Encoded with the same charset (utf-8)! 字符串都使用相同的字符集(utf-8)进行解码/编码! Encoding/Decoding: Base64 编码/解码:Base64

Crypto: AES 加密:AES

If you need more Information, just ask, but I think I provided all neccessary informations. 如果您需要更多信息,请询问,但我认为我提供了所有必要的信息。

Edit: 编辑:

public String bytearrayToString(byte[] bytearray){
    String str = null;
    try {
        str = new String(bytearray, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return str;
}

public byte[] stringToBytearray(String str){
    byte[] bytearray = null;
    try {
        bytearray = str.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return bytearray;
}

You must be using JSONObjects to send the Base64 String to the server. 您必须使用JSONObjects将Base64字符串发送到服务器。 If so then the JSONObject will add " \\ " escape characters to the string which are illegal Base64 characters. 如果是这样,那么JSONObject会将“\\”转义字符添加到字符串中,这些字符是非法的Base64字符。 All you need to do is 你需要做的就是

String rectifiedString = Base64String.replace("\\","");

on the server end before using the acquired string. 在使用获取的字符串之前在服务器端。

Optional 可选的

Also on the server side use 另外在服务器端使用

import javax.xml.bind.DatatypeConverter;

and

String result = DatatypeConverter.printBase64Binary(bytearrayToDecode);

byte[] result = DatatypeConverter.parseBase64Binary(str);

instead. 代替。

It will not only get the job done but is much faster at encoding/decoding ( as benchmarked here ). 它不仅可以完成工作,而且可以更快地进行编码/解码( 在此处进行基准测试 )。

If you have a space in your encoded string, which is an illegal character in base64. 如果编码字符串中有空格,则在base64中是非法字符。 If you remove it, it still generates the same image/data in the converter you linked to, and can be now decoded by your code as well. 如果删除它,它仍会在链接到的转换器中生成相同的图像/数据,现在也可以通过代码进行解码。

UPDATE: Some decoders (like the one you linked, or Base64.getMimeDecoder() in Java) ignore illegal characters, others (like Base64.getDecoder()) don't allow them. 更新:一些解码器(如您链接的解码器,或Java中的Base64.getMimeDecoder())忽略非法字符,其他解码器(如Base64.getDecoder())不允许它们。

Use for encode: 用于编码:

Base64.getEncoder().encodeToString(yourString.getBytes("UTF-8"));

and decode: 并解码:

byte[] decodedBytes = Base64.getDecoder().decode(yourString);
String stringDecode = new String(decodedBytes, "UTF-8");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM