Re: MD5 encryption question - communication with Java

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




AHanso wrote:
Hey I am new to C# (My background is in Java),

I am writing a C# application (that uses the Compact Framework) that
communicates to a Java server. To login the server is expecting the
password to be MD5 encrypted

Hashed.

, and then base64 encoded. The Java client
is doing this as follows, and it works with the server:

private static String encodePassword(String password) {

try {
MessageDigestalgorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(password.getBytes());
byte[] encrypted = algorithm.digest();

ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream encoder = MimeUtility.encode(out, "base64");
encoder.write(encrypted);
return new String(out.toByteArray());

} catch (...exception code )
}
}

This returns a value of: ISMvKXpXpadDiUoOSoAf

Might help if you told us what the password is... or rather, some other
random string, and the results of encoding it in Java and C#.


In c# when I try to do this I get a very different value

Not that different! :)

, The current
code is doing this...

System.Security.Cryptography.MD5 md5 =
System.Security.Cryptography.MD5.Create ();
byte[] encrypted =
md5.ComputeHash(System.Text.Encoding.ASCII.GetBytes(password));
string encoded = Convert.ToBase64String(encrypted, 0,
encrypted.Length);

This returns a value of: ISMvKXpXpadDiUoOSoAfww==

OK so the Java gives us

ISMvKXpXpadDiUoOSoAf

and the C# gives us

ISMvKXpXpadDiUoOSoAfww==

Since base64 encoding turns arbitary 3-byte sequences into printable
4-byte sequences, we can see that the C# output is the result of base64
encoding the same input, with an extra three bytes on the end. However,
we also know that = is the base64 padding character, so there will be
fewer than three bytes extra. We can find out exactly what the extra is
by asking the Immediate window:

?Convert.FromBase64String("fw==")
{Dimensions:[1]}
[0]: 127

Aha. So the input to the base64 encoding is carrying an extra byte
(value 127) on the end, compared to the Java. The Java base64 string is
the encoding of 15 bytes, and the C# base64 is the encoding of 16
bytes.

At this point I wondered which is correct. The .NET MD5 class is
inherited from HashAlgorithm, which specified a HashSize member, which
for MD5 is 128 (bits) = 16 bytes. Checking the definition of MD5
(wikipedia), I see that an MD5 hash is indeed supposed to be 128 bits =
16 bytes. But you say that passing ISMvKXpXpadDiUoOSoAf is currently
giving the correct result? Well, ISMvKXpXpadDiUoOSoAf is the base64
encoding of only 15 bytes! Which makes for something of a mystery, and
smells like a bug *somewhere*.

However, let's not worry about that right now, let's rather just change
the C# from

string encoded = Convert.ToBase64String(encrypted, 0,
encrypted.Length);

to

string encoded = Convert.ToBase64String(encrypted, 0, 15);

and see if it works. If it works, go pester whoever's responsible for
the Java server. If not... come back here! :)


And last, where is the best place to learn about this? I have been
doing a lot of looking on line, but wondering what the best site would
be to learn about some of these issues. (I will have the same problem
looking for XML handeling in C# too)

I usually start with a book for the basics, for more in depth stuff on
the technical side I wuold go to wikipedia, for implementation details
I would search in this group.


Thank you for your help. Other than these problems I have been pretty
shocked at how easy the conversion to C# has been. Kind of fun.

Well, less surprising when you consider the history and motivations
behind C# ...


--
Larry Lard
Replies to group please

.



Relevant Pages

  • Re: Ascii Datei wird maximal 32K =?iso-8859-1?Q?gro=DF?=
    ... Es wird das default encoding verwendet, das der laufenden Plattform. ... Klassisch ist das ne perl aufgabe. ... die java da mehr braucht. ...
    (de.comp.lang.java)
  • Re: Why doesnt strrstr() exist?
    ... > Unicode is a character set, not an encoding. ... > AFAIK the language doesn't specify how to deal with Unicode ... I am not *that* familliar with Java. ...
    (comp.lang.c)
  • Re: Reading text files with java.nio.*
    ... > probably guess I am not a great java programmer!) ... text file in UTF-16BE encoding, this won't be what you want. ... you could treat System.out as a byte stream and don't do ...
    (comp.lang.java.programmer)
  • Re: How can I use class FileInputStream as a reverse of FileOutputStream?
    ... Java also handles different ... > encodings (an encoding specifies how characters are represented as ... > text encoding in the file, use a Writer of some kind. ... > * you want to write binary data, and want to make sure that it is not ...
    (comp.lang.java.help)
  • Re: problem when sending data over TCP socke from c# client to jav
    ... so after adding new line char to the end of the string and sending the java ... > Vadym Stetsyak aka Vadmyst ... >> there is no error on the send method, just the java server doesn't get ...
    (microsoft.public.dotnet.languages.csharp)