提问人:aF. 提问时间:4/17/2013 最后编辑:Matthias BraunaF. 更新时间:6/16/2023 访问量:769957
如何使用OpenSSL加密/解密文件?
How to use OpenSSL to encrypt/decrypt files?
答:
加密:
openssl aes-256-cbc -a -salt -pbkdf2 -in secrets.txt -out secrets.txt.enc
解密:
openssl aes-256-cbc -d -a -pbkdf2 -in secrets.txt.enc -out secrets.txt.new
评论
-md sha256
加密:
openssl enc -in infile.txt -out encrypted.dat -e -aes256 -k symmetrickey
解密:
openssl enc -in encrypted.dat -out outfile.txt -d -aes256 -k symmetrickey
有关详细信息,请参见 openssl(1)
文档。
评论
-k symmetrickey
-pass stdin
-pass 'pass:PASSWORD'
-k symmetrickey
-k
-K
使用随机生成的公钥进行更新。
Encypt:
openssl enc -aes-256-cbc -a -salt -in {raw data} -out {encrypted data} -pass file:{random key}
解密:
openssl enc -d -aes-256-cbc -in {ciphered data} -out {raw data}
评论
我在网上找到了一个开源程序,它使用 openssl 来加密和解密文件。它使用单个密码执行此操作。这个开源脚本的伟大之处在于它通过粉碎文件来删除原始未加密的文件。但危险的是,一旦原始未加密的文件消失了,您必须确保记住密码,否则它们就无法解密您的文件。
这里是github上的链接
https://github.com/EgbieAnderson1/linux_file_encryptor/blob/master/file_encrypt.py
评论
使用 GPG(OpenSSL 的更好替代品)
尽管您已经特别询问了 OpenSSL(有关 OpenSSL 解决方案,请参阅下面的“使用 OpenSSL”),但您可能要考虑使用 GPG 来代替加密,基于本文 OpenSSL 与 GPG 加密异地备份?
要使用 GPG 执行相同的操作,请使用以下命令:
要加密:
gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data
解密:
gpg --output un_encrypted.data --decrypt encrypted.data
注意:加密或解密时,系统会提示您输入密码。并使用标志表示没有缓存。--no-symkey-cache
使用 OpenSSL(简答题)
如上所述,您可能希望使用 gpg
而不是 openssl,但要使用 openssl
来回答问题:
要加密:
openssl enc -aes-256-cbc -in un_encrypted.data -out encrypted.data
解密:
openssl enc -d -aes-256-cbc -in encrypted.data -out un_encrypted.data
注意:加密或解密时,系统会提示您输入密码。
使用 OpenSSL(长答案)
您最好的信息来源可能是:https://www.openssl.org/docs/man1.1.1/man1/enc.htmlopenssl enc
命令行:采用以下形式: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]
关于您的问题的最有用参数的解释:
-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.
-md digest
Use the specified digest to create the key from the passphrase.
The default algorithm as of this writing is sha-256. But this
has changed over time. It was md5 in the past. So you might want
to specify this parameter every time to alleviate problems when
moving your encrypted data from one system to another or when
updating openssl to a newer version.
评论
gpg
--no-symkey-cache
--symmetric
要加密:
$ openssl bf < arquivo.txt > arquivo.txt.bf
解密:
$ openssl bf -d < arquivo.txt.bf > arquivo.txt
bf === CBC 模式下的河豚
评论
请注意,OpenSSL CLI 使用弱非标准算法将密码转换为密钥,安装 GPG 会导致将各种文件添加到主目录并运行 gpg-agent 后台进程。如果您希望使用现有工具获得最大的可移植性和控制,则可以使用 PHP 或 Python 访问较低级别的 API,并直接传入完整的 AES 密钥和 IV。
通过 Bash 调用的 PHP 示例:
IV='c2FtcGxlLWFlcy1pdjEyMw=='
KEY='Twsn8eh2w2HbVCF5zKArlY+Mv5ZwVyaGlk5QkeoSlmc='
INPUT=123456789023456
ENCRYPTED=$(php -r "print(openssl_encrypt('$INPUT','aes-256-ctr',base64_decode('$KEY'),OPENSSL_ZERO_PADDING,base64_decode('$IV')));")
echo '$ENCRYPTED='$ENCRYPTED
DECRYPTED=$(php -r "print(openssl_decrypt('$ENCRYPTED','aes-256-ctr',base64_decode('$KEY'),OPENSSL_ZERO_PADDING,base64_decode('$IV')));")
echo '$DECRYPTED='$DECRYPTED
这将输出:
$ENCRYPTED=nzRi252dayEsGXZOTPXW
$DECRYPTED=123456789023456
您还可以使用 PHP 的函数安全地将密码转换为密钥。openssl_pbkdf2
评论
不要使用 OPENSSL 默认密钥派生。
目前,公认的答案使用了它,不再推荐和安全。
对于攻击者来说,简单地暴力破解密钥是非常可行的。
https://www.ietf.org/rfc/rfc2898.txt
PBKDF1 应用哈希函数,该函数应为 MD2 [6]、MD5 [19] 或 SHA-1 [18],用于派生密钥。派生键的长度是有界的 哈希函数输出的长度,MD2 为 16 个八位字节 以及 SHA-1 的 MD5 和 20 个八位字节。PBKDF1 与密钥兼容 PKCS #5 v1.5 中的派生过程。建议仅将 PBKDF1 用于与现有 应用程序,因为它生成的密钥可能不够大 一些应用程序。
PBKDF2 应用伪随机函数(参见附录 B.1 example) 派生密钥。派生密钥的长度实质上是 无限。(但是,派生密钥的最大有效搜索空间可能受到底层结构的限制 伪随机函数。有关进一步讨论,请参阅附录 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。阅读该死的问题。
评论
aes-256-cbc
-pbkdf2
-iter
-in
-out
-k
meow
meow
-pass file:keyfile
keyfile
如其他答案所述,以前版本的 openssl 使用弱密钥派生函数从密码派生 AES 加密密钥。但是,openssl v1.1.1 支持更强大的密钥派生函数,其中密钥是从密码派生的,使用随机生成的盐,以及 sha256 哈希的多次迭代(默认为 10,000)。pbkdf2
要加密文件:
openssl aes-256-cbc -e -salt -pbkdf2 -iter 10000 -in plaintextfilename -out encryptedfilename
要解密文件:
openssl aes-256-cbc -d -salt -pbkdf2 -iter 10000 -in encryptedfilename -out plaintextfilename
注意:javascript 中的等效/兼容实现(使用 Web 加密 api)可以在 https://github.com/meixler/web-browser-based-file-encryption-decryption 中找到。
评论
man enc
openssl enc
评论
PKCS5_PBKDF2_HMAC
EVP_*