poojitha
poojitha

Reputation: 335

openssl_public_encrypt method doesn't encrypt beyond 118 chracters

I can encrypt small strings but when it exceeds 118 characters it doesn't seems to work ? any idea ?

private function encryptData($plaintext) {

    $plaintext = '{ "id":"1" "name":"Bottled Beer" "description":"" "parent_id":"111" "taxonomy":"SIMPLE_PLU" },{ "id":"2" "name":"Stout" }';

    $publicKey = "../ssh_keys/coasts/public/pub.pem";  

    $fp=fopen($publicKey,"r"); 
    $pub_key_string=fread($fp,8192); 
    fclose($fp); 

    $pubKey = openssl_pkey_get_public($pub_key_string);

    openssl_public_encrypt($plaintext, $encrypted, $pubKey);

    return $encrypted;
}

Upvotes: 0

Views: 2065

Answers (1)

hago
hago

Reputation: 1710

The length of plain data used to encrypt depends on the length of your key, it must be not bigger than the number the length of key subtract 11. For example, if your use a 2048bit key, the maximum length is 2048/8 - 11 = 245.
If you need to process data more than that limit, you could separate you plain data into blocks, encrypt them each and concatenate the cipher blocks(their lengths always equals the length of your key in byte). of course, you have to do the same while decrypting the concatenated cipher data.

Upvotes: 9

Related Questions