如何从其编码值解码后获取数据?
How to get data after decoding from its encoded value?
我正在测试如何使用 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.
我做错了什么?
我认为问题在于您执行的操作顺序。如果您仔细查看您的代码,您首先是 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;
当你按照正确的顺序做事时,它就会起作用。
这段代码我已经测试过,确实有效
<?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;
我正在测试如何使用 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.
我做错了什么?
我认为问题在于您执行的操作顺序。如果您仔细查看您的代码,您首先是 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;
当你按照正确的顺序做事时,它就会起作用。
这段代码我已经测试过,确实有效
<?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;