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

Encryption 如何使用OpenSSL加密/解密文件?,encryption,openssl,Encryption,Openssl,我想用一个密码加密和解密一个文件 我如何使用OpenSSL来做到这一点 安全警告:AES-256-CBC不提供安全警告,并且容易受到攻击。你应该用类似的东西来代替 加密: openssl aes-256-cbc -a -salt -in secrets.txt -out secrets.txt.enc openssl enc -in infile.txt -out encrypted.dat -e -aes256 -k symmetrickey 解密: openssl aes-256-cbc

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

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

安全警告:AES-256-CBC不提供安全警告,并且容易受到攻击。你应该用类似的东西来代替

加密:

openssl aes-256-cbc -a -salt -in secrets.txt -out secrets.txt.enc
openssl enc -in infile.txt -out encrypted.dat -e -aes256 -k symmetrickey
解密:

openssl aes-256-cbc -d -a -in secrets.txt.enc -out secrets.txt.new
openssl enc -in encrypted.dat -out outfile.txt -d -aes256 -k symmetrickey
加密:

openssl aes-256-cbc -a -salt -in secrets.txt -out secrets.txt.enc
openssl enc -in infile.txt -out encrypted.dat -e -aes256 -k symmetrickey
解密:

openssl aes-256-cbc -d -a -in secrets.txt.enc -out secrets.txt.new
openssl enc -in encrypted.dat -out outfile.txt -d -aes256 -k symmetrickey

有关详细信息,请参阅文档。

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

百科全书:

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 enc -aes-256-cbc -in un_encrypted.data -out encrypted.data
openssl enc -d -aes-256-cbc -in encrypted.data -out un_encrypted.data
gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data
gpg --output un_encrypted.data --decrypt encrypted.data
$ openssl bf < arquivo.txt > arquivo.txt.bf
$ openssl bf -d < arquivo.txt.bf > arquivo.txt
解密:

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 enc -aes-256-cbc -in un_encrypted.data -out encrypted.data
openssl enc -d -aes-256-cbc -in encrypted.data -out un_encrypted.data
gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data
gpg --output un_encrypted.data --decrypt encrypted.data
$ openssl bf < arquivo.txt > arquivo.txt.bf
$ openssl bf -d < arquivo.txt.bf > arquivo.txt

我在网上找到了一个开源程序,它使用openssl加密和解密文件。它只需要一个密码就可以做到这一点。这个开源脚本的优点在于它通过分解文件来删除原始的未加密文件。但危险的是,一旦原始的未加密文件消失,你必须确保记住密码,否则就没有其他方法来解密你的文件

这里是github上的链接

简短回答: 您可能希望使用
gpg
而不是
openssl
,因此请参见本答案末尾的“其他注意事项”。但要使用openssl回答问题,请执行以下操作:


加密:

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 enc -aes-256-cbc -in un_encrypted.data -out encrypted.data
openssl enc -d -aes-256-cbc -in encrypted.data -out un_encrypted.data
gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data
gpg --output un_encrypted.data --decrypt encrypted.data
$ openssl bf < arquivo.txt > arquivo.txt.bf
$ openssl bf -d < arquivo.txt.bf > arquivo.txt
要解密:

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 enc -aes-256-cbc -in un_encrypted.data -out encrypted.data
openssl enc -d -aes-256-cbc -in encrypted.data -out un_encrypted.data
gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data
gpg --output un_encrypted.data --decrypt encrypted.data
$ openssl bf < arquivo.txt > arquivo.txt.bf
$ openssl bf -d < arquivo.txt.bf > arquivo.txt

注意:加密或解密时,系统将提示您输入密码


长答覆:
openssl 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.
要解密:

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 enc -aes-256-cbc -in un_encrypted.data -out encrypted.data
openssl enc -d -aes-256-cbc -in encrypted.data -out un_encrypted.data
gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data
gpg --output un_encrypted.data --decrypt encrypted.data
$ openssl bf < arquivo.txt > arquivo.txt.bf
$ openssl bf -d < arquivo.txt.bf > arquivo.txt

注意:加密或解密时会提示您输入密码。

要加密:

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 enc -aes-256-cbc -in un_encrypted.data -out encrypted.data
openssl enc -d -aes-256-cbc -in encrypted.data -out un_encrypted.data
gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data
gpg --output un_encrypted.data --decrypt encrypted.data
$ openssl bf < arquivo.txt > arquivo.txt.bf
$ openssl bf -d < arquivo.txt.bf > arquivo.txt
$openssl bfarquivo.txt.bf
要解密:

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 enc -aes-256-cbc -in un_encrypted.data -out encrypted.data
openssl enc -d -aes-256-cbc -in encrypted.data -out un_encrypted.data
gpg --output encrypted.data --symmetric --cipher-algo AES256 un_encrypted.data
gpg --output un_encrypted.data --decrypt encrypted.data
$ openssl bf < arquivo.txt > arquivo.txt.bf
$ openssl bf -d < arquivo.txt.bf > arquivo.txt
$openssl bf-darquivo.txt

bf===CBC模式下的Blowfish

请注意,OpenSSL CLI使用弱非标准算法将密码短语转换为密钥,安装GPG会导致各种文件添加到主目录中,并且GPG代理后台进程正在运行。如果您希望现有工具具有最大的可移植性和控制性,那么可以使用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默认密钥派生

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

攻击者只需对密钥进行暴力攻击是非常可行的

PBKDF1应用哈希函数,该函数应为MD2[6]、MD5[19]或 SHA-1[18],以派生密钥。派生键的长度是有界的 由哈希函数输出的长度决定,MD2为16个八位字节 SHA-1的MD5和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次。 以下是关于迭代次数的一个很好的答案:


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

如其他答案中所述,早期版本的openssl使用弱密钥派生函数从密码派生AES加密密钥。但是,openssl v1.1.1支持更强大的密钥派生功能,其中密钥是使用带有随机生成的salt的
pbkdf2
从密码派生的,并且多次迭代sha256哈希(默认情况下为10000)

要加密文件,请执行以下操作:

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
要解密文件,请执行以下操作:

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

要使用明文密码,请将
-k symmetrickey
替换为
-pass stdin
-pass'pass:password'
由于没有密钥派生,因此不要使用上述命令。在这里阅读更多内容:与@jonasl的评论相关,请注意
-k symmetrickey
具有误导性。
-k
选项用于指定密码,OpenSSL从中派生对称密钥。如果要指定对称密钥,必须使用
-K
选项。应使用
PKCS5\u PBKDF2\u HMAC
从密码派生密钥和IV。您应该使用
EVP.*
函数进行加密和解密。请参见OpenSSL wiki上的。事实上,您可能应该使用经过身份验证的加密,因为它同时提供机密性和真实性。请参阅OpenSSL wiki上的。不理解您的问题为什么需要OpenSSL。下面的一条评论显示GPG更好——也是因为安全性。我投反对票。你可能想看看“禁止”以便保存加密文件使用的所有加密选项。。。这一点在openssl enc的“默认”选项已经改变,并且在将来可能会改变的情况下尤为重要。还为新的-pbkdf2选项设置更高的随机迭代计数。根据OP的用例,答案可能不是最优的(在撰写本文时)。具体而言,参数“-a”可能不是最优的,并且