Same Java code returns different result running from Netbeans and JAR file

When i try to run java code using Netbeans, everything is good and RC2 encryption code gives 5B\q\OI5c¸ä3î1Ü. But running compiled jar file from windows the output is different 5B?\q\O?I5cä3???1Ü?. Problems solves if i run the same jar file from linux.

Jon Skeet
people
quotationmark

It's almost certainly just the way the console handles non-ASCII characters in each case. However, that's a corollary to you doing something you shouldn't to start with: converting an arbitrary byte[] to String when you shouldn't do so, or at least in a way that you shouldn't, e.g with new String(byte[]). Even specifying a Charset (which you should always do when converting between text and a binary representation of that text, in either direction) wouldn't help here.

The result of encryption is not encoded text - it's just bytes. Ideally you should keep that as a byte[] - it's just binary data, after all. If you do need to convert it into text though, you should use hex or base64 to do so, as that way you'll get an ASCII representation which is always reversible to get back to your original binary data.

For base64, there's a good public domain library you might want to consider using.

people

See more on this question at Stackoverflow