【问题标题】:How to get data after decoding from its encoded value?如何从其编码值解码后获取数据?
【发布时间】:2016-12-10 09:43:26
【问题描述】:

我正在测试如何使用 PHP 编码函数从编码数据中获取实际数据。编码后我无法获得原始数据。相反,我得到了一些特殊的 Unicode 字符...

我的代码如下。

$key = '28e336ac6c9423d946ba02d19c6a2632'; // Randomly generated key
$request_params = array(
    'controller' => 'mylist',
    'action'     => 'read',
    'username'   => 'test',
    'password'   => '12345'
));
$enc_request = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, json_encode($request_params), MCRYPT_MODE_ECB));
//echo $enc_request;exit; // Here I am getting the encoded string.

$paramas = base64_decode(trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, json_decode($enc_request), MCRYPT_MODE_ECB)));
print_r($paramas); // Here I am getting like ... ºÇ
echo $paramas->controller; // Got nothing.

我做错了什么?

【问题讨论】:

  • 解析错误:语法错误,第 8 行 tst.php 中出现意外的 ')'
  • 修复:然后:Notice: Trying to get property of non-object in tst.php on line 14
  • 当我只尝试 var_dump($paramas);它让我... string(3) "ºÇ"...

标签: php json base64 mcrypt rijndael


【解决方案1】:

我认为问题在于您执行的操作顺序。如果您仔细查看您的代码,您首先是 JSON 编码,然后是加密,最后是 Base64 编码。因此,要取回原始值,您需要以相反的顺序进行操作。首先是 Base64 解码,然后是解密,最后是 JSON 解码。尝试类似

$paramas = json_decode(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($enc_request), MCRYPT_MODE_ECB));

ECB 模式也只能用于测试。如果您要使用它,请选择 CBC。

此外,mcrypt 已被贬低。您应该改为查看 openssl_ecrypt/openssl_decrypt。我没有安装 mcrypt,但这可以使用 OpenSSL:

$key = '28e336ac6c9423d946ba02d19c6a2632'; // Randomly generated key
$request_params = array(
    'controller' => 'mylist',
    'action'     => 'read',
    'username'   => 'test',
    'password'   => '12345'
);
$enc_request = base64_encode(openssl_encrypt(json_encode($request_params), 'AES-256-ECB', $key));
//echo $enc_request;exit; // Here I am getting the encoded string.

$paramas = json_decode(openssl_decrypt(base64_decode($enc_request), 'AES-256-ECB', $key));
print_r($paramas); // Here I am getting like ... ºÇ
echo $paramas->controller;

【讨论】:

  • 更正顺序是错误的,但我正在测试我认为正确的顺序,即使那时我也收到 json_decode 错误Control character error, possibly incorrectly encoded
  • 尝试回显json_decode之前的文本,应该是可读的字符串
【解决方案2】:

当你以正确的顺序做事时,它会起作用。

这段代码我已经测试过了,它确实有效

<?php

$key = '28e336ac6c9423d946ba02d19c6a2632';//randomly generated key
$request_params = array(
    'controller' => 'mylist',
    'action'     => 'read',
    'username'   => 'test',
    'password'   => '12345'
);
$js = json_encode($request_params);
$encd = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $js, MCRYPT_MODE_ECB);

$enc_request = base64_encode($encd);
echo $enc_request . PHP_EOL;

// now reverse process in correct order
$one   = base64_decode($enc_request);
$two   = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $one, MCRYPT_MODE_ECB);
$twoa  = trim($two);
echo $twoa . PHP_EOL;

$three = json_decode($twoa);

print_r($three);
echo $three->controller . PHP_EOL;

它也适用于@rypskar 建议的openssl 函数

<?php

$key = '28e336ac6c9423d946ba02d19c6a2632';//randomly generated key
$request_params = array(
    'controller' => 'mylist',
    'action'     => 'read',
    'username'   => 'test',
    'password'   => '12345'
);
$js = json_encode($request_params);
//$encd = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $js, MCRYPT_MODE_CBC);
$encd = openssl_encrypt($js, 'AES-256-ECB', $key);

$enc_request = base64_encode($encd);
echo $enc_request . PHP_EOL;

// now reverse process in correct order
$one   = base64_decode($enc_request);
//$two   = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $one, MCRYPT_MODE_CBC);
$two   = openssl_decrypt($one, 'AES-256-ECB', $key);
$twoa  = trim($two);
echo $twoa . PHP_EOL;
$three = json_decode($twoa);

print_r($three);
echo $three->controller . PHP_EOL;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 2017-10-02
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    相关资源
    最近更新 更多