Bash脚本openssl加密,传递问题md5

时间:2012-09-05 08:16:23

标签: bash encryption openssl

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

针对我们正在加密的文件执行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
}
编辑:好的,我能够让这个工作(减去tar部分),但我的问题仍然是,有没有办法改变这个,所以我可以使用openssl enc的隐藏输入密码和一些如何修改通过附加#md5sumhere输入的密码?不太清楚如何去做。

再次感谢。

1 个答案:

答案 0 :(得分:3)

我使用哈希密码而不是明文密码:

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

作为旁注:请在计算摘要时使用sha1或更高版本(sha256更为理想)

相关问题