如何用python加密文件,但可以使用shell解密? [英] How to encrypt a file with python but can be decrypted using the shell?

查看:417
本文介绍了如何用python加密文件,但可以使用shell解密?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要加密文件,发送给另一个人,然后他只能使用shell解密。

I need to encrypt a file, send it to another person, who then can only decrypt it using shell.

我通常使用 openssl 命令: openssl enc -aes-256-cbc -salt -in [filename] -out [file out name] -pass file:[direct path to key文件] ,然后发送文件。

I usually encrypt the file with the openssl command: openssl enc -aes-256-cbc -salt -in [filename] -out [file out name] -pass file:[direct path to key file], then send the file.

然后,另一个人将使用 openssl 命令: openssl enc -d -aes-256-cbc -in [加密文件] -out [文件输出名] -pass文件:[到密钥文件的直接路径]

The other person then would decrypt the file again with the openssl command: openssl enc -d -aes-256-cbc -in [encrypted file] -out [file out name] -pass file:[direct path to key file]

我会使用 os.system 来做到这一点,

I would use os.system to do this, but I feel like there has to be another way to encrypt the file with python which then could be decrypted on the shell side.

推荐答案

您需要使用 openssl

我使用命令行GnuPG,有非常好的Python库: python-gnupg 。它是一个命令行 gpg 的包装,所以它们的工作原理是一样的。

I use command line GnuPG and there is very nice Python library: python-gnupg . It is a wrapper over command line gpg so they work simply the same.

它包含密码)可以使用非对称加密。为每个部分创建私钥/公钥对,然后使用收件人公钥加密邮件,并使用发件人私钥sigg。收件人将使用发件人公钥检查发件人的签名,收件人将使用她的私钥解密邮件。私钥可以通过密码保护,但如果您确定您的环境安全,您可以使用空密码。

Instead of key file (I think it contains password) you can use asymmetric cryptography. Create private/public pairs of keys for each part and then encrypt message using recipient public key and sigg it using sender private key. Recipient will check signature of sender using sender public key and recipient will decrypt message using her private key. Private keys can be protected by password but if you are sure your environments are safe you can use empty passwords.

这篇关于如何用python加密文件,但可以使用shell解密?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆