473,503 Members | 1,647 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MD5 encryption question - communication with Java

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, 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

In c# when I try to do this I get a very different value, The current
code is doing this...

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

This returns a value of: ISMvKXpXpadDiUoOSoAfww==

My questions are what would be causing the difference between the Java
output and the c# output? The Java server is expecting the first
value.

What is the proper way to do this?

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)

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.

May 24 '06 #1
3 11025

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.GetByte s(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

May 25 '06 #2
Thank you for the help. Sure enough using only 15 bytes worked. Of
course the fun part of solving this problem is that I now have another
task added to my list. I now also get to go back and figure out what
is happening on the server and fix that too.

May 25 '06 #3
AHanso <br******@gmail.com> wrote:
Thank you for the help. Sure enough using only 15 bytes worked. Of
course the fun part of solving this problem is that I now have another
task added to my list. I now also get to go back and figure out what
is happening on the server and fix that too.


Try closing the encoder - it's no doubt waiting for you to write some
more binary data to it so it can produce the next chunk of base64.
(I suspect there are nicer ways of converting to base64, by the way.
Have a look at the Apache Codec classes. IIRC, they've got some base64
abilities.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5330
by: ^CeFoS^ | last post by:
Hi to everybody, due to I want to use the serial port of a server machine through an applet allocated in html document. > Then one application will run in the server machine and using > the serial...
4
3515
by: Daniel Orner | last post by:
Hello, I'm working on a project that consists of a Java application on the desktop, and a server with Python CGI code. I'm trying to find an encryption algorithm, which would let me send...
1
8985
by: Praveen | last post by:
Hi, I have installed WebSphere Portal on AIX and connected to DB2 on a remote machine, Getting the followin errors when trying to get the values from database thru applications installed on...
8
3725
by: VM | last post by:
Does C# work with encryption and compression? I know that there are C# Encryption class(es) but my client is also interested in compressing the data. We want to use some encryption in the generated...
2
6284
by: Carolyn Vo | last post by:
We have code in Java that encrypts a string using DES. However, when we encrypt in C# the string is encrypted differently. The code to encrypt in Java is: DESKeySpec desKeySpec = new...
0
3253
by: news.onet.pl | last post by:
I hava a problem with communication between Java/Corba server based on JDK ORB with Java/Corba client (applet) based on the same ORB. I`m using IOR to localize server. client`s ORB i initialize...
7
3575
by: bylum | last post by:
Servlet SQLException Communication link failure java howto i can't connect jsp and database(mysql). This is the exception: exception org.apache.jasper.JasperException:...
5
3160
by: AeonOfTime | last post by:
Let's assume a web application (in this case a browser-based game) with a custom HTTP server built on PHP, and a client also built on PHP. The client uses the server to access and change data. Even...
0
1294
jeffstl
by: jeffstl | last post by:
After checking the box "Force Protocol Encryption" on SQL Server Network Utility there are several Crystal reports that begin to throw the following error when run Query Engine Error:...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.