The openssl enc
command by default uses a randomly generated salt value when encrypting. It starts the output with that value, preceded by a "magic number" that represents the characters Salted__
. Taking the enc
version of your command, you can see that illustrated here:
$ printf 1234567890123456 | openssl enc -aes-256-cbc -iv 0123456789012345 -k 01234567890123456789012345678901 | hexdump -C
00000000 53 61 6c 74 65 64 5f 5f ed 6c b5 aa e2 92 4a 00 |Salted__.l....J.|
00000010 68 f7 b2 40 d2 44 44 8b fa 4c 05 95 99 cf 76 32 |[email protected]|
00000020 4e 15 37 65 93 00 d6 b2 ff 4d 1b 6c af 46 64 f6 |N.7e.....M.l.Fd.|
00000030
When decrypting, this same magic number is expected. Your file does not contain it, hence the error. This is an OpenSSL proprietary thing.
It is possible (but not recommended) to avoid the salting, using the -nosalt
option:
$ printf 1234567890123456 | openssl enc -aes-256-cbc -iv 0123456789012345 -k 01234567890123456789012345678901 -nosalt | hexdump -C
00000000 54 05 6a fb c3 60 a9 32 3d 2e e0 4c 2a 21 4a a1 |T.j..`.2=..L*!J.|
00000010 3c a7 34 f3 8f c4 15 33 99 dd 08 f7 e5 ef ea 57 |<.4....3.......W|
00000020
Using that same -nosalt
option on your decryption command, you should no longer get that bad magic number
. You may still run into other issues, depending on what kind of padding (if any) you used when encrypting your data, but you did not give enough information to draw any conclusions on that.
Like mentioned in the comments, there is a difference between -k
lowercase and -K
uppercase. It is relevant for your situation and you are probably using the wrong one. Check out the openssl enc
man page for more info, and to read more about the -nosalt
option as well.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…