From 86d20c4d19369aad2a2182f7bec556f75120b8af Mon Sep 17 00:00:00 2001 From: r1a Date: Tue, 23 Jan 2024 19:38:15 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=8A=A0=E5=AF=86=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../archive_encrypt.sh" | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 "\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/archive_encrypt.sh" 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 0000000..913a88f --- /dev/null +++ "b/\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/archive_encrypt.sh" @@ -0,0 +1,45 @@ +#!/bin/bash +# 脚本用途介绍 +# 这个脚本用使用7z来加密计算机上的文件。 +# 用AES加密文件,然后用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 -- Gitee From e855e106178f9426ca48e5bf90e4d1746c6e37c9 Mon Sep 17 00:00:00 2001 From: r1a Date: Tue, 23 Jan 2024 19:38:23 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E8=A7=A3=E5=AF=86=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../archive_decrypt.sh" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100755 "\345\256\236\347\224\250\345\260\217\345\267\245\345\205\267/archive_decrypt.sh" 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 0000000..818e996 --- /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 -- Gitee From cb25ffeacbf69bc1cc9c45ec65a1209488cf8a96 Mon Sep 17 00:00:00 2001 From: r1a Date: Tue, 23 Jan 2024 19:46:47 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=AE=8C=E5=96=84=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E6=8F=8F=E8=BF=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../archive_encrypt.sh" | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) 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" index 913a88f..c4f75d0 100755 --- "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" @@ -1,8 +1,10 @@ #!/bin/bash -# 脚本用途介绍 -# 这个脚本用使用7z来加密计算机上的文件。 -# 用AES加密文件,然后用RSA非对称加密加密密码。 -# 压缩包密码是随机生成的高强度字符串,只需要保管GPG私钥,不需要记压缩包的密码,充分利用对称加密的算法高效和非对称加密的安全。 +# 脚本用途介绍: +# 该脚本用使用7zip来加密计算机上的文件。 +# 然后用RSA非对称加密,加密密码。 +# 密码是随机生成的高强度字符串,用户只需要保管GPG私钥,不需要记压缩包的密码。 +# 每个压缩包都能拥有不同的密码,成千上万各密码,都不用记,只需要一个密钥。 +# 充分利用对称加密的算法效率和非对称加密的安全。 # 依赖:需要openssl,gpg,和 7z。 # 检查参数数量 -- Gitee