7z 带有唯一密码和文件名的目录

huangapple go评论50阅读模式
英文:

7z directrories with unique passwords and filenames

问题

我有一堆目录,我想用7z来进行密码保护。我想生成一个64字符的字符串作为密码,一个6字符的字符串作为文件的前缀。

理想情况下,我希望将它们都保存到一个文件中,以数组的形式来跟踪名称和密码。

目前我手动操作,但现在我有太多要做,所以我想使用bash来完成。

我使用我的密码管理器生成了密码和6字符的字符串。将它们复制并粘贴到脚本中运行。对于1或2个文件来说,这样做还可以,但现在我有数百个文件。

Password_string=NzNeUGMpA1DkFPGQW3CEfLX1PIQdFNmKwyBrTqjv73ydcPwAJnNHUZFj4r2siBEu

a=1

for i in *;
do 7za a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=off -p$Password_string -mhe=on $i.7z $i
    new=$(printf "636995_archive_%04d.7z" "$a") #04 pad to length of 4
    mv -i -- "$i.7z" "$new" #rename to include numbers
    let a=a+1 #increment counter
done

这是您提供的bash脚本,我已经将其翻译好了。

英文:

I have a bunch of directories that I want to password protect with 7z. I want to generate a 64 character string to use as the password, a 6 character string to use as a prefix to the file.
I would ideally like to save both to a file in an array to keep track of the names and passwords.

Currently i do this manually, but I have too many to do now so I would like to use bash to do it.

I generated the passwords and 6 character string using my password manager. Copy and paste into the script and run it. For 1 or 2 files this ok, but now I have 100's.

Password_string=NzNeUGMpA1DkFPGQW3CEfLX1PIQdFNmKwyBrTqjv73ydcPwAJnNHUZFj4r2siBEu

a=1

for i in *;
do 7za a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=off -p$Password_string -mhe=on $i.7z $i
    new=$(printf "636995_archive_%04d.7z" "$a") #04 pad to length of 4
    mv -i -- "$i.7z" "$new" #rename to include numbers
    let a=a+1 #increment counter
done

答案1

得分: 2

你可以在循环中随机生成前缀和密码。创建存档后,可以在下一次循环迭代之前将变量记录到文件中。

以下是我的示例。我注释了7z命令,这样我可以测试脚本并查看日志。

seq=0
echo 'dir,prefix,seq,archive,password' > log.csv

for dir in /*; do
    prefix=$(head -c 6 <(tr -dc '0-9' < /dev/urandom))  # 生成随机的6位前缀
    pw64=$(head -c 64 <(tr -dc '[:alnum:]' < /dev/urandom))  # 生成随机的64字符字母数字字符串
    ((seq+=1))
    archive=$(printf "${prefix}_archive_%04d" "$seq")  # 存档名称
    # 7za a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=off -p"$pw64" -mhe=on "$archive.7z" "$dir"
    echo "$dir,$prefix,$seq,$archive,$pw64" >> log.csv
done

日志的部分视图:

dir,prefix,seq,archive,password
/bin,614175,1,614175_archive_0001,C7UhfDcfUyjV0JeOBykMn6fDMF4H4hWka7Y4xluzmWGDPqN0vlNzReIwLv7cYSE7
/boot,605074,2,605074_archive_0002,BsUQ5uEFpMZKZnKpUq7OH3VVMpp4jrsUrtVWBt6h1Q3S3mHAr5cnJk9w10LefzL0
/dev,676241,3,676241_archive_0003,5zcnV9EO8Z5nOA3VANGdMoVGTIdjT77RUKybd6cgfXLXDXLA9jiPYtJO66ZwTSbq
英文:

you can randomly generate the prefix and password in your loop. After you create the archive, you can log the variables to a file before the next iteration of the loop.

Here's my example. I commented the 7z command, so I could test the script and see the log.

seq=0
echo &#39;dir,prefix,seq,archive,password&#39; &gt; log.csv

for dir in /*; do
    prefix=$(head -c 6 &lt;(tr -dc &#39;0-9&#39; &lt; /dev/urandom))  # generate random 6-digit prefix
    pw64=$(head -c 64 &lt;(tr -dc &#39;[:alnum:]&#39; &lt; /dev/urandom))  # generate random 64-char alphanumeric string
    ((seq+=1))
    archive=$(printf &quot;${prefix}_archive_%04d&quot; &quot;$seq&quot;)  # name of archive
    #7za a -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=off -p&quot;$pw64&quot; -mhe=on &quot;$archive.7z&quot; &quot;$dir&quot;
    echo &quot;$dir,$prefix,$seq,$archive,$pw64&quot; &gt;&gt; log.csv
done

Partial view of the log:

dir,prefix,seq,archive,password
/bin,614175,1,614175_archive_0001,C7UhfDcfUyjV0JeOBykMn6fDMF4H4hWka7Y4xluzmWGDPqN0vlNzReIwLv7cYSE7
/boot,605074,2,605074_archive_0002,BsUQ5uEFpMZKZnKpUq7OH3VVMpp4jrsUrtVWBt6h1Q3S3mHAr5cnJk9w10LefzL0
/dev,676241,3,676241_archive_0003,5zcnV9EO8Z5nOA3VANGdMoVGTIdjT77RUKybd6cgfXLXDXLA9jiPYtJO66ZwTSbq

huangapple
  • 本文由 发表于 2023年2月10日 11:21:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75406622.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定