diff --git "a/\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/archive_decrypt.sh" "b/\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/archive_decrypt.sh" new file mode 100755 index 0000000000000000000000000000000000000000..818e9962a6e60d1f9d51a2ab698dfde86d16882e --- /dev/null +++ "b/\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/archive_decrypt.sh" @@ -0,0 +1,49 @@ +#!/bin/bash +# 加密脚本的自动解密。 + +# 检查参数数量 +if [ $# -lt 1 ]; then + echo "用法: $0 <压缩包的文件名> [解压的位置]" + exit 1 +fi + +# 压缩包文件名 +archive_name="$1" +extract_location="${2:-.}" # 如果未提供解压位置,默认为当前目录 + +# 检查压缩包是否存在 +if [ ! -f "${archive_name}" ]; then + echo "压缩包文件不存在: ${archive_name}" + exit 1 +fi + +# 检查同名的txt文件是否存在 +txt_file="${archive_name%.7z}.passwd.txt" + +if [ -f "${txt_file}" ]; then + # 读取txt文件内容 + cat "${txt_file}" + + # 验证SHA1校验和 + sha1sum=$(sha1sum "${archive_name}" | awk '{print $1}') + stored_sha1sum=$(grep -oP 'SHA1 Checksum: \K.*' "${txt_file}") + + if [ "${sha1sum}" != "${stored_sha1sum}" ]; then + echo -e "\e[91mSHA1校验失败!压缩包可能已被修改。\e[0m" + exit 1 + else + echo -e "\e[92m校验通过!\e[0m" + fi + + # 读取使用GPG加密后的密码 + encrypted_password=$(grep -oP 'Encrypted Password: \K.*' "${txt_file}") + + # 解密密码 + password=$(echo "${encrypted_password}" | gpg --decrypt --quiet) + + # 解压缩压缩包 + 7z x -p"${password}" -o"${extract_location}" "${archive_name}" +else + echo "找不到同名的txt文件: ${txt_file}" + exit 1 +fi \ No newline at end of file diff --git "a/\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/archive_encrypt.sh" "b/\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/archive_encrypt.sh" new file mode 100755 index 0000000000000000000000000000000000000000..c4f75d03be8e108c5a5dbd70f072a97aefd3f43c --- /dev/null +++ "b/\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/archive_encrypt.sh" @@ -0,0 +1,47 @@ +#!/bin/bash +# 脚本用途介绍: +# 该脚本用使用7zip来加密计算机上的文件。 +# 然后用RSA非对称加密,加密密码。 +# 密码是随机生成的高强度字符串,用户只需要保管GPG私钥,不需要记压缩包的密码。 +# 每个压缩包都能拥有不同的密码,成千上万各密码,都不用记,只需要一个密钥。 +# 充分利用对称加密的算法效率和非对称加密的安全。 +# 依赖:需要openssl,gpg,和 7z。 + +# 检查参数数量 +if [ $# -lt 2 ]; then + echo "用法: $0 <保存的文件名> <要添加到压缩包的文件...>" + exit 1 +fi + +# 生成16位密码 +password=$(openssl rand -base64 12 | tr -dc 'a-zA-Z0-9' | head -c16) + +# 压缩文件名 +archive_name="$1" +txt_file="${archive_name%.7z}.passwd.txt" + +# 移除第一个参数 +shift + +# 使用密码加密文件和文件名,生成压缩包 +7z a -p"${password}" -mhe=on "${archive_name}" "$@" + +# 使用GPG加密密码 +encrypted_password=$(echo "${password}" | gpg --symmetric --armor --passphrase-fd 0 --quiet) + +# 计算SHA1校验和 +sha1sum=$(sha1sum "${archive_name}" | awk '{print $1}') + +# 获取文件名、创建日期和文件大小 +file_name=$(basename "${archive_name}") +creation_date=$(date -r "${archive_name}") +file_size=$(du -h "${archive_name}" | awk '{print $1}') + +# 将加密的密码、SHA1校验和、文件名、创建日期和文件大小写入txt文件 +echo "SHA1 Checksum: ${sha1sum}" >> "${txt_file}" +echo "File Name: ${file_name}" >> "${txt_file}" +echo "Creation Date: ${creation_date}" >> "${txt_file}" +echo "File Size: ${file_size}" >> "${txt_file}" +echo "Encrypted Password: ${encrypted_password}" > "${txt_file}" + +echo "压缩包已创建并加密,密码已使用GPG加密,并将信息写入${txt_file}文件。" \ No newline at end of file