Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

openssl_public_encrypt and JSON

Tags:

json

php

Let's say we need to store in a crypted way some confidential data into a db. And say that we need them into json format as will be more suitable for data reconstruction.

There's something that I miss that is driving me crazy.

Take that json for instance

$json = {"customer":{"customer_address":"Fake address 123","customer_city":"Fake City","customer_company":"","customer_countrycode":"it","customer_email":"","customer_telephone":"+39.347.xxxxxxx","customer_zip":"yyyyy"},"currency_code":"EUR","commision_amount":"84"}

now I want to crypt this json and I do the following

$pubKey = openssl_pkey_get_public($puk);
openssl_public_encrypt($json, $json_crypted, $pubKey);

if I echo $json_crypted it doesn't show anything, but if I remove some field (like customer_company, that is empty) all seems to work. I've tried to find something into documentation about this strange behaviour but I can't find anything.

Is someone aware of the reason behind that result?

Edit

Even if I remove other field (not an empty one) all seems to work. I'm speechless because it has to be a silly thing that I can't understand

like image 588
DonCallisto Avatar asked Sep 03 '25 17:09

DonCallisto


1 Answers

From the comments in documentation:

http://www.php.net/manual/en/function.openssl-public-encrypt.php#95307

openssl_private_encrypt() has a low limit for the length of the data it can encrypt due to the nature of the algorithm.

To encrypt the larger data you can use openssl_encrypt() with a random password (like sha1(microtime(true))), and encrypt the password with openssl_public_encrypt(). This way the data can be encrypted with a public key and decrypted with the private one.

Your json must exceed the length limit...

like image 132
Getz Avatar answered Sep 05 '25 07:09

Getz