3 Replies Latest reply on Jan 9, 2009 5:59 AM by Frank Kraut

    problems with MD5 encoding

    Frank Kraut Newbie

      Hey,
      does everyone knows why the MD5 encoding with the libary jbosssx is different from the encoding with the java.security.MessageDigest libary?
      I use JBoss 4.2.3.GA and JDK 1.6.0_05.
      And how can I use the libary from JBoss? I didn't found any method with an returning statement in combination with MD5. That's why I used the JDK libary.

      MessageDigest md = MessageDigest.getInstance("MD5");
       md5 = md.digest(pw.getBytes());
      
       //decodieren in base 64
       encoded = new BASE64Encoder().encode(new String(md5).getBytes());
      


      Thank you for any help.

        • 1. Re: problems with MD5 encoding
          Rohitha Dayan Gunawardhana Newbie

          This may be help you.

          public String getMD5(String str) throws NoSuchAlgorithmException {
           MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
           digest.update(str.getBytes());
           byte[] hash = digest.digest();
          
           return hexStringFromBytes(hash);
           }
          
           public String hexStringFromBytes(byte[] b){
           char[] hexChars ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
           String hex = "";
          
           int msb;
          
           int lsb = 0;
           int j;
          
           // MSB maps to idx 0
          
           for (j = 0; j < b.length; j++){
          
           msb = ((int)b[j] & 0x000000FF) / 16;
           lsb = ((int)b[j] & 0x000000FF) % 16;
           hex = hex + hexChars[msb] + hexChars[lsb];
           }
           return(hex);
           }


          • 2. Re: problems with MD5 encoding
            Frank Kraut Newbie

            I need the MD5 encoding with base64. How do I get it with your method so that the result for the string "hendrix" is "schPjWcrXWqEp6SG6BtGWg==".
            The result with your method with bas64 for "hendrix" was "QjFDODRGOEQ2NzJCNUQ2QTg0QTdBNDg2RTgxQjQ2NUE=" and the result with the jdk was "schPP2crXWqEp6SG6BtGWg==".

            • 3. Re: problems with MD5 encoding
              Frank Kraut Newbie

              problem solved...it was my mistake.
              with

              encoded = new BASE64Encoder().encode(md5);
              
              does it work.