面试题答案
一键面试以下是实现该功能的Bash脚本代码:
加密部分
#!/bin/bash
# 定义变量
plaintext_file="plaintext.txt"
encrypted_file="encrypted.txt"
password="your_password"
# 使用OpenSSL进行对称加密,这里使用AES-256-CBC算法
openssl enc -aes-256-cbc -salt -in $plaintext_file -out $encrypted_file -k $password
- 定义变量:
plaintext_file
定义了要加密的明文文件。encrypted_file
定义了加密后的文件。password
定义了用于加密和解密的密码。
- 加密命令:
openssl enc
:这是OpenSSL中用于加密和解密的子命令。-aes - 256 - cbc
:指定使用AES - 256 - CBC对称加密算法。-salt
:在加密过程中添加盐值,增加密码安全性。-in $plaintext_file
:指定输入的明文文件。-out $encrypted_file
:指定输出的加密文件。-k $password
:指定用于加密的密码。
解密部分
#!/bin/bash
# 定义变量
encrypted_file="encrypted.txt"
decrypted_file="decrypted.txt"
password="your_password"
# 使用OpenSSL进行对称解密
openssl enc -d -aes-256-cbc -in $encrypted_file -out $decrypted_file -k $password
- 定义变量:
encrypted_file
定义了要解密的加密文件。decrypted_file
定义了解密后的文件。password
定义了用于解密的密码,必须与加密时的密码一致。
- 解密命令:
openssl enc
:同样是OpenSSL的加密和解密子命令。-d
:指定为解密操作。-aes - 256 - cbc
:指定解密使用的算法,需与加密时一致。-in $encrypted_file
:指定输入的加密文件。-out $decrypted_file
:指定输出的解密文件。-k $password
:指定用于解密的密码。