# RSA and PHP

**URL:** <https://www.ruby-forum.com/t/rsa-and-php/174749>\
**Category:** Rails\
**Created:** [September 1, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/rsa-and-php/174749 "2009-09-01T16:44:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![joana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed655f/32.png) [@joana](https://www.ruby-forum.com/u/joana)\
**Post date:** [September 1, 2009, 4:44pm UTC](https://www.ruby-forum.com/t/rsa-and-php/174749/1 "2009-09-01T16:44:55Z")

</div>

Hi,

I am trying to encrypt a string on PHP using RSA which should then be  
decrypted by Ruby on Rails. In PHP, I am using the following commands:

$fp = fopen(…);  
$pKey = fread($fp, filesize(…));  
openssl\_public\_encrypt($data, $enc, $pKey);  
return base64\_encode($enc);

On Ruby on Rails, I get the following error: “padding check failed”

I already tried all padding configurations possible in PHP without  
success.

Has anyone managed to exchange RSA strings between PHP and Ruby?

Cheers,  
joana

---

<div class="post-metadata">

**Author:** ![joana](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed655f/32.png) [@joana](https://www.ruby-forum.com/u/joana)\
**Post date:** [September 2, 2009, 3:29am UTC](https://www.ruby-forum.com/t/rsa-and-php/174749/2 "2009-09-02T03:29:59Z")

</div>

joana wrote:

> $fp = fopen(…);  
> $pKey = fread($fp, filesize(…));  
> openssl\_public\_encrypt($data, $enc, $pKey);  
> return base64\_encode($enc);

Given that the actual encryption is handled by OpenSSL on both sides, I  
would expect you are still missing some configuration difference. Either  
in the algorithms used or as hinted in the padding being used. There  
could also possible be some difference in the Base64 encoding/decoding,  
but likely not. One other remote possibility might be the character  
encoding that’s used which writing/reading the Base64 strings.

That’s about all I can think of right away. The key thing here though is  
if OpenSSL is truly setup the same way on both sides it should work.

I was also wondering why you chose a public key encryption over using a  
symmetric key. Are you doing some form of key exchange between the two  
applications? Of course, this might be perfectly logical in your case,  
but I would typically go with a symmetric algorithm (such as AES) to  
both dramatically increase the encryption speed and eliminate the key  
exchange phase.
