1

I have a question about function openssl_private_encrypt() in PHP. Before coding, I have valid private key generated with OpenSSL. Based on one of the forum thread in php.net, I have code below:

$fp = fopen("D:\\myprivatekey.pem", "r");
$priv_key = fread($fp, 8192);
fclose($fp);
$res = openssl_get_privatekey($priv_key, 'mypassphrase');
openssl_private_encrypt('plaintext', $crypttext, $res);
echo "String crypted: $crypttext";

I have copied file libeay32.dll and ssleay32.dll from C:\xampp\php to C:\xampp\apache\bin Then the result is full of unreadable character like:

String crypted: ^«ƒ¸GÖÕaR³žØc®íû›&x©bŒsÌ̉R+\,Ιlãq4¨­¼Œ8´˜pcšM¸¹”޵À;›1¬‘­‚ác;³•盤À0޾Ne@»ÿ­¡¯æ´^b‡ãIAÂ)¯{û³òÌ+A=Gô\M¾GrXt^Ëï·âOE-¶¹HÝû×L5ÆÙJ˜”ÄWºÚú9t/Ó:­ÛÑÇÙ­­¨Bpþq/,Œo·@¿e™¾^7[º qom…ójR*ˆn„nÇ®Nx«ïÈøkW2„9L«RÇÇôZ& ãÞò¾rAO „Íj+¥`~û°rÌÿ5Ô!„¿W

How can I get the output as ASCII string MD5-like (but more than 32 in length), for example: 87f0ea083c7a03bb89aec910 ........

I think the unreadable characters can be converted to array of bytes, and then convert it as string, but how I can do that in PHP?

Thanks.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
danisupr4
  • 815
  • 1
  • 9
  • 22

1 Answers1

1

Problem solved. Just use function bin2hex().

Thanks for stackoverflow, when I typed the question, it shows me another similar thread as suggestion, brilliant !!

danisupr4
  • 815
  • 1
  • 9
  • 22