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

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


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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs