たとえば、
openssl aes-256-cbc -e -pass pass:xxxxx -in test.txt -out test.txt.aes256
とした場合、復号するときは
openssl aes-256-cbc -d -pass pass:xxxxx -in test.txt.aes256 -out test.txt
とするわけですが、
bad decrypt
139859917407904:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:539:
とか
bad decrypt
80CB49729C7F0000:error:1C800064:Provider routines:ossl_cipher_unpadblock:bad decrypt:providers/implementations/ciphers/ciphercommon_block.c:124:
とかいうエラーが出ることがあります。
これは、おそらく、暗号化で使用した OpenSSL と、復号で使用した OpenSSL のバージョンが違うからで、上のエラーは新しめの OpenSSL で暗号化して古めの OpenSSL で復号したとき、下のエラーは古めの OpenSSL で暗号化して新しめの OpenSSL で復号したときに出ます。
問題の原因は、-md
というオプションのデフォルト値が新旧で違うからで、新しいのは sha-256
、古いのは md5
がデフォルト値のようです。
ちなみに、古い OpenSSL には -md sha-256
というオプションはないかもしれないですが、
openssl aes-256-cbc -d -md sha256 -pass pass:xxxxx -in test.txt.aes256 -out test.txt
のように -md sha256
とすれば、新しい OpenSSL で暗号化したファイルも復号できるかもしれないです。
逆に新しい OpenSSL で古い OpenSSL で暗号化したファイルが復号できないときはやはり
openssl aes-256-cbc -d -md md5 -pass pass:xxxxx -in test.txt.aes256 -out test.txt
のように -md md5
とすれば復号できるかもしれません。
あと、-md md5
は脆弱性があるとされてるので、ほんとはあまり使わない方が良いようです。
それで、古い OpenSSL でも、暗号化するときに
openssl aes-256-cbc -e -md sha256 -pass pass:xxxxx -in test.txt -out test.txt.aes256
と -md sha256
をつけておくのがいいかもしれません。
以上