3des 암호화/해독을 구현 한 PHP 양식이 있습니다. 프런트 엔드에서 데이터베이스에 정보를 제출하고 삽입 한 후 백엔드의 정보를 읽고 싶습니다.PHP 3DES 해독이 이상한 (나쁜) 문자를 보임
암호 해독 후 내 양식의 정보가 올바른 문자 대신 단지 부적절한 문자를 반환합니다 (예 : �СU�k�8��t�ó�2(�f�B
). 이 문제를 어떻게 해결할 수 있습니까?
<?php
function encrypt($clear, $key , $base64 = true)
{
if (!$clear)
return '';
$clear = pack("a*H2", $clear, "80");
if (function_exists('mcrypt_module_open') &&
($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_CBC, "")))
{
$iv = create_iv(mcrypt_enc_get_iv_size($td));
mcrypt_generic_init($td, $key, $iv);
$cipher = $iv . mcrypt_generic($td, $clear);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
else {
@include_once('des.inc');
if (function_exists('des')) {
$des_iv_size = 8;
$iv = create_iv($des_iv_size);
$cipher = $iv . des($key, $clear, 1, 1, $iv);
}
else {
raise_error(array(
'code' => 500, 'type' => 'php',
'file' => __FILE__, 'line' => __LINE__,
'message' => "Could not perform encryption; make sure Mcrypt is installed or lib/des.inc is available"
), true, true);
}
}
return $base64 ? base64_encode($cipher) : $cipher;
}
function decrypt($cipher, $key , $base64 = true)
{
if (!$cipher)
return '';
$cipher = $base64 ? base64_decode($cipher) : $cipher;
if (function_exists('mcrypt_module_open') &&
($td = mcrypt_module_open(MCRYPT_TripleDES, "", MCRYPT_MODE_CBC, "")))
{
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = substr($cipher, 0, $iv_size);
// session corruption? (#1485970)
if (strlen($iv) < $iv_size)
return '';
$cipher = substr($cipher, $iv_size);
mcrypt_generic_init($td, $key, $iv);
$clear = mdecrypt_generic($td, $cipher);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
}
else {
@include_once('lib/des.inc');
if (function_exists('des')) {
$des_iv_size = 8;
$iv = substr($cipher, 0, $des_iv_size);
$cipher = substr($cipher, $des_iv_size);
$clear = des($this->config->get_crypto_key($key), $cipher, 0, 1, $iv);
}
else {
raise_error(array(
'code' => 500, 'type' => 'php',
'file' => __FILE__, 'line' => __LINE__,
'message' => "Could not perform decryption; make sure Mcrypt is installed or lib/des.inc is available"
), true, true);
}
}
/*-
* Trim PHP's padding and the canary byte; see note in
* rcmail::encrypt() and http://php.net/mcrypt_generic#68082
*/
$clear = substr(rtrim($clear, "\0"), 0, -1);
return $clear;
}
function create_iv($size)
{
$iv = '';
for ($i = 0; $i < $size; $i++)
$iv .= chr(mt_rand(0, 255));
return $iv;
}
**echo decrypt('ijvdfvf1==+Ac44tgLHdAL+w7O2MiiIZplC','s&gs1?m5l8PQIOGckvxDT4kR',true);**
?>
내가 암호화하고 내 양식에 UTF-8 문자를 해독 할 :
여기 내 소스 코드입니다.
필자는 PHP를 간신히 알고 있지만 decryptor에서 초기'pack' 연산을 취소하지 않는 것으로 보입니다. 되돌아 오기 전 마지막 단계로'unpack'을 부를 수도 있습니다. –
@GregS, 나는 당신의 코멘트를 이해할 수 없었다 :(어디에서 풀어야하는가? ?????? – brelian
''encrypt '의 첫 번째 단계로''pack'을했기 때문에''decrypt'의 마지막 단계로''unpack'해야합니다. –