Page 1 of 1
Trying to create bytearray from string in php
Posted: Wed Jun 24, 2009 2:39 pm
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
Re: Trying to create bytearray from string in php
Posted: Wed Jun 24, 2009 3:22 pm
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));
Re: Trying to create bytearray from string in php
Posted: Thu Jun 25, 2009 2:17 am
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
