PHP에서 AES256 암호 해독을 수행하는 방법은 무엇입니까?
문제
암호화 된 텍스트를 암호화 해야하는 텍스트가 있습니다. AES-256-CBC로 암호화되어 있습니다. 암호화 된 텍스트, 키 및 iv가 있습니다. 그러나 내가 무엇을 시도하든 나는 그것을 작동시키는 것 같지 않습니다.
인터넷은 McRypt의 Rijndael Cypher 가이 작업을 수행 할 수 있어야한다고 제안했습니다.
function decrypt_data($data, $iv, $key) {
$cypher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
// initialize encryption handle
if (mcrypt_generic_init($cypher, $key, $iv) != -1) {
// decrypt
$decrypted = mdecrypt_generic($cypher, $data);
// clean up
mcrypt_generic_deinit($cypher);
mcrypt_module_close($cypher);
return $decrypted;
}
return false;
}
지금은 2 개의 경고를 받고 출력은 gibberish입니다.
Warning: mcrypt_generic_init() [function.mcrypt-generic-init]: Key size too large; supplied length: 64, max: 32 in /var/www/includes/function.decrypt_data.php on line 8
Warning: mcrypt_generic_init() [function.mcrypt-generic-init]: Iv size incorrect; supplied length: 32, needed: 16 in /var/www/includes/function.decrypt_data.php on line 8
모든 도움이 감사하겠습니다.
해결책
나는이 물건에 대해 크게 익숙하지 않지만 시도하는 것 같아요 MCRYPT_RIJNDAEL_256
대신에 MCRYPT_RIJNDAEL_128
분명한 다음 단계가 될 것입니다 ...
편집하다: 당신 말이 맞아요 - 이것은 당신이 필요로하는 것이 아닙니다. MCRYPT_RIJNDAEL_128
실제로 올바른 선택입니다. 당신이 제공 한 링크에 따르면, 당신의 키와 IV는 다음과 같은 두 배나 길다.
// How do you do 256-bit AES encryption in PHP vs. 128-bit AES encryption???
// The answer is: Give it a key that's 32 bytes long as opposed to 16 bytes long.
// For example:
$key256 = '12345678901234561234567890123456';
$key128 = '1234567890123456';
// Here's our 128-bit IV which is used for both 256-bit and 128-bit keys.
$iv = '1234567890123456';
다른 팁
한 예제를 보내주세요. 코드를 확인하십시오.
$data_to_encrypt = "2~1~000024~0910~20130723092446~T~00002000~USD~F~375019001012120~0~0~00000000000~";
$key128 = "abcdef0123456789abcdef0123456789";
$iv = "0000000000000000";
$cc = $data_to_encrypt;
$key = $key128;
$iv = $iv;
$length = strlen($cc);
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128,'','cbc','');
mcrypt_generic_init($cipher, $key, $iv);
$encrypted = base64_encode(mcrypt_generic($cipher,$cc));
mcrypt_generic_deinit($cipher);
mcrypt_generic_init($cipher, $key, $iv);
$decrypted = mdecrypt_generic($cipher,base64_decode($encrypted));
mcrypt_generic_deinit($cipher);
echo "encrypted: " . $encrypted;
echo "<br/>";
echo "length:".strlen($encrypted);
echo "<br/>";
echo "decrypted: " . substr($decrypted, 0, $length);
제휴하지 않습니다 StackOverflow