Reputation: 19
The following program output differs on Windows vs. Unix systems. Could you please explain to me why this is happening, and how this behavior can be made consistent across Unix and Windows.
Because i am using Unix for the encoding of a file and it would not be possible to decode it on Windows systems.
Any help in this regard would be appreciated. Thanks in advance.
import sun.misc.BASE64Encoder;
import sun.misc.CharacterEncoder;
public class TestEncode {
public static void main(String[] args) {
byte signature[] = "[35, 44, -31, 18, 78, 84, -113, 1, 27, 36, -79, -60, 75, -14, -80, -99, 65, 11, -45, -54, 23, -100, 74, -54, -26, -77, 33, -40, 104, 90, -33, 32, -123, -76, -27, -118, -25, -97, -85, 22, -64, 102, -7, 119, -65, 35, -114, 31, -83, 73, -57, 63, -7, 47, -31, 48, 28, -109, 54, -90, -24, -21, -102, 59, 82, -14, -52, -77, -22, -25, -15, -81, 70, 52, -42, 93, 76, -51, 96, 87, 29, -37, -40, -71, -121, 44, -44, 74, 23, -76, 29, 108, -56, 48, 46, -26, -73, -53, 90, 53, 25, -96, 115, -79, 93, -128, -46, -119, -30, 22, -107, -27, 6, -120, 2, 19, -72, -5, 30, -54, -34, 26, -22, -44, 93, 40, 84, -125]".getBytes();
byte encodedSignature[] = null;
CharacterEncoder encoder;
encoder = new BASE64Encoder();
encodedSignature = encoder.encode(signature).getBytes();
System.out.println(encodedSignature.length);
}
}
Upvotes: 1
Views: 5423
Reputation: 21902
You might be using a different charset on each machine. Try this to find out:
System.out.println("Default Charset=" + Charset.defaultCharset());
I suspect your problem when calling the getBytes()
method. By default it uses the platform's default charset. If you want to guarantee that it's using the same one, specify it in the getBytes()
method by calling getBytes("UTF-8");
Upvotes: 3
Reputation: 36423
Can you give this a try and what are your results for windows vs linux:
import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import java.io.IOException;
public class Base64Test {
public static void main(String args[]) throws IOException, Base64DecodingException {
String orig = "original String before base64 encoding in Java";
//encoding byte array into base 64
String encoded = Base64.encode(orig.getBytes("UTF-8"));//make sure the bytes are utf-8 and not platform default
System.out.println("Original String: " + orig );
System.out.println("Base64 Encoded String : " + new String(encoded,"UTF-8"));//ensure string is utf-8
//decoding byte array into base64
byte[] decoded = Base64.decode(encoded);
System.out.println("Base 64 Decoded String : " + new String(decoded,"UTF-8"));//ensure string is utf-8
}
}
Mine is (Windows only):
Original String: original String before base64 encoding in Java
Base64 Encoded String : b3JpZ2luYWwgU3RyaW5nIGJlZm9yZSBiYXNlNjQgZW5jb2RpbmcgaW4gSmF2YQ==
Base64 Decoded String : original String before base64 encoding in Java
The problems that you might be facing are:
getBytes()
is involved maybe set it to your own default like getBytes("UTF-8")
(in my example i tried to do this)But I cant be certain
Upvotes: 1