解码和编码问题.android.util.*和java.util.*中Base64类的不同实现? [英] Decoding and Encoding issue. Different implementations of Base64 Class in android.util.* and java.util.*?
问题描述
我正在编写一个应用程序,该应用程序使用给定的String str进行以下操作:
I am writing an App which does the following with a given String str:
encode(encrypt(encode(stringToBytearray(str))));
服务器接收Base64编码的字符串,然后将其解码->解密->解码,以从应用程序获取发送的String str.
The Server receives the Base64 encoded String, which is then decoded -> decrypted -> decoded, to get the sent String str from the App.
不幸的是,它不适用于所有字符串,长字符串导致长Base64字符串,并且我的服务器抛出以下异常:
Unfortunately it doesnt work for all Strings, long Strings lead to a long Base64 String and my Server throws the following Exception:
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)
字符串的格式为[[string,string,...,string]",不带"s".
The String has the format "[string, string, ..., string]" without "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.
奇怪的是,如果我不发送它,但可以解码(decrypt(decode(decode(stringToBytearray(str)))));设备本身上的字符串,就可以完美运行.
Whats weird is, that if I dont send it, but decode(decrypt(decode(stringToBytearray(str)))); the String on the Device itself, it all works perfectly.
我的设置:JDK 7,eclipse(adt-bundle,android开发)(Windows 7)
My Setup: JDK 7, eclipse (adt-bundle, android development) (Windows 7)
JDK 8,eclipse('normal'java)(Linux(xubuntu))
JDK 8, eclipse ('normal' java) (Linux (xubuntu))
是因为两个类(Base64)的实现方式有所不同吗?如果是这样,我如何使它工作?
Is it because both Classes (Base64) are implemented diffrently? If so, how can I make it work?
以下是编码/解码方法:
Here are the Encoding/Decoding Methods:
编码(设备:在Windows 7,adt-bundle,Android-Dev,jdk 7上):
Encoding (Device: on 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);
}
解码(服务器:在Linux上,jdk 8):
Decoding (Server: on Linux, jdk 8):
import java.util.Base64
public byte[] decode(byte[] bytearrayToEncode){
return Base64.getUrlDecoder().decode(bytearrayToDecode);
}
所有字符串都使用相同的字符集(utf-8)进行解码/编码!编码/解码:Base64
Strings are all De-/Encoded with the same charset (utf-8)! Encoding/Decoding: Base64
加密:AES
如果您需要更多信息,请问一下,但是我想我提供了所有必要的信息.
If you need more Information, just ask, but I think I provided all neccessary informations.
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;
}
推荐答案
您必须使用JSONObjects将Base64字符串发送到服务器.如果是这样,则JSONObject将在字符串中添加"\"转义字符,这些字符是非法的Base64字符.您所需要做的就是
You must be using JSONObjects to send the Base64 String to the server. If so then the JSONObject will add " \ " escape characters to the string which are illegal Base64 characters. All you need to do is
String rectifiedString = Base64String.replace("\\","");
在服务器端使用获取的字符串之前.
on the server end before using the acquired string.
可选
也在服务器端使用
import javax.xml.bind.DatatypeConverter;
和
字符串结果= DatatypeConverter.printBase64Binary(bytearrayToDecode);
byte[] result = DatatypeConverter.parseBase64Binary(str);
相反.
It will not only get the job done but is much faster at encoding/decoding (as benchmarked here).
这篇关于解码和编码问题.android.util.*和java.util.*中Base64类的不同实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!