2016-08-04 117 views
0

我正在做一个如何使用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. 

我在做什么错了?

+1

解析错误:语法错误,意外“)”在尖沙咀。 php 8行 – RiggsFolly

+1

修复:那么:'注意:尝试获取第14行tst.php中的非对象的属性' – RiggsFolly

+0

当我只尝试var_dump($ paramas);它得到我...字符串(3)“ºÇ”... –

回答

1

我认为问题出在您的操作顺序上。如果你仔细观察你的代码,你是第一个JSON编码,然后加密并且最后编码为Base64。因此,为了恢复原始值,您需要按照相反的顺序进行操作。先进行Base64解码,然后解密并上传JSON解码。试试类似

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

此外ECB模式只应用于测试。如果你打算使用这个,请去CBC。

此外,mcrypt的是depricated。你应该检查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; 
+0

纠正顺序是错误的,但我测试我相信是正确的顺序,我得到一个json_decode错误控制字符错误,甚至可能不正确编码甚至然后 – RiggsFolly

+0

尝试回显json_decode之前的文本,它应该是一个可读的字符串 – rypskar

+0

啊我只是忘了'trim()'现在它的工作 – RiggsFolly

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; 

它还与建议openssl功能通过@rypskar

<?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;