MST

星途 面试题库

面试题:Bash结合gpg实现文本文件加密及权限控制

在Bash环境下,利用gpg对文本文件进行加密,使得只有特定用户组的成员能够解密。假设已有用户组名为secure_group,且已为该组内成员生成了相应的密钥对。描述实现过程,包括密钥管理、文件加密和解密的Bash脚本编写,以及如何设置权限确保安全性。
13.1万 热度难度
编程语言Bash

知识考点

AI 面试

面试题答案

一键面试
  1. 密钥管理
    • 确保secure_group组内成员的公钥已收集并导入到本地密钥环。假设公钥文件为member1.pubmember2.pub等,可以使用以下命令导入公钥:
    gpg --import member1.pub
    gpg --import member2.pub
    
  2. 文件加密Bash脚本编写
    • 创建一个名为encrypt_file.sh的脚本:
    #!/bin/bash
    if [ $# -ne 2 ]; then
        echo "Usage: $0 <input_file> <output_encrypted_file>"
        exit 1
    fi
    input_file=$1
    output_file=$2
    gpg --recipient secure_group --output $output_file --encrypt $input_file
    
    • 给脚本添加执行权限:
    chmod +x encrypt_file.sh
    
  3. 文件解密Bash脚本编写
    • 创建一个名为decrypt_file.sh的脚本:
    #!/bin/bash
    if [ $# -ne 2 ]; then
        echo "Usage: $0 <input_encrypted_file> <output_decrypted_file>"
        exit 1
    fi
    input_file=$1
    output_file=$2
    gpg --output $output_file --decrypt $input_file
    
    • 给脚本添加执行权限:
    chmod +x decrypt_file.sh
    
  4. 设置权限确保安全性
    • 对于加密后的文件,确保只有secure_group组内成员有读取权限:
    chgrp secure_group encrypted_file.gpg
    chmod g+r encrypted_file.gpg
    chmod o-r encrypted_file.gpg
    
    • 对于私钥,确保只有所有者有读取和写入权限:
    chmod 600 ~/.gnupg/private -r
    
    • 对于encrypt_file.shdecrypt_file.sh脚本,确保只有所有者有执行权限:
    chmod 700 encrypt_file.sh
    chmod 700 decrypt_file.sh