Trying to create bytearray from string in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
raine
Forum Newbie
Posts: 3
Joined: Wed Jun 24, 2009 2:35 pm

Trying to create bytearray from string in php

Post by raine »

Hi!

I've run to this problem because i need to call webservice which wants password md5(bytearrayfromstring).. The receiving webservice is created with java so they probably use in their end String.GetBytes();

Does anybody know how to create bytearray in php or the logic behind Java's GetBytes()?

Raine
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Trying to create bytearray from string in php

Post by requinix »

Strings can be treated as arrays - you don't need any kind of "GetBytes" function for it.

Code: Select all

$string = "Example";
echo $string[0]; // E
PHP's md5() creates a binary string then encodes it into hexadecimal. I'd say ~67% of services like it in that format. The other ~33% want a base-64 encoded representation of the raw binary output.

Code: Select all

// 2/3rds of people want
echo md5("Example");
 
// The others want
echo base64_encode(md5("Example", true));
raine
Forum Newbie
Posts: 3
Joined: Wed Jun 24, 2009 2:35 pm

Re: Trying to create bytearray from string in php

Post by raine »

Hi tasairis! and thank you for your reply!

This doesn't solve the problem. I'll try to be more spesific..
with java:

Code: Select all

 
        String password = "Password";
        try{
        MessageDigest md5 = MessageDigest.getInstance("MD5");
 
        byte[] key = md5.digest( (password).getBytes());
        String encodedKey = new sun.misc.BASE64Encoder().encode(key);
 
        System.out.println(encodedKey);
        }catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
 
The result with Java is: 3GR+tl5nEeFVN1IYISs5ZA==

with PHP:

Code: Select all

 
<?php
$password = "Password";
 
$key = md5($password,true);
$encodedKey = base64_encode($key);
 
echo $key;
?>
 
The result with PHP is: Üd~¶^gáU7R!+9d
And when the md5 true parameter is false: dc647eb65e6711e155375218212b3964
Ok I think i solved this i had one variable in wrong place :)

This wasn't about bytearray :)
Post Reply