I tried to encrypt file using this command:
openssl enc -aes-256-cbc -in $infile -out $outfile -pass file:$keyfile
For the same input file, Why the output files has different hashes.
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.Sign up to join this community
CBC requires an IV: the very first block of data should be random. It is sent with the data so that the recipient has what they need to decrypt the message. The IV for CBC should be uniformly random and unpredictable. The
openssl enc command creates an IV with 8 fixed bytes and 8 random bytes (it should be 16 random bytes). When you encrypt the same message twice, the IV changes. (There's an option
-nosalt to use a fixed IV, which is even worse for security and shouldn't be done unless the password was itself generated randomly for a single message.)
All non-broken modes of encryption have an IV or equivalent, so that encrypting the same message twice results in different ciphertexts. This is necessary, at least, so that an adversary cannot detect whether two encrypted messages have the same plaintext. Repeating the IV for distinct messages can open other attacks; for example, with CBC, a common prefix to two messages is visible in the ciphertext if the IV is reused. If you ever find that encrypting the same message twice leads to the same ciphertext, something is broken.
Don't use the
openssl command for serious work. It's meant only as a showcase of the library functions, and it's badly designed even for that. As we've seen, the
enc command doesn't quite do things securely since its IV isn't as random as it should. A worse problem is the way the key is derived from the password:
openssl enc uses a single round of MD5, which is bad — deriving a key from a password should use an intrinsically slow function (a key stretching function) to slow down brute force cracking attempts where the attacker tries likely passwords (dictionary words, 1337 variations, etc.).
gpg -c filename
You'll get prompted for the passphrase. The encrypted file will be stored in
filename.gpg. To decrypt, run
gpg filename.gpg and enter the passphrase.
(I think you accidentally added the
-d decryption option in the command in your question.)
The encrypted files differ because of the random 64-bit cryptographic salt that OpenSSL generates and includes in each encrypted file by default. This can be disabled with the
-nosalt option, but as the documentation in OpenSSL's
man enc states, this is generally a very bad idea:
-saltoption should ALWAYS be used if the key is being derived from a password unless you want compatibility with previous versions of OpenSSL and SSLeay.
-saltoption it is possible to perform efficient dictionary attacks on the password and to attack stream cipher encrypted data. The reason for this is that without the salt the same password always generates the same encryption key. When the salt is being used the first eight bytes of the encrypted data are reserved for the salt: it is generated at random when encrypting a file and read from the encrypted file when it is decrypted.