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
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!

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.