locked
Translate java code to C# issue RRS feed

  • Question

  • User-1895633123 posted

    Hi, we are using a third party application that uses java for remote authentication. Unfortunately the developers of the app weren't able to assist us in translating the code into C# or give us any assistance in the matter.

    public static final char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7','8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    public static byte[] md5Of(List<String> list) {
    	try {
    		StringBuffer rawString = new StringBuffer();
    		for (String elem : list) {
    			rawString.append(elem);
    		}
    		byte[] p = rawString.toString().getBytes("ISO-8859-1");
    		MessageDigest md = MessageDigest.getInstance("MD5");
    		md.reset();
    		md.update(p);
    		byte[] digest = md.digest();
    		return digest;
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return null;
    }
    
    public static String hexdigit(byte[] input) {
    	char[] b16 = new char[input.length * 2];
    	int i = 0;
    	for (byte c : input) {
    		int low = c & 0xF;
    		int high = (c & 0xF0) >> 4;
    		b16[(i++)] = HEX[high];
    		b16[(i++)] = HEX[low];
    	}
    	return new String(b16);
    }

    The above is the java code that needs to be translated.

    I have made two separate attempts to translate it, but the string being returned does not match the format java is looking for.

            public static readonly char[] HEX = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    
            public static byte[] md5Of(IList<string> list)
            {
                try
                {
                    StringBuilder rawString = new StringBuilder();
                    foreach (string elem in list)
                    {
                        rawString.Append(elem);
                    }
                    byte[] digest = ConvertStringToMD51(rawString.ToString());
                    return digest;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    Console.Write(e.StackTrace);
                }
                return null;
            }
    
            public static byte[] ConvertStringToMD51(string ClearText)
            {
                Encoding enc = Encoding.GetEncoding("ISO-8859-1");
                byte[] ByteData = enc.GetBytes(ClearText);
                MD5 gene = MD5.Create();
                byte[] HashData = gene.ComputeHash(ByteData);
                StringBuilder oSb = new StringBuilder();
                for (int x = 0; x < HashData.Length; x++)
                {
                    oSb.Append(HashData[x].ToString("x2"));
                }
                return Encoding.Unicode.GetBytes(oSb.ToString());
            }
    
            public static string hexdigit(byte[] input)
            {
                char[] b16 = new char[input.Length * 2];
                int i = 0;
                foreach (byte c in input)
                {
                    int low = c & 0xF;
                    int high = (c & 0xF0) >> 4;
                    b16[(i++)] = HEX[high];
                    b16[(i++)] = HEX[low];
                }
                return new string(b16);
            }

    Attempt number one. Then I read up and thought perhaps sbyte would be better suited. (The java converted app I used also suggested sbyte, but things had to be changed since the translated code used invalid functions that were only available in java)

    public String ConvertStringToMD51(string ClearText)
      {
       byte[] ByteData = Encoding.ASCII.GetBytes(ClearText);  
    
       
       MD5 md5 = MD5.Create();
       byte[] HashData = md5.ComputeHash(ByteData);
       StringBuilder oSb = new StringBuilder();
       for (int x = 0; x < HashData.Length; x++)
       {
        oSb.Append(HashData[x].ToString("X2"));
       }
    
       SByte[] FinalHash = ConvertToSbyte(HashData);
       return encBytes( FinalHash );
      }
    
     public SByte[] ConvertToSbyte(Byte[] arr)
      {
       SByte[] Hash = new SByte[arr.Length];
       int i = 0;
       foreach (Byte b in arr)
       {
        Hash[i] = unchecked((SByte)b);
        i++;
       }
       return Hash;
      }
    
    public String encBytes(SByte[] barr)
      {
       StringBuilder licKey = new StringBuilder();
       for (int i = 0; i <= 15; i++)
       {
        SByte i1 = barr[i];
        int j = 0;
        j |= i1 & 0xFF;
        j %= 32;
        if (i > 0 && i % 4 == 0)
        {
         licKey.Append("-");
        }
        licKey = licKey.Append(Convert.ToChar(validChars.Substring(j,1)));
    
       }
    
       return licKey.ToString();
      }

    As you can see the MessageDigest does not exist in C#, but the code in C# is the attempt I made to replicate this behavior. It's obviously not working at this point.

    If anyone has any insight on what could be wrong, or any assistance, it would be greatly appreciated.

    Regards

    Marilee

    Friday, August 22, 2014 3:46 AM

Answers

  • User-271186128 posted

    Hi PaySpaceMG,

    Thanks for your posting.

    According to your code and description, I think you want to use MD5 in C#. So, please refer to the following code:

            static string GetMd5Hash(MD5 md5Hash, string input)
            {
    
                // Convert the input string to a byte array and compute the hash.
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
    
                // Create a new Stringbuilder to collect the bytes
                // and create a string.
                StringBuilder sBuilder = new StringBuilder();
    
                // Loop through each byte of the hashed data 
                // and format each one as a hexadecimal string.
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }
    
                // Return the hexadecimal string.
                return sBuilder.ToString();
            }
    

    More details, please refer to the following link: http://msdn.microsoft.com/en-us/library/system.security.cryptography.md5(v=vs.110).aspx

    If you have any further questions, please feel free to post in this forum.

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 25, 2014 4:25 AM