1

Receiver has given me a public key and I need to use it to encrypt username and password. I have done various r&d but nothing works. I have used

function public_encrypt($plaintext)
{
    $fp=fopen("private.key","r");
    $pub_key=fread($fp,8192);
    fclose($fp);
    openssl_get_publickey($pub_key);
    openssl_public_encrypt($plaintext,$crypttext, $pub_key);
    return(base64_encode($crypttext));
}

But it gives me openssl_public_encrypt(): key parameter is not a valid public key error.

I have also added begin and end line to key, but still no success Kindly guide me with steps to follow as I am new in it.

12
  • $pubkey=openssl_get_publickey($pub_key); Commented Jul 25, 2016 at 10:48
  • I have tried it. used that $pubkey in encryption, still no success. Still getting same error. please help. Commented Jul 25, 2016 at 10:57
  • The get publickey needs the certificate file Commented Jul 25, 2016 at 10:59
  • 2
    It's interesting that you named your public key as private.key. The problem here is that you didn't receive properly formatted key. The second part of the problem is that you can't encrypt any amount of data with public key, there's a limit and it's rather small. Public/private key cryptography is usually used to transmit a symmetric key between two parties. If I were you, I'd ask for a PEM formatted public key so you don't have to go through the process of trying to coerce it yourself into what openssl understands. Commented Jul 25, 2016 at 12:54
  • 1
    Also, @Jonasw - where did you read that public key starts with ----PUBLIC KEY START----? That's not the correct delimiter in any of the encoding formats. Commented Jul 25, 2016 at 12:57

1 Answer 1

1

You're trying to decode a key that is not in any known format it seems. I presume it has been made by somebody that does not fully understand crypto, as it has a 1023 bit modulus.

If you look at the hexadecimal representation you'll find a modulus with a value of:

4D49BB0D2E0FA132D081A6338C178124AB2B1B61A57C6C30D05EAD179BE1040B235E0EE83F3BF29A8F19DC33B7E245FAE7BE96E35CC2DF49E8B519D4F53501E3566D693A66E69D8C812AF66AC6D5D86ED764ED27A91C5828CE860A8B01077C15142B77BF772AFF201577DF8FBE9E92168539480E024DFF51173CD26B65858ACA

(simply the last value within the structure with a size close to 1024 bits) and a public exponent with a value

010001
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.