Click here to Skip to main content
14,644,614 members
Rate this:
Please Sign up or sign in to vote.
See more:
i have tried the below code
<?php
class MyEncryption
{
public $pubkey = '123';
public $privkey = '123';
public function encrypt($data)
{
if (openssl_public_encrypt($data, $encrypted, $this->pubkey))
$data = base64_encode($encrypted);
else
throw new Exception('Unable to encrypt data. Perhaps it is bigger than the key size?');
return $data;
}
public function decrypt($data)
{
if (openssl_private_decrypt(base64_decode($data), $decrypted, $this->privkey))
$data = $decrypted;
else
$data = '';
return $data;
}
}
$test=new MyEncryption();
echo $test->encrypt("grr");
?>


but i got the below error

Warning: openssl_public_encrypt() [function.openssl-public-encrypt]: key parameter is not a valid public key in C:\xampp\htdocs\speed\rsa.php on line 11
Fatal error: Uncaught exception 'Exception' with message 'Unable to encrypt data. Perhaps it is bigger than the key size?' in C:\xampp\htdocs\speed\rsa.php:14 Stack trace: #0 C:\xampp\htdocs\speed\rsa.php(34): MyEncryption->encrypt('grr') #1 {main} thrown in C:\xampp\htdocs\speed\rsa.php on line 14
Posted
Comments
Sergey Alexandrovich Kryukov 21-Jan-13 12:37pm
   
Why are you asking about RSA but show base64..?
—SA

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The error message is quite descriptive - the string '123' is not a valid public key.

This PHP manual page[^] and this OpenSSL HOWTO page[^] should give you some hints about how you can get public and private keys.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100