douzhaobo6488 2014-04-11 12:49
浏览 293
已采纳

openssl_public_encrypt和JSON

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

  • 写回答

1条回答 默认 最新

  • doujiu3768 2014-04-11 13:01
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题