In the question that you referenced, the accepted answer explains how to derive the iv (and key) from the the password - not how to derive the iv from the ciphertext produced by openssl enc
.
The reason for this, and for the behavior that you observed, is that openssl enc
does not store the iv with the ciphertext. openssl enc
derives the iv from the password, unless the iv is specified using the -iv
option.
The first and second commands that you posted function as expected, because you did not specify the -iv
options in these commands. So openssl enc
derived the iv from the password for both of these commands. The iv's for both commands were the same, because the passwords provided in both commands were the same.
In the third command, you specified the -iv
option. But, in the fourth command, you did not specify the iv
option. Therefore, for the fourth command, openssl enc
derived the iv from the password, and of course this iv was different than the one that you specified in the third command. For the fourth command to function correctly, you would need to specify the same iv that you used in the third command, using the -iv
option.
For more information, see https://wiki.openssl.org/index.php/Enc
Also, the default key derivation method used by openssl enc
is very weak, as noted in the question that you referenced. For a stronger key derivation function, use the -pbkdf2
option. See https://crypto.stackexchange.com/questions/3298/is-there-a-standard-for-openssl-interoperable-aes-encryption for how the salt is stored, and the key and iv are derived from the password when using the -pbkdf2
option.