加密:
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
虽然您特别询问了关于OpenSSL的问题(请参见下面的"使用OpenSSL"以获取OpenSSL解决方案),但您可能希望考虑使用GPG来进行加密,根据这篇文章OpenSSL vs GPG for encrypting off-site backups?。
要使用GPG执行相同的操作,您可以使用以下命令:
加密:
gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data
解密:
gpg --output un_encrypted.data --decrypt encrypted.data
--no-symkey-cache
标志来禁用缓存。
如上所述,您可能更倾向于使用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 enc
,您最好的信息来源可能是:https://www.openssl.org/docs/man1.1.1/man1/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]
-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
让我在解密文件时没有被提示输入密码。看起来密码已经被存储了一段时间,这并不是我想要的。 - user76284--no-symkey-cache
会禁用 gpg 在使用 --symmetric
时的缓存功能,即使代理正在运行。 - user76284加密:
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'
。 - Zenexer-k symmetrickey
是具有误导性的。-k
选项用于指定密码,OpenSSL从中派生对称密钥。如果您想要指定对称密钥,则必须使用-K
选项。 - user1071847不要使用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 了,请认真阅读问题。
aes-256-cbc
作为"密码命令",-pbkdf2
是"基于密码的密钥派生函数,第二代",-iter
是密钥派生函数运行的迭代次数,-in
是输入文件,-out
是加密/解密的结果,-k
是密码本身。第二个命令将提示输入密码,在这种情况下是meow
。为了安全起见,请使用比meow
要长得多的密码。请注意,我通常使用-pass file:keyfile
和keyfile
通常是一个64个或更多字符的文件。您将需要它来解密,所以不要丢失它! - undefined如其他答案所述,之前版本的openssl使用了一个弱密钥派生函数来从密码中派生AES加密密钥。然而,openssl v1.1.1支持更强的密钥派生函数,其中密钥是使用pbkdf2
和随机生成的盐以及多次sha256哈希(默认为10,000次)从密码中派生出来的。
要加密一个文件:
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
注意:使用Web Crypto API在JavaScript中有一个等效/兼容的实现,可以在https://github.com/meixler/web-browser-based-file-encryption-decryption找到。
man enc
,以获取有关使用openssl enc
的选项。 - undefined加密:
$ openssl bf < arquivo.txt > arquivo.txt.bf
解密:
$ openssl bf -d < arquivo.txt.bf > arquivo.txt
bf === 在CBC模式下的Blowfish算法
使用随机生成的公钥进行更新。
加密:
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
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
openssl_pbkdf2
函数来安全地将口令转换为密钥。
PKCS5_PBKDF2_HMAC
从密码中派生出一个密钥和IV。你应该使用EVP_*
函数进行加密和解密。请参阅OpenSSL维基上的EVP对称加密和解密。事实上,你应该使用认证加密,因为它提供了机密性和真实性。请参阅OpenSSL维基上的EVP认证加密和解密。 - jww