我在下面找到了一些脚本并对其进行了修改,检查现在 PKCS#5 填充是否完成。
<?php
function printStringToHex($text)
{
$size = strlen($text);
for($i = 0; $i < $size; $i++)
{
echo dechex(ord($text[$i])) . " ";
}
}
function encrypt($input)
{
echo "<PRE>*** Encrypt *** </PRE>";
echo "<PRE>Raw input: " . $input . "</PRE>";
$size = mcrypt_get_block_size('des', 'cbc');
echo "<PRE>Block: " . $size . "</PRE>";
$input = pkcs5_pad($input, $size);
echo "<PRE>PKCS#5 padding: ";
echo printStringToHex($input);
echo "</PRE>";
$td = mcrypt_module_open('des', '', 'cbc', '');
$iv = pack('H*','insert hex iv here');
$key = pack('H*','insert hex key here');
mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
echo "<PRE>Raw output: " . $data . "</PRE>";
echo "<PRE>Hex output: ";
echo printStringToHex($data);
echo "</PRE>";
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
$data = base64_encode($data);
echo "<PRE>B64 output: " . $data . "</PRE>";
echo "<PRE>B64 output len: ";
echo strlen($data) . "</PRE>";
return $data;
}
function pkcs5_pad ($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
$enc = encrypt("insert plaintext message here");