Jump to content

Help with Crypt_RSA


richever

Recommended Posts

I'm not sure if my problem is due to a misunderstanding of the Crypt_RSA APIs or how public key encryption works, or both.  I am getting a token that has been encrypted using RSA and I need to decrypt it and parse the resulting text. 

 

I am replacing a Java application with a php page. From the Java app I am using the file, called 'private.key',  that it was using to decrypt the token.  This file is binary data that contains the private key.  Based on the documentation for Crypt_RSA I wrote the following script:

 

<?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;
    }
  }
}
?>

 

The problem is with the $key.  When check_error($key) is called I get an error saying that it needs to be set as either 'public' or 'private'.  The problem is that I don't understand how to do this.  Looking over the API for Crypt_RSA leads me to believe that I need to create a Crypt_RSA_KeyPair first and then get the keys, public and private, from that.  I see a couple ways to do this but none of them seem like the right thing to do. 

 

Any help much appreciated!

 

Rich

Link to comment
https://forums.phpfreaks.com/topic/102849-help-with-crypt_rsa/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.