如何使用OpenSSL加密/解密文件?

aF.*_*aF. 185 encryption openssl

我想使用一个密码加密和解密一个文件.

我如何使用OpenSSL来做到这一点?

Szo*_*ske 246

这是来自谷歌的问题的最佳答案:http: //tombuntu.com/index.php/2007/12/12/simple-file-encryption-with-openssl/

加密:

openssl aes-256-cbc -a -salt -in secrets.txt -out secrets.txt.enc
Run Code Online (Sandbox Code Playgroud)

解密:

openssl aes-256-cbc -d -a -in secrets.txt.enc -out secrets.txt.new
Run Code Online (Sandbox Code Playgroud)

但这根本没有使用公钥基础设施,所以有点像用螺丝刀锤击钉子:-)

  • 答案可能不是最佳的(截至本文撰写时)取决于OP的用例.具体而言,参数"-a"可能不是最佳的,答案并未解释其用途."-a"通常在加密输出以ASCII /文本形式传输时使用,并且与二进制形式相比具有增加输出大小的效果.原始海报没有指定输出格式,因此我觉得至少应该提到这一点.请参阅答案:http://stackoverflow.com/a/31552829/952234,其中还包含有关为什么应该使用gpg而不是openssl执行此任务的说明. (15认同)
  • 不要使用上面的命令,因为没有密钥派生.在这里阅读更多内容:[openssl弱密钥派生](https://www.reddit.com/r/crypto/comments/4gu3df/weak_key_derivation_in_openssl/) (4认同)
  • @jonasl根据最新的手册页指出:“在Openssl 1.1.0中,默认摘要已从MD5更改为SHA256。”来源:https://github.com/openssl/openssl/blob/master/doc/man1 /enc.pod (2认同)
  • 如果您打算在另一台机器上使用此文件,则可以在@Kebman的注释中添加“ -md sha256”到您的编码和解码命令中。这样可以避免OpenSSL版本不兼容/差异 (2认同)

moo*_*moo 149

简答:

您可能希望使用gpg而不是在此答案的末尾openssl看到"附加说明".但要回答这个问题openssl:

要加密:

openssl enc -aes-256-cbc -in un_encrypted.data -out encrypted.data
Run Code Online (Sandbox Code Playgroud)

要解密:

openssl enc -d -aes-256-cbc -in encrypted.data -out un_encrypted.data
Run Code Online (Sandbox Code Playgroud)

注意:加密或解密时将提示您输入密码.


答案很长:

您最好的信息来源openssl enc可能是:https://www.openssl.org/docs/apps/enc.html

命令行: openssl enc采用以下形式:

openssl enc -ciphername [-in filename] [-out filename] [-pass arg]
[-e] [-d] [-a/-base64] [-A] [-k password] [-kfile filename] 
[-K key] [-iv IV] [-S salt] [-salt] [-nosalt] [-z] [-md] [-p] [-P] 
[-bufsize number] [-nopad] [-debug] [-none] [-engine id]
Run Code Online (Sandbox Code Playgroud)

关于您的问题的最有用参数的说明:

-e
    Encrypt the input data: this is the default.

-d    
    Decrypt the input data.

-k <password>
    Only use this if you want to pass the password as an argument. 
    Usually you can leave this out and you will be prompted for a 
    password. The password is used to derive the actual key which 
    is used to encrypt your data. Using this parameter is typically
    not considered secure because your password appears in 
    plain-text on the command line and will likely be recorded in 
    bash history.

-kfile <filename>
    Read the password from the first line of <filename> instead of
    from the command line as above.

-a
    base64 process the data. This means that if encryption is taking 
    place the data is base64 encoded after encryption. If decryption 
    is set then the input data is base64 decoded before being 
    decrypted.
    You likely DON'T need to use this. This will likely increase the
    file size for non-text data. Only use this if you need to send 
    data in the form of text format via email etc.

-salt
    To use a salt (randomly generated) when encrypting. You always
    want to use a salt while encrypting. This parameter is actually
    redundant because a salt is used whether you use this or not 
    which is why it was not used in the "Short Answer" above!

-K key    
    The actual key to use: this must be represented as a string
    comprised only of hex digits. If only the key is specified, the
    IV must additionally be specified using the -iv option. When 
    both a key and a password are specified, the key given with the
    -K option will be used and the IV generated from the password 
    will be taken. It probably does not make much sense to specify 
    both key and password.

-iv IV
    The actual IV to use: this must be represented as a string 
    comprised only of hex digits. When only the key is specified 
    using the -K option, the IV must explicitly be defined. When a
    password is being specified using one of the other options, the 
    IV is generated from this password.
Run Code Online (Sandbox Code Playgroud)

补充说明:

虽然您已经特别询问过OpenSSL,但您可能需要考虑使用GPG代替基于本文的加密目的OpenSSL与GPG加密异地备份?

要使用GPG执行相同操作,您可以使用以下命令:

要加密:

gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data
Run Code Online (Sandbox Code Playgroud)

要解密:

gpg --output un_encrypted.data --decrypt encrypted.data
Run Code Online (Sandbox Code Playgroud)

注意:加密或解密时将提示您输入密码.

  • 关于优先选择GPG而不是OpenSSL的好评.我发现OpenSSL使用这样一个弱密码派生哈希来获取密钥令人难以置信! (7认同)
  • “加密或解密时,系统会提示您输入密码。” `gpg` 让我可以解密文件而不提示输入密码。看起来密码会存储一段时间,这是我不想要的。 (6认同)
  • 一定要使用“-md md5”选项来兼容在没有指定-md选项的旧openssl上加密的文件,否则你会发现文件不会在新系统上解密:https://github.com/ libressl-便携式/便携式/问题/378 (4认同)
  • 默认值在 openssl 版本之间发生变化。1.0.x 对 -md 选项使用默认值 md5。1.1.x 版使用 sha256。如果您解密并得到“:数字信封例程:EVP_DecryptFinal_ex:解密错误”错误。尝试指定“-md md5”或“-md sha256”。 (2认同)
  • @user76284这可能会解决您的问题:https://unix.stackexchange.com/a/395876/79875。我认为您正在运行“gpg-agent” (2认同)
  • @moo 似乎选项 `--no-symkey-cache` 在使用 gpg 和 `--metry` 时会禁用缓存,即使代理正在运行也是如此。 (2认同)
  • “--no-symkey-cache”标志的位置很重要:“$ gpg --output crypto.data --symmetry --no-symkey-cache --cipher-algo AES256 un_encrypted.data”。如果添加到该行的末尾,您将收到“gpg:注意:‘--no-symkey-cache’不被视为一个选项”。它是在2.2.7版本中首次添加的。 (2认同)

小智 31

加密:

openssl enc -in infile.txt -out encrypted.dat -e -aes256 -k symmetrickey
Run Code Online (Sandbox Code Playgroud)

解密:

openssl enc -in encrypted.dat -out outfile.txt -d -aes256 -k symmetrickey
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅openssl(1)文档.

  • 要使用明文密码,请将`-k symmetrickey`替换为`-pass stdin`或`-pass'pass:PASSWORD'` (10认同)
  • 与@ jonasl的评论相关,请注意`-k symmetrickey`具有误导性.`-k`选项用于指定密码,OpenSSL从该密码派生对称密钥.如果要指定对称密钥,则必须使用`-K`选项. (4认同)
  • 不要使用上面的命令,因为没有密钥派生.在这里阅读更多内容:[openssl弱密钥派生](https://www.reddit.com/r/crypto/comments/4gu3df/weak_key_derivation_in_openssl/) (3认同)

Arn*_*liu 21

不要使用 OPENSSL 默认密钥派生。

目前接受的答案使用它,不再推荐和安全。

攻击者简单地暴力破解密钥是非常可行的。

https://www.ietf.org/rfc/rfc2898.txt

PBKDF1 应用散列函数(应为 MD2 [6]、MD5 [19] 或 SHA-1 [18])来派生密钥。派生密钥的长度受散列函数输出长度的限制,MD2 和 MD5 为 16 个八位字节,SHA-1 为 20 个八位字节。PBKDF1 与 PKCS #5 v1.5 中的密钥派生过程兼容。推荐 PBKDF1 仅用于与现有应用程序兼容,因为它生成的密钥对于某些应用程序来说可能不够大。

PBKDF2 应用伪随机函数(示例见附录 B.1)来导出密钥。派生密钥的长度基本上是无界的。(但是,导出密钥的最大有效搜索空间可能受到底层伪随机函数结构的限制。有关进一步讨论,请参见附录 B.1。)建议新应用使用 PBKDF2。

做这个:

openssl enc -aes-256-cbc -pbkdf2 -iter 20000 -in hello -out hello.enc -k meow

openssl enc -d -aes-256-cbc -pbkdf2 -iter 20000 -in hello.enc -out hello.out

注意:解密迭代必须与加密迭代相同。

迭代次数必须至少为 10000。这是关于迭代次数的一个很好的答案:https : //security.stackexchange.com/a/3993

另外……我们这里有足够多的人推荐 GPG。阅读该死的问题。


nne*_*eko 8

使用随机生成的公钥进行更新。

加密:

openssl enc -aes-256-cbc -a -salt -in {raw data} -out {encrypted data} -pass file:{random key}
Run Code Online (Sandbox Code Playgroud)

解密:

openssl enc -d -aes-256-cbc -in {ciphered data} -out {raw data}
Run Code Online (Sandbox Code Playgroud)


mti*_*935 8

如其他答案中所述,以前版本的 openssl 使用弱密钥派生函数从密码派生 AES 加密密钥。但是,openssl v1.1.1 支持更强的密钥派生功能,其中密钥是从密码中派生的,使用pbkdf2随机生成的盐,以及多次迭代的 sha256 哈希(默认为 10,000)。

要加密文件:

openssl aes-256-cbc -e -salt -pbkdf2 -iter 10000 -in plaintextfilename -out encryptedfilename
Run Code Online (Sandbox Code Playgroud)

解密文件:

openssl aes-256-cbc -d -salt -pbkdf2 -iter 10000 -in encryptedfilename -out plaintextfilename
Run Code Online (Sandbox Code Playgroud)


小智 6

加密:

$ openssl bf < arquivo.txt > arquivo.txt.bf
Run Code Online (Sandbox Code Playgroud)

解密:

$ openssl bf -d < arquivo.txt.bf > arquivo.txt
Run Code Online (Sandbox Code Playgroud)

bf === CBC 模式下的河豚