bash 脚本中的文件加密,无需明确提供密码

mor*_*ous 7 linux encryption bash openssh ubuntu

我想自动化以下手动过程。

目前,我正在使用 openssl 加密一组文件,如下所示:

在 CBC 模式下使用 256 位 AES 将 file.txt 加密到 file.out

$ openssl enc -aes-256-cbc -salt -in file1 -out file1.enc

然后我会被提示输入密码,然后用它来加密文件

解密时,我输入

$ openssl enc -d -aes-256-cbc -in file1.enc -out file

然后提示我输入密码 - 我再次手动输入密码。

我想自动化这个加密/解密过程 - 所以我需要找到一种为 openssh 提供密码的方法。

我的第一个想法是是否可以从文件中读取密码(比如说)?或者有没有更好的方法来做到这一点?

另外,我想我必须限制谁可以查看密码文件 - 否则,这会破坏使用密码的整个目标。我正在考虑以特定用户身份运行 bash 脚本,然后仅授予该用户对该文件内容的读取权限。

这是这样做的方式 - 还是有更好的方法?

当然,所有这些都会导致另一个问题——即,如何以另一个用户身份运行 bash 脚本——而不必在终端输入用户密码......?

顺便说一句,我在 Linux Ubuntu 10.0.4 上运行

aki*_*ira 8

阅读man openssl(尤其是PASS PHRASE ARGUMENTS部分):

Several commands accept password arguments, typically using -passin 
and -passout for input and output passwords respectively. These allow
the password to be obtained from a variety of sources. Both of these
options take a single argument whose format is described below. If no
password argument is given and a password is required then the user is
prompted to enter one: this will typically be read from the current
terminal with echoing turned off.

   pass:password
             the actual password is password. Since the password is visible
             to utilities (like 'ps' under Unix) this form
             should only be used where security is not important.

   env:var   obtain the password from the environment variable var. Since 
             the environment of other processes is visible on
             certain platforms (e.g. ps under certain Unix OSes)
             this option should be used with caution.

   file:pathname
             the first line of pathname is the password. If the same 
             pathname argument is supplied to -passin and -passout
             arguments then the first line will be used for the input 
             password and the next line for the output password.
             pathname need not refer to a regular file: it could for 
             example refer to a device or named pipe.

   fd:number read the password from the file descriptor number. This 
             can be used to send the data via a pipe for example.

   stdin     read the password from standard input.
Run Code Online (Sandbox Code Playgroud)

openssl enc接受-pass <arg>......所以,从上面给出的列表中选择你的 arg。例如:

 echo -n "secret" | openssl enc -aes-256-cbc -salt \
        -in file1 -out file1.enc \
        -pass stdin
Run Code Online (Sandbox Code Playgroud)

  • 所以OP想要`-pass file:"mysecret.txt"` (2认同)