Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have tried the below code
PHP
<?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

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900