title: Why do Java MD5 and PHP MD5 deliver entirely different results?
body: <p>While preparing access data for a website, I suddenly got aware of the fact that the MD5 encrypted passwords created by Java are not at all the same as those created by PHP MD5. Looking through the postings here and elsewhere I found several solutions in Java assuring that the would deliver exactly the same result as PHP - but all Java implementations deliver the same result among themselves but an entirely different result from the one by PHP.</p>
  
  <p>I therefore wrote a small Java program sending the same password to my local server for conversion by PHP, and at the same time converting it by itself using Java. Here the program:</p>
  
  <pre><code>public PHPDriver() {
    String pwd = "aabbccdd";
    String p = encodeByPHP("http://localhost/testsite/md5.php?pwd=" + pwd);
    System.out.println("PHPDriver:       " + pwd + " -&gt; " + p);
  
    System.out.println("md5:             " + pwd + " -&gt; " + md5(p));
  ...
  public String encodeByPHP(String url) {
    try {
      // create a link to  a URL
      URL urlAddress = new URL(url);
      URLConnection link = urlAddress.openConnection();
      BufferedReader inStream = new BufferedReader(new InputStreamReader(link.getInputStream()));
      return inStream.readLine();  
    } catch (MalformedURLException e) {
  ...
  public String md5(String input)  {
    String result = input;
    try {
      if(input != null) {
        MessageDigest md = MessageDigest.getInstance("MD5"); //or "SHA-1"
        md.update(input.getBytes());
        BigInteger hash = new BigInteger(1, md.digest());
        result = hash.toString(16);
        while(result.length() &lt; 32) {
          result = "0" + result;
        }
      }
    } catch (NoSuchAlgorithmException nsa) {
  </code></pre>
  
  <p>And the (overwhelmingly complicated;-) PHP page:</p>
  
  <pre><code>&lt;?php
    $pwd = $_GET['pwd'];
    // echo $pwd . ' -&gt; ';
    echo sha1($pwd); 
  ?&gt;
  </code></pre>
  
  <p>The result of this looks as follows:</p>
  
  <pre><code>PHPDriver:       aabbccdd -&gt; 527bee2730bf234e9a78bde5af091ece9c6302d5
  md5:             aabbccdd -&gt; ab86815613f7f321001efef1935dbe7d
  </code></pre>
  
  <p>What's going wrong here? Is this an mis-encoding problem? And why is the PHP result 40 char's long instead of the usual 32?</p>