Jump to content

RSA Encryption Problem


rchamberlin

Recommended Posts

I need to send a password that's RSA encrypted with a public key (along with some other data).  Here are the exact instructions from the documentation for the password:

 

  • Encode password in Base64 and ensure UTF-8 encoding
  • Encrypt password with RSA with provided public key, no block mode, and PKCS1Padding
  • Encode resulting encryption in Base64 with ensured UTF-8 encoding

Here's the code I'm using that to me seems correct:

function encryptPassword($pass) {

     $pass = base64_encode($pass);

     $fp = fopen("./cert.crt", "r");
     $publicKey = fread($fp, 8192);
     fclose($fp);
     openssl_get_publickey($publicKey);

     openssl_public_encrypt($pass, $cryptedText, $publicKey, OPENSSL_PKCS1_PADDING);

     return base64_encode(utf8_encode($cryptedText));
}

I'm getting an error on the other side saying the password can't be decoded.  Support from them is basically non-existent other than the error message, and them saying everything is correct on their side.  My question is, am I sending what they're asking for according to the documentation?

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/294531-rsa-encryption-problem/
Share on other sites

UTF-8 encoding in the last step doesn't make sense. I think they mean:

1. Make sure password is in UTF-8 encoding

2. base64_encode() it

3. Encrypt

4. base64_encode() that too

 

So try without the utf8_encode().

 

Thanks, I actually forgot to take that out before I posted.  I threw it in there as a last ditch attempt to get it working before I went to the message boards.  Taking it out has not affect on the result.  I appreciate the response!

The code seems right, though the

openssl_get_publickey($publicKey);
isn't doing anything so you should remove it.

 

Have you checked the return value manually? Does it look like random alphanumeric characters?

 

General advice applies too: make sure you have your environment set up for development by using

error_reporting = -1
display_errors = on
in your php.ini (restart the web server if not) and check for error messages.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.