File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help please, what is wrong here. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help please, what is wrong here. " Watch "Help please, what is wrong here. " New topic
Author

Help please, what is wrong here.

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Hi All,

I wonder if anyone here can help me. I have an algorithm to decrypt a password from a string and i am struggling to decrypt it. The algorithm itself shows an example and i tried the example but i cant get the results that were shown in the example to match with the result in my sample code.

Here is the algorithm.

1.First get the bytes of the original password, assuming a "Latin-1" encoding. For the password "Baltimore1," these bytes are: 66 97 108 116 105 109 111 114 101 49 44 (i.e. the value of "B" is 66, "a" is 97, etc).
2.Then get the MD5 hash of these bytes. MD5 is a standard, public algorithm. Once again, for the password "Baltimore1," these bytes work out as: 223 238 161 24 62 121 39 143 115 167 51 163 245 231 226 94
3.Finally, create the new password by forming a string whose Latin-1 encoding is the bytes from the previous step. For the "Baltimore1," password, if this is string is written to the screen, it looks like: ���_>y'?s�3����^(i.e. the 62 above is a ">", the 121 is a "y", etc).

To try the above, i tried to get the string shown in step 3 using the "Baltimore1," password. Here is the code that i wrote.



And here is the output of the above program.



As you can see, the output of line 1 is correct. The bytes are exactly as described in the example. I was expecting the output on line 4 to be 223 238 161 24 62 121 39 143 115 167 51 163 245 231 226 94 as shown in the example. Does anyone know what i am doing wrong?
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3737
    
  16
Arrays don't override the toString() method, so the value you are returning from the MD5 method is actually the value of the array reference. To convert the values in an array to a String, Try the Arrays.toString(byte[]) method.


Joanne
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

I've updated the MD5() method to include the Arrays.toString(md5hash) line.



The output is now as shown below


Does this mean that the string that is being hashed is also not correct?

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39788
    
  28
Don't know, but that "hash" doesn't look like an MD5; it looks as if you have simply picked up the hashCode of the array.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3737
    
  16
You'd be much better off working with arrays rather than Strings.
Try to do the following

1. Convert you original String to a byte array. There's a method in the String class that will do this for you.

2. Print this array using the Arrays.toString() method

3. Pass this array to your MD5 method.

4. In your MD5 method, use this array to create the md5 hash.

5. Return this hash from the method. You will need to change the return type of the method to do this.

6. Use a for loop to print the elements of the array as ints. You can convert a byte to an int using
(int ) (b & 0x00FF)
where b is the byte variable

7. Create a String from the array returned from the MD5 method. Check the list of String constructors for a quick way to do this.

8. Print this String.
[ July 04, 2008: Message edited by: Joanne Neal ]
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Thanks guys for your help. I followed some of your advice and changed the code as shown below as an example.

It looks like im almost there but not quite yet.



And here is the output from the above program



The bytes shown in line 3 are corect.
The bytes shown in line 4 are partially correct in that the positive numbers are correct. Im not sure why the others have come out as negative numbers. Whats interesting is that when i add 256 to the negative numbers the answer becomes correct. Why does this happen? Have i used a variable that is the wrong size?
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

I managed to sort out the password issue. Thank you all for your help.
I am now having trouble running the same code on jdk1.5. It works on 1.4 as described here.

http://www.coderanch.com/t/410990/java/java/PKCS-Different-resutls-JDK-JDK
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39788
    
  28
Please tell us how you sorted out the password.
Please explain more about why it won't run on Java5; applications from older versions of Java ought all to work on newer versions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help please, what is wrong here.