Click here to Skip to main content
11,924,995 members (46,199 online)
Rate this:
Please Sign up or sign in to vote.
See more: PHP Encryption
i have tried the below code
class MyEncryption
public $pubkey = '123';
public $privkey = '123';
public function encrypt($data)
if (openssl_public_encrypt($data, $encrypted, $this->pubkey))
$data = base64_encode($encrypted);
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;
$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 21-Jan-13 7:13am
Sergey Alexandrovich Kryukov 21-Jan-13 12:37pm
Why are you asking about RSA but show base64..?

1 solution

Rate this: bad
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151125.3 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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