使用OpenSSL加密和解密文件是一个常见的任务,以下是详细步骤:
使用对称加密(AES)
openssl rand -base64 32 > aes.key
openssl enc -aes-256-cbc -salt -in plaintext.txt -out encrypted.bin -pass file:aes.key
plaintext.txt
是要加密的文件,encrypted.bin
是加密后的文件,aes.key
是包含AES密钥的文件。使用非对称加密(RSA)
openssl genpkey -algorithm RSA -out rsa_key.pem -aes256
openssl rsa -pubout -in rsa_key.pem -out rsa_key.pub
openssl pkeyutl -encrypt -in plaintext.txt -out encrypted.bin -pubin -inkey rsa_key.pub
rsa_key.pem
是私钥文件,rsa_key.pub
是公钥文件。使用对称加密(AES)
openssl enc -d -aes-256-cbc -in encrypted.bin -out decrypted.txt -pass file:aes.key
encrypted.bin
是加密后的文件,decrypted.txt
是解密后的文件,aes.key
是包含AES密钥的文件。使用非对称加密(RSA)
openssl pkeyutl -decrypt -in encrypted.bin -out decrypted.txt -inkey rsa_key.pem
encrypted.bin
是加密后的文件,decrypted.txt
是解密后的文件,rsa_key.pem
是私钥文件。通过以上步骤,你可以使用OpenSSL轻松地加密和解密文件。