LoginSignup
0
1

More than 3 years have passed since last update.

opensslで暗号化したファイルをpythonからopensslで復号

Posted at

以前、pythonのライブラリを使って暗号化・復号化する記事を書いたりもしましたが、そんな事をしなくても大抵の場合、opensslをコマンドから実行すれば十分ということに気が付きました。:sweat_smile:

動作確認環境
arch linux
openssl 1.1.1
python3.8.1

$ openssl enc -e -aes-256-cbc -k 'password' -in original_file -out encrypted_file

openssl 1.1.1だと"Using -iter or -pbkdf2 would be better."という警告がでますが、このオプションをつけると古いバージョンで復号化できなくなるので注意。

decrypt.py
from subprocess import run, PIPE
password = 'password'
file_path = 'encrypted_file'

completed = run(args=[
    'openssl', 'enc', '-d', '-aes-256-cbc',
    '-k', password, '-in', file_path],
    check=True, stdout=PIPE)
print(completed.stdout.decode())
0
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1