Priyanka Gurav
Priyanka Gurav

Reputation: 21

How to encrypt data using rsa public key in php

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.

Upvotes: 1

Views: 466

Answers (1)

Maarten Bodewes
Maarten Bodewes

Reputation: 94058

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

Upvotes: 1

Related Questions