如果不是块大小的倍数,则零填充输入数据。如果数据本身具有尾随零,则会导致结果不明确。显然,在这种情况下,OpenSSL不允许您使用零填充,这说明了错误的返回值。
您可以通过手动添加填充来避免这种情况。
$message = "Lorem ipsum";$key = "123456789012345678901234";$iv = "12345678";$message_padded = $message;if (strlen($message_padded) % 8) { $message_padded = str_pad($message_padded, strlen($message_padded) + 8 - strlen($message_padded) % 8, "");}$encrypted_mcrypt = mcrypt_encrypt(MCRYPT_3DES, $key, $message, MCRYPT_MODE_CBC, $iv);$encrypted_openssl = openssl_encrypt($message_padded, "DES-EDE3-CBC", $key, OPENSSL_RAW_DATA | OPENSSL_NO_PADDING, $iv);printf("%s => %sn", bin2hex($message), bin2hex($encrypted_mcrypt));printf("%s => %sn", bin2hex($message_padded), bin2hex($encrypted_openssl));
这将打印相等。
4c6f72656d20697073756d => c6fed0af15d494e485af3597ad628cec4c6f72656d20697073756d0000000000 => c6fed0af15d494e485af3597ad628cec
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)