在Ubuntu中,使用OpenSSL加密數據有多種方法,以下是一些常用的命令:
加密文件:
openssl enc -aes-256-cbc -salt -in input_file -out encrypted_file
-aes-256-cbc
:指定使用AES-256-CBC加密算法。-salt
:添加鹽值以增加安全性。-in input_file
:指定要加密的輸入文件。-out encrypted_file
:指定加密后的輸出文件。解密文件:
openssl enc -d -aes-256-cbc -in encrypted_file -out decrypted_file
-d
:表示解密操作。生成RSA密鑰對:
openssl genpkey -algorithm RSA -out rsa_key.pem -pkeyopt rsa_keygen_bits:2048
-algorithm RSA
:指定使用RSA算法。-out rsa_key.pem
:指定生成的私鑰文件。-pkeyopt rsa_keygen_bits:2048
:指定密鑰長度為2048位。提取公鑰:
openssl rsa -pubout -in rsa_key.pem -out rsa_pubkey.pem
-pubout
:提取公鑰。-in rsa_key.pem
:指定私鑰文件。-out rsa_pubkey.pem
:指定輸出的公鑰文件。使用公鑰加密文件:
openssl pkeyutl -encrypt -in input_file -out encrypted_file -pubin -inkey rsa_pubkey.pem
-encrypt
:表示加密操作。-in input_file
:指定要加密的輸入文件。-out encrypted_file
:指定加密后的輸出文件。-pubin
:指定使用公鑰進行加密。-inkey rsa_pubkey.pem
:指定公鑰文件。使用私鑰解密文件:
openssl pkeyutl -decrypt -in encrypted_file -out decrypted_file -inkey rsa_key.pem
-decrypt
:表示解密操作。使用openssl enc
命令進行對稱加密:
openssl enc -aes-256-cbc -salt -in plaintext.txt -out ciphertext.bin
使用openssl pkeyutl
命令進行非對稱加密:
openssl pkeyutl -encrypt -in plaintext.txt -out ciphertext.bin -pubin -inkey public_key.pem
通過以上方法,你可以在Ubuntu系統中使用OpenSSL進行數據的加密和解密操作。