Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7608795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:00:41+00:00 2026-05-31T01:00:41+00:00

I have a web service developed in c#. It uses MD5 to generate session

  • 0

I have a web service developed in c#.
It uses MD5 to generate session key.

c# :

public static string GetMD5(string pTxt)
{
    string sCTxt = "";
    byte[] aTxt;
    UnicodeEncoding oEnc = new UnicodeEncoding();
    aTxt = oEnc.GetBytes(pTxt);
    HashAlgorithm oHash = new MD5CryptoServiceProvider();
    byte[] aCTxt = oHash.ComputeHash(aTxt);
    foreach (byte b in aCTxt)
        sCTxt += String.Format("{0:X2}", b);
    return (sCTxt);
}

For several reasons, I have to make the same GetMD5 method in PHP.
Of course, the basic md5() function does not return the same hash (because of the UNICODE)

I tried to simulate the code in PHP but with no success

php:

public function HexToBytes($s) {
    return join('', array_map('chr', array_map('hexdec', str_split($s, 2))));
}

public function GetMD5($pStr) {
    $data = mb_convert_encoding($pStr, 'UTF-16LE', 'ASCII');
    $h = $this->HexToBytes(hash_hmac('md5', $data, ''));
    return (base64_encode($h));
}

Any idea why the result are not the same ?

Thanks in advance


**

FIXED! Thanks!

**

For those interested, here is the PHP method that matches the c# one

public function str2hex($string) {
   $hex = "";
   for ($i = 0; $i < strlen($string); $i++)
      $hex .= (strlen(dechex(ord($string[$i]))) < 2) ? "0" . dechex(ord($string[$i])) : dechex(ord($string[$i]));       
   return $hex;
}

public function GetMD5($pStr) {
   $data = mb_convert_encoding($pStr, 'UTF-16LE', 'UTF-8');
   $h = $this->str2hex(md5($data, true));
   return strtoupper($h);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:00:42+00:00Added an answer on May 31, 2026 at 1:00 am

    I think you just overcomplicated your methods. The following alternatives worked for me:

    C#:

    public static string GetMD5(string text)
    {
        byte[] textBytes = Encoding.UTF8.GetBytes(text);
        byte[] hash = MD5.Create().ComputeHash(textBytes);
    
        return Convert.ToBase64String(hash);
    }
    

    PHP:

    public function GetMD5($pStr) {
        return base64_encode(
                  md5(mb_convert_encoding($pStr, "UTF8", "Unicode"), true));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed a web service and host it on my server and I
I have developed a Web service using WCF Service Application. This service application is
i have developed a web servcies in my local system with name (inbox service).
I have developed a web application that uses a web server and database hosted
I have developed a web service using netbeans. It's currently being hosted locally(localhost). I
I have a web service developed in ColdFusion that I am trying to consume
My company has developed a .NET web service and a client dll that uses
I have a developed an iphone application. The web service was developed by someone
I have developed a web service with the .asmx extension using C# and i
I have developed a web service using JAX-WS and in the web.xml i had

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.