使用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輕松地加密和解密文件。