【问题标题】:Bash script openssl encryption, md5 in pass issueBash脚本openssl加密,md5 in pass问题
【发布时间】:2012-08-29 22:08:53
【问题描述】:

我正在尝试创建一个用于加密文件的脚本方法,我正在尝试让它执行以下操作:

对我们正在加密的文件进行 MD5 校验和,仅剥离 MD5 值并将其放入变量中,

向用户询问密码,(希望我可以使用 openssl 输入法输入密码,而不是使用来自 bash 的输入,如果它打印到终端似乎不太安全)但我不知道该怎么走关于这一点,是否有一种有效的安全方式来做到这一点?

不管怎样,取那个密码,加一个#,然后是md5总和,IE:passhere#md5sumoforiginalfile

在理想情况下,它会先 tar.gz 文件,然后对压缩存档执行加密。

这是我有点难过的地方,我在打印 md5 并将 md5 哈希合并到密码中时遇到问题,也感觉按原样输入也不正确,有没有更好的方法这样做?

提前谢谢大家!

这是函数(最初由作者:Matt Reid 从网络上的另一个脚本中获取)

function encrypt() {
filein="$1"
fileout="$2"

if [ "$filein" = "no" ]; then
    echo -n "No input file specified, what file are we encrypting: "
    read filein
fi

if [ -r "$filein" ]; then
    if [ "$fileout" = "no" ]; then
        fileout="$filein.aes256"
    fi
    if [ -f "$fileout" ]; then
        echo "Output file exists already, encrypting will overwrite this file."
        echo -n "Do you want to encrypt anyway? [Y/n]: "
        read choice
        if [ "$choice" = "Y" ] || [ "$choice" = "y" ] || [ "$choice" = "" ]; then
    openssl enc -aes-256-cbc -a -salt -in $filein -out $fileout
            generate_digests $filein $fileout
            sdelete $filein
            exit 0;
        else 
            exit 2;
        fi      
    else
    filemd5=$(openssl dgst -md5 $filein | cut -d '  ' -f 1)
    echo "Please enter password: "
        read passvar
    openssl enc -aes-256-cbc -a -salt -out $fileout -k ${passvar}#${filemd5}
        generate_digests $filein $fileout
        sdelete $filein
        exit 0;
    fi
else
    echo "Input file does not exist or is not readable. You're attempting to encrypt file: '$filein'"
    exit 1;
fi
}

编辑:好的,我能够得到这个工作(减去焦油部分),但我的问题仍然存在,有没有办法改变这个,所以我可以使用 openssl enc 的隐藏输入作为密码以及一些如何修改通过附加 #md5sumhere 输入的密码?不太清楚该怎么做。

再次感谢。

【问题讨论】:

    标签: bash encryption openssl


    【解决方案1】:

    我会使用散列密码而不是明文密码:

    salt="astringtobeusedassalt"
    hashedpassword=`openssl passwd -salt $salt`
    echo $hashedpassword
    finalpassword="$hashedpassword""#""$filemd5" # $filemd5 variable from your script
    

    附带说明:计算摘要时请使用sha1 或更高版本(sha256 更可取)

    【讨论】:

    • Avio,好主意,我没有考虑这样做。我会做这个补充。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    相关资源
    最近更新 更多