getBytes() - Encoding Style problem

Hi people,

I need to encrypt a password using SHA algorithm through the MessageDigest object. Initially, I need obtain the bytes of the password String in order to pass it to the update() method of the mentioned object.

The problem is that the getBytes() method return different values on different platforms. I am using Windows and Sun Solaris OSs. I also noticed that even on the same OS, changing the jdk version will return in different result for the same String.

Could you please help me with this issue because it is getting very frustrating.

Regards George

[599 byte] By [George_Azzopardia] at [2007-11-27 8:01:29]
# 1
Use getBytes(String charsetName) String method.
czakoa at 2007-7-12 19:43:35 > top of Java-index,Security,Cryptography...
# 2
Hi,Yes I used getBytes(String charset) already, but still with the same problem.any other ideas?Regards
George_Azzopardia at 2007-7-12 19:43:35 > top of Java-index,Security,Cryptography...
# 3
> Yes I used getBytes(String charset) already, but> still with the same problem.Then you are not doing it properly because if you use "utf-8" as the charset then it will be the same on ALL platforms.
sabre150a at 2007-7-12 19:43:35 > top of Java-index,Security,Cryptography...
# 4
Hi,Could you kindly provide me with a simple example?I am getting confused. Thanks & RegardsGeorge Azzopardi
George_Azzopardia at 2007-7-12 19:43:35 > top of Java-index,Security,Cryptography...
# 5
> Could you kindly provide me with a simple example?I don't understand you problem! What is there about providing the charset encoding parameter value to getBytes() that you don't understand?
sabre150a at 2007-7-12 19:43:35 > top of Java-index,Security,Cryptography...
# 6
I already used getBytes("UTF-8"), however on UNIX machine i am getting a different value than on a Windows machine.Could it be different jvm versions on different machines?RegardsGeorge
George_Azzopardia at 2007-7-12 19:43:35 > top of Java-index,Security,Cryptography...
# 7

> I already used getBytes("UTF-8"), however on UNIX

> machine i am getting a different value than on a

> Windows machine.

I doubt it!

>

> Could it be different jvm versions on different

> machines?

No! You have a coding error. You need to post all your code.

sabre150a at 2007-7-12 19:43:35 > top of Java-index,Security,Cryptography...
# 8

The code is very simple.

System.out.println("TEST".getBytes("UTF-8"));

I am using jvm 1.5.0_04 both on Windows and Unix. On Windows, the above statement is returning [B@6b97fd while on Unix it is returning [B@e48e1b.

I don't know what's wrong with this. Maybe I have a misconception of some principles.

Thanks for your support

Regards

George

George_Azzopardia at 2007-7-12 19:43:35 > top of Java-index,Security,Cryptography...
# 9
That statement doesn't print the contents of the array, it prints whatever byte[].toString() prints. Try looking inside, at the elements of the array.
ejpa at 2007-7-12 19:43:35 > top of Java-index,Security,Cryptography...
# 10
OOOOps. OK thank you very much. Problem solvedRegardsGeorge
George_Azzopardia at 2007-7-12 19:43:35 > top of Java-index,Security,Cryptography...