Portál AbcLinuxu, 10. května 2025 04:39
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_CBC, '');
$iv_size = mcrypt_enc_get_iv_size($cipher);
printf("iv_size = %d\n",$iv_size);
$key256 = '12345678901234561234567890123456';
$key128 = '1234567890123456';
$iv = '1234567890123456';
printf("iv: %s\n",bin2hex($iv));
printf("key256: %s\n",bin2hex($key256));
printf("key128: %s\n",bin2hex($key128));
// This is the plain-text to be encrypted:
$cleartext = "The quick brown fox jumped over the lazy dog +ľščťžýáíé:,-ôlúä´=;°°%ˇ(/!\")_:?";
printf("plainText: %s\n\n",$cleartext);
if (mcrypt_generic_init($cipher, $key256, $iv) != -1)
{
// PHP pads with NULL bytes if $cleartext is not a multiple of the block size..
$cipherText = mcrypt_generic($cipher,$cleartext );
//mcrypt_generic_deinit($cipher);
// Display the result in hex.
printf("256-bit encrypted result:\n%s\n\n",bin2hex($cipherText));
}
$cleartext2="2fddc3abec692e1572d9b7d629172a05caf230bc7c8fd2d26ccfd65f9c545269e2eb156091fd107608ae3cede2de06681f9fbcb8135431f960623f036b276f87be88604a9805bb397e72ec9a508174081a6b1291aa609d6a9529324b52ad2b47";
$cipherText2 = mdecrypt_generic($cipher,$cleartext2 );
mcrypt_generic_deinit($cipher);
// Display the result in hex.
echo "256-bit DECRYPTED result:".$cipherText2;
Řešení dotazu:
function hex2bin($h) //tuto funkciu treba, tato nie je nativna v php. ale kupodivu bin2hex je.
{
if (!is_string($h)) return null;
$r='';
for ($a=0; $a< strlen ($h); $a+=2) { $r.=chr(hexdec($h{$a}.$h{($a+1)})); }
return $r;
}
$cleartext2=hex2bin($cleartext2);
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.