doqw89029 2019-04-29 21:43
浏览 118

使用utf8格式的值在json中传输加密数据

I'm trying to stringify output from openssl_public_encrypt and other openssl functions i php, and the output don't seem to be utf8 encoded. Here is a sample code that generate the error that is my problem in a nutshell.

<?php
  $pubkey=<<<EOD
  -----BEGIN PUBLIC KEY-----
  MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAueWffJhr4j+PZhf4QlFF
  1HEmcu9d93YYBIQdZBZLWx4uqxsZ6Q3FaBVMkHh0h+sDHx1je2fQprTEMjWGSIu0
  HlXRZqPLkVUCpQg2j1oQk2BbZExS6kyziVa1G9ai094WqMz3MjyimOvJxuCAsb+i
  rQ/HaC2+vBAdm8wjLYEkqe/q7Q6Tnf+U6bpPYASXTz0WlLJj/G2LLTpEYzF3IgTB
  tRsTI6hwpmpHzpKUucEvliEesEPMAs3xp4AaKBdqKQoGFsiA2p1jxJIRUXC/ur7f
  2ZgWI59AtemVd+FRZfUapfe5uDD3M5cJy/6Uh9Yg+7vMzuCzi/yBDDFwyy4hD2RJ
  YwIDAQAB
  -----END PUBLIC KEY-----
  EOD;
  $jsontest= new \stdClass();
  $data="Testing some text ÆØåæøåéè";
  openssl_public_encrypt($data,$encrypted,$pubkey,OPENSSL_PKCS1_OAEP_PADDING);
  //Next line outputs encoding UTF8 sometimes but not consequently
  echo "

data1: ".mb_detect_encoding($encrypted)."
";
  $jsontest->data1=$encrypted;
  $data="Testing some other text ÆØåæøåéè";
  openssl_public_encrypt($data,$encrypted,$pubkey,OPENSSL_PKCS1_OAEP_PADDING);
  //Next line outputs encoding UTF8 sometimes but not consequently
  echo "

data1: ".mb_detect_encoding($encrypted)."
";
  $jsontest->data2=$encrypted;
  header('Content-Type: application/json; charset=UTF-8');
  //print_r($jsontest);
  $json=null;
  try {
      $json = json_encode($jsontest, JSON_THROW_ON_ERROR);
  } catch (JsonException $e) {
      echo 'Error:'.$e;
  }
  if($json)echo "JSON output:
$json";
?>

Expected output would be a stringified json object with utf8 encoded property values. Instead i get this error message:

"Error:JsonException: Malformed UTF-8 characters, possibly incorrectly encoded in 'the php file':24"

When i run the above code snippet, the 'mb_detect_encoding' lines output 'UTF-8' sometimes, but not always.

There seems to be a problem in openssl_public_encrypt, where the output is not conform to utf-8 encoding.

A very strange behavior detected: Probably mb_detect_encoding does not detect correct, because the json_encode function fails every time. and probably openssl_public_encrypt is to blame for this behavior.

Anyways i can't stringify the supposedly UTF-8 encoded output from openssl_public_encrypt. I use base64 encoding of encrypted data for now as a solution, but the data overhead is around the double of original data.

I use openssl in php to encrypt/decrypt with rsa, ecdh and aes, in conjuction with javascript webCrypto.

Can anybody help me solve this problem, as i am probably not the only one who has this problem.

Edit:

Got it wrong! The function json_encode in php is the showstopper! It doesn't accept UTF-8 encoded json strings although json is specified for UTF-8 to my knowledge. It certainly is accepted by and retrieved ok in file_get_contents("php://input"). Is there any reason for that?

  • 写回答

1条回答 默认 最新

  • dpqy77560 2019-04-30 03:34
    关注

    "Malformed UTF-8 characters" means the input data contains invalid characters.

    If the data is hard coded, save your file with UTF-8 (no BOM) encode.

    If not, use iconv to convert or check the input data.

    Encrypted data is in binary format, you may need do base64 encode before run json encode

    $jsontest->data1 = base64_encode($encrypted);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染