I am trying to implement a php page that encrypts a token the same way that a java program does.

My java code looks like this:

DESede desede = new DESede( seed,-1);
String encoded = desede.encode( encodeMe );

This gives me back a printable string.

My php code looks like this:
$crypted=mcrypt_ecb(MCRYPT_3DES,$key,$toencrypt, MCRYPT_ENCRYPT);

Which returns a binary string. The encode() is doing some kind of encoding that I need to duplicate in php.

Clues appreciated.
Thanks

    What does the "printable string" look like? Does the Java program have any documentation?

      here is an example:

      Java src like this:
      DESede desede = new DESede( seed,-1);
      String encoded = desede.encode( encodeMe );
      System.out.println("EncodeMe: "+encodeMe+ " Encoded token:" + encoded );

      Output like this:
      Seed: b166305afb02f2f8b166305afb02f2f8
      EncodeMe: |1082474855830|helloworld Encoded token:cWdqJ8GWJ2P5LQuAoGk
      ErIKDsz7V5uzK9YVap9GO3ww=

      Whereas for the same input my php gives me:

      Crypted : "cG"ÒØq├CâxÒ[0·ð÷ìºÑ»~·qZM┤ÏË╗♀■

      Not the same ball park, not printable.

      This is my first php prog, be gentle !.

        Thought it would be something like that; looks like the Java code has been run through base64 encoding: [man]base64_encode[/man].

          Hey weedpacket, thanks for sticking with me !.

          I worked out the base64 encoding, but thats not the full story unfortunaltely.
          I was still getting slightly different tokens from the two implementations. eg: from php I would get this: (base64)
          NMZfff9JyQNKaMiiI2vcWoKDsz7V5uzK9YVap9GO3ww=

          and from the java:
          NMZfff9JyQNKaMiiI2vcWoKDsz7V5uzKzQy9JfS90FA=

          they differ after character 32.

          I looked in more detail at the java class I was using, with a bit of 'jad' I got the source of the 'encode()' method, which looks like this:
          encodecipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
          I wonder if this PKCS5Padding has anything to do with it ?.

          I have printed the data and key before encryption and they are the same, the results after encryption differ as above.

          My PHP looks like:

          $data = '|1082538054680|helloworld';
          
          $mcrypt_module = mcrypt_module_open('tripledes', '', ecb', '');
          
          //getting initialization vector
          $mcrypt_iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($mcrypt_module), MCRYPT_RAND);
          
          //getting key size for this encryption mode
          $key_size = mcrypt_enc_get_key_size($mcrypt_module);
          echo('Key size is:'.$key_size."\n"); // 24 !.
          mcrypt_generic_init($mcrypt_module,$key,$mcrypt_iv);
          
          $encrypted_data = mcrypt_generic($mcrypt_module, $data);
          $base64_encrypted = base64_encode( $encrypted_data );
          echo('Here is the base 64 encrypted :'.$base64_encrypted."\n");
          

          Can't see a way to specify that PKCS5Padding part.
          Hmm....

            I spent a couple of days working through mcrypt, and getting php's mcrypt stuff to be useable with other systems. What i discovered was an issue with PHP's handling of types. When you go to do the conversion to base64, php does some odd conversions to the encrypted string, and you end up with something that's CLOSe...but not exactly what you need.

              Write a Reply...