Need help with Crypt_RSA
| From: | Richard Everhart | Date: | Mon, 28 Apr 2008 23:24:07 +0000 |
| Subject: | Need help with Crypt_RSA | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-28845@lists.php.net to get a copy of this message | ||
I'm having problems with the Crypt_RSA extension. This is probably due to my
misunderstanding of its APIs but I'm not sure.
Here is what I'm doing: I am currently replacing a Java app with a PHP app. The
Java web app is passed a token (a string) encrypted using RSA. The provider on
the sending side is BouncyCastle and the KeyFactory is defined as 'RSA'. I'm
not sure if that makes any difference but I'm mentioning it just in case.
The Java app I'm replace does just the reverse of the Java app sending the token
- it decryps the token using RSA. On the decrypt side, I have a file called
private.key (also used by the Java app I'm replacing) containing binary data.
The encrypting Java app has a file called public.key it used to encrypt the
token before sending.
In my php code I'm reading the private.key file into a string. However, when I
check for errors I get the following:
error: invalid key type. It must be private or public
What I'm not clear on is how I specify that the key is private or public? I can
see doing this if I've generated the KeyPair myself but how do I do this when
reading in a key from a file?
Here's my code below. It is taken from the example included with Crypt_RSA.
<?php
require_once 'Crypt/RSA.php';
if (checkForValue() == 0)
{
$token = $_GET['token'];
if (empty($token))
{
// do something...
}
echo "token: " . $token;
$private_key = file_get_contents('private.key', FILE_BINARY);
$key = Crypt_RSA_key::fromString($private_key);
check_error($key);
$rsa = new Crypt_RSA;
check_error($rsa);
$rsa->setParams(array('dec_key' => $key));
check_error($rsa);
$plain_txt = $rsa->decrypt($token);
check_error($rsa);
echo "plain_txt: " . $plain_txt;
}
function check_error(&$obj)
{
if ($obj->isError())
{
$error = $obj->getLastError();
switch ($error->getCode())
{
case CRYPT_RSA_ERROR_WRONG_TAIL :
// nothing to do
break;
default:
// echo error message and exit
echo "error: " . $error->getMessage();
exit;
}
}
}
?>
Any help much appreciated.
Rich