2016-08-04 115 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;