I currently have a java program that is creating a MD5 message digest using the following...
MessageDigest dig = MessageDigest.getInstance("MD5");
dig.update(secret);
dig.update(key, offset, len);
return dig.digest();
The problem I am having is that java is using a signed byte array for key and unsigned for secret. How is it possible to change this is C#?
I have the following code in C#:
MD5CryptoServiceProvider md = new MD5CryptoService Provider();
CryptoStream cs = new CryptoStream(Stream.Null, md, CryptoStreamMode.write);
cs.Write(secret, 0, 0);
cs.Write(key, offset, len);
cs.Close();
However secret is a byte[] and key is an sbyte[] to make them the same as in java. I ahave no idea? Any help would be appreciated.
Pankaj TalsaniaPosted Dec 11, 2007, 5:27 PM
Hi AR,
are you able to convert to C# from JAVA? i am in same boat. I need to convert similar application. I have some problem. let me know. Thx
AlanPosted Oct 1, 2007, 4:04 PM
As CryptoStream.Write() takes a byte[] as its first parameter, I think you'll just have to convert 'key' from an sbyte[] to a byte[] before calling the method.
I think just casting each sbyte element to a byte with overflow checking turned off will probably suffice for cryptography purposes because, as the following console app shows, the bit representation is maintained when the sbyte contains a negative value (i.e. -1 corresponds to 255, both of which have a bit pattern of 11111111 etc.):
using System;
class Program
{
static void Main()
{
sbyte[] skey = new sbyte[5]{2, 1, 0,-1, -2};
byte[] key = new byte[skey.Length];
for (int i = 0; i < skey.Length ; i++)
{
key[i] = unchecked((byte)skey[i]);
}
foreach(byte b in key)
{
Console.WriteLine(b); // 2, 1, 0, 255, 254
}
Console.ReadLine();
}
}