Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: PHP Encryption
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 21-Jan-13 6:13am
Comments
Sergey Alexandrovich Kryukov at 21-Jan-13 12:37pm
   
Why are you asking about RSA but show base64..?
—SA

1 solution

Rate this: bad
good
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.
  Permalink  

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

  Print Answers RSS
0 Maciej Los 450
1 OriginalGriff 393
2 Sergey Alexandrovich Kryukov 229
3 CPallini 189
4 CHill60 180
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 5,022
2 Maciej Los 3,269
3 Peter Leow 3,164
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 21 Jan 2013
Copyright © CodeProject, 1999-2014
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