Ответ 1
Фактически оба mcrypt_encrypt()
и mcrypt_decrypt()
а также другие функции расшифровки (например, mcrypt_generic()
или mdecrypt_generic()
) сделать введите параметр $data
в длину n * <<blocksize>>
. Характер заполнения - это символ NUL
(\x0
или \0
), тогда как <<blocksize>>
зависит от используемых шифров и используемых режимов блочного шифрования. Вы должны взглянуть на Блокировать режимы шифрования операции и Заполнение (криптография).
Ниже представлен вывод mcrypt_get_block_size()
для каждого из доступных шифров и режимов на моей машине. Очевидно, что функция не учитывает, что такие режимы, как CFB, OFB и CTR, не требуют каких-либо специальных мер для обработки сообщений, длины которых не являются кратными размеру блока, поскольку все они работают с помощью XORing открытого текста с выходом блока шифр (цитата из Википедии). CBC, который используется в вашем примере, всегда требует, чтобы последний блок был дополнен до шифрования.
cast-128
cbc: 8 bytes
cfb: 8 bytes
ctr: 8 bytes
ecb: 8 bytes
ncfb: 8 bytes
nofb: 8 bytes
ofb: 8 bytes
stream: not supported
gost
cbc: 8 bytes
cfb: 8 bytes
ctr: 8 bytes
ecb: 8 bytes
ncfb: 8 bytes
nofb: 8 bytes
ofb: 8 bytes
stream: not supported
rijndael-128
cbc: 16 bytes
cfb: 16 bytes
ctr: 16 bytes
ecb: 16 bytes
ncfb: 16 bytes
nofb: 16 bytes
ofb: 16 bytes
stream: not supported
twofish
cbc: 16 bytes
cfb: 16 bytes
ctr: 16 bytes
ecb: 16 bytes
ncfb: 16 bytes
nofb: 16 bytes
ofb: 16 bytes
stream: not supported
arcfour
cbc: not supported
cfb: not supported
ctr: not supported
ecb: not supported
ncfb: not supported
nofb: not supported
ofb: not supported
stream: 1 bytes
cast-256
cbc: 16 bytes
cfb: 16 bytes
ctr: 16 bytes
ecb: 16 bytes
ncfb: 16 bytes
nofb: 16 bytes
ofb: 16 bytes
stream: not supported
loki97
cbc: 16 bytes
cfb: 16 bytes
ctr: 16 bytes
ecb: 16 bytes
ncfb: 16 bytes
nofb: 16 bytes
ofb: 16 bytes
stream: not supported
rijndael-192
cbc: 24 bytes
cfb: 24 bytes
ctr: 24 bytes
ecb: 24 bytes
ncfb: 24 bytes
nofb: 24 bytes
ofb: 24 bytes
stream: not supported
saferplus
cbc: 16 bytes
cfb: 16 bytes
ctr: 16 bytes
ecb: 16 bytes
ncfb: 16 bytes
nofb: 16 bytes
ofb: 16 bytes
stream: not supported
wake
cbc: not supported
cfb: not supported
ctr: not supported
ecb: not supported
ncfb: not supported
nofb: not supported
ofb: not supported
stream: 1 bytes
blowfish-compat
cbc: 8 bytes
cfb: 8 bytes
ctr: 8 bytes
ecb: 8 bytes
ncfb: 8 bytes
nofb: 8 bytes
ofb: 8 bytes
stream: not supported
des
cbc: 8 bytes
cfb: 8 bytes
ctr: 8 bytes
ecb: 8 bytes
ncfb: 8 bytes
nofb: 8 bytes
ofb: 8 bytes
stream: not supported
rijndael-256
cbc: 32 bytes
cfb: 32 bytes
ctr: 32 bytes
ecb: 32 bytes
ncfb: 32 bytes
nofb: 32 bytes
ofb: 32 bytes
stream: not supported
serpent
cbc: 16 bytes
cfb: 16 bytes
ctr: 16 bytes
ecb: 16 bytes
ncfb: 16 bytes
nofb: 16 bytes
ofb: 16 bytes
stream: not supported
xtea
cbc: 8 bytes
cfb: 8 bytes
ctr: 8 bytes
ecb: 8 bytes
ncfb: 8 bytes
nofb: 8 bytes
ofb: 8 bytes
stream: not supported
blowfish
cbc: 8 bytes
cfb: 8 bytes
ctr: 8 bytes
ecb: 8 bytes
ncfb: 8 bytes
nofb: 8 bytes
ofb: 8 bytes
stream: not supported
enigma
cbc: not supported
cfb: not supported
ctr: not supported
ecb: not supported
ncfb: not supported
nofb: not supported
ofb: not supported
stream: 1 bytes
rc2
cbc: 8 bytes
cfb: 8 bytes
ctr: 8 bytes
ecb: 8 bytes
ncfb: 8 bytes
nofb: 8 bytes
ofb: 8 bytes
stream: not supported
tripledes
cbc: 8 bytes
cfb: 8 bytes
ctr: 8 bytes
ecb: 8 bytes
ncfb: 8 bytes
nofb: 8 bytes
ofb: 8 bytes
stream: not supported
Поэтому вы должны rtrim()
вывод функций дешифрования, чтобы получить исходную строку, если ваш шифр работает на блоках фиксированной длины:
$output = rtrim($decrypted, "\0");