Reputation: 28079
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.
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();
Upvotes: 0
Views: 783
Reputation: 3029
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");
Why not to use http://sourceforge.net/projects/openssl-net/ ? It's a openssl wrapper to .NET.
Upvotes: 1
Reputation: 11438
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");
Upvotes: 2