0

Ok, I have a text file containing an encrypted string of text called textToDecrypt.txt. I run the following command in OpenSSL to create a file called decrypted.txt containing the decrypted data:

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

When I type this in, the next thing it asks me for is my Passphrase which is fine when I am doing this manually, however I am planning to do this programatically in C# and it is causing issues when I send the first command to the program followed by the second command containing the passphrase, as shown below.

OpenSSL Output

My question is, can I include the passphrase as part of the initial command, instead of sending the decrypt command first followed by the passphrase?

Just to clarify, when I do this manually (by opening up cmd.exe, navigating to the directory containing my openssl.exe and running this then typing in the first command followed by the passphrase when prompted) everything works perfectly, when I try to recreate this process programmatically things go wrong.

I tried to use Thread.Sleep() to wait a few seconds before sending the second command but it had the same result.

Edit: My C# code is below:

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();
abatishchev
  • 98,240
  • 88
  • 296
  • 433
JMK
  • 27,273
  • 52
  • 163
  • 280
  • Look at your output: The problem doesn't seem to be that the passphrase can't be entered, but that the private key file can't be found. Are you sure it's in the program's current directory? – codeling Dec 28 '11 at 14:28
  • It is, like I say when I run these commands manually everything works fine, the issue is when I run the same commands using the same exe programmatically. – JMK Dec 28 '11 at 14:30
  • But the output still says that it doesn't find the private.pem file... your application probably has a different "current directory" set. There's very little probability that the rsautl application would print that error just because it's started by another program, not because **it really doesn't find private.pem** – codeling Dec 28 '11 at 14:31

2 Answers2

2

The output of rsautl tells you that it can't find the private.pem file. This means that the process is probably running in another directory than the one where this file is.

Try setting the working directory to the one where private.pem and textToDecrypt.txt are in (see this question: .NET Process.Start default directory?)

openSsl.WorkingDirectory = // working directory

Or, use absolute paths for private.pem and textToDecrypt.txt:

openSsl.StandardInput.WriteLine("rsautl -decrypt -inkey x:\full\path\private.pem -in x:\full\path\textToDecrypt.txt -out decrypted.txt");
Community
  • 1
  • 1
codeling
  • 11,056
  • 4
  • 42
  • 71
1
  1. Try to set full path to keyfile: openSsl.StandardInput.WriteLine("rsautl -decrypt -inkey c:\full\path\there\private.pem -in textToDecrypt.txt -out decrypted.txt");

  2. Why not to use http://sourceforge.net/projects/openssl-net/ ? It's a openssl wrapper to .NET.

werewindle
  • 3,009
  • 17
  • 27