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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘