在解密命令中包含密码短语

时间:2011-12-28 14:17:12

标签: c# .net encryption openssl

好的,我有一个文本文件,其中包含一个名为textToDecrypt.txt的加密文本字符串。我在OpenSSL中运行以下命令来创建一个名为decrypted.txt的文件,其中包含解密数据:

rsautl -decrypt -inkey private.pem -in textToDecrypt.txt -out decrypted.txt

当我输入时,它要求我的下一件事是我的Passphrase,当我手动执行此操作时这很好,但是我打算在C#中以编程方式执行此操作,并且当我发送第一个命令时它会导致问题到程序后跟第二个包含密码的命令,如下所示。

OpenSSL Output

我的问题是,我可以将密码作为初始命令的一部分包括在内,而不是先发送解密命令,然后再发送密码吗?

只是为了澄清一下,当我手动执行此操作时(通过打开cmd.exe,导航到包含我的openssl.exe的目录并运行它,然后输入第一个命令,然后在提示时输入密码)一切正常,当我尝试以编程方式重新创建这个过程。

我尝试使用Thread.Sleep()在发送第二个命令之前等待几秒钟,但结果相同。

编辑:我的C#代码如下:

Process openSsl = new Process();

openSsl.StartInfo.FileName = processExecutable;
openSsl.StartInfo.UseShellExecute = false;
openSsl.StartInfo.CreateNoWindow = false;
openSsl.StartInfo.RedirectStandardInput = true;

openSsl.Start();

openSsl.StandardInput.WriteLine("rsautl -decrypt -inkey private.pem -in textToDecrypt.txt -out decrypted.txt");
openSsl.StandardInput.WriteLine("MyPassphrase");
openSsl.StandardInput.Close();

2 个答案:

答案 0 :(得分:2)

rsautl的输出告诉您它无法找到private.pem文件。这意味着该进程可能正在另一个目录中运行,而不是该文件所在的目录。

尝试将工作目录设置为private.pemtextToDecrypt.txt所在的目录(请参阅此问题:.NET Process.Start default directory?

openSsl.WorkingDirectory = // working directory

或者,使用private.pemtextToDecrypt.txt的绝对路径:

openSsl.StandardInput.WriteLine("rsautl -decrypt -inkey x:\full\path\private.pem -in x:\full\path\textToDecrypt.txt -out decrypted.txt");

答案 1 :(得分:1)

  1. 尝试设置密钥文件的完整路径: openSsl.StandardInput.WriteLine("rsautl -decrypt -inkey c:\full\path\there\private.pem -in textToDecrypt.txt -out decrypted.txt");

  2. 为什么不使用http://sourceforge.net/projects/openssl-net/?它是.NET的开放包装器。

相关问题