You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在新进程中启动7z.exe并安全传递加密密码?

问题:Windows Server下无明文密码启动7z.exe生成加密压缩包

在Windows Server环境中,需要启动7z.exe并传入包含密码在内的所有参数以生成加密压缩包,但不能通过明文密码传递——因为进程命令行可能被服务器上的其他账户查看。

已尝试的两种方案

1. 使用环境变量传递密码

尝试通过设置环境变量传递密码,代码如下:

var psi = new ProcessStartInfo("7z.exe", "a ... -p%MYSECRET%")
{
    CreateNoWindow = true,
    WindowStyle = ProcessWindowStyle.Hidden,
    UseShellExecute = false,
    EnvironmentVariables =
    {
        { "MYSECRET", password }
    }
};

var process = Process.Start(psi);
process.WaitForExit();

问题:7z.exe将字面量%MYSECRET%作为密码,并未解析环境变量的实际值。

2. 通过重定向标准输入输入密码

尝试通过标准输入传递密码,代码如下:

var psi = new ProcessStartInfo("7z.exe", "a ... -p")
{
    CreateNoWindow = true,
    WindowStyle = ProcessWindowStyle.Hidden,
    UseShellExecute = false,
    RedirectStandardInput = true
};

var process = Process.Start(psi);

process.StandardInput.Write(password);
process.StandardInput.Close();
process.WaitForExit();

问题:生成的压缩包无法用原密码或空密码解密,推测7z.exe仅支持交互式键盘输入密码。

已排除的方案

考虑过通过cmd.exe执行批处理解析环境变量,但此方式会导致明文密码出现在7z.exe的进程命令行,违背了隐藏密码的初衷。

当前诉求

目前想到可通过C#封装7z.dll并调用COM接口传递参数,但不确定是否值得投入精力,现寻求其他可行方案。

内容的提问来源于stack exchange,提问作者aoven

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 02:33:13