3

I'm trying to generate a private key with OpenSSL from c#. In order to do this, I've tried to redirect process standard input, but I still have some problems. I mention that I want to use aes256 encryption over the private key, and for achieving that, it needs a password, so, my problem is that I don't know how to transmit that password to the process, I mean that I create the process, I run the command for creating the key with OpenSSL with aes256 encryption and further I have to enter the password and confirmation password.

This is my code:

Process cmd1 = new Process();
cmd1.StartInfo.FileName = "cmd.exe";
cmd1.StartInfo.RedirectStandardInput = true;
cmd1.StartInfo.RedirectStandardOutput = true;
cmd1.StartInfo.CreateNoWindow = true;
cmd1.StartInfo.UseShellExecute = false;
cmd1.StartInfo.WorkingDirectory = @"C:\usr\local\ssl\bin";

cmd1.Start();

cmd1.StandardInput.WriteLine("openssl genrsa -aes256 -out E:\\testing_folder\\urian_test7.com.key.pem 2048");
cmd1.StandardInput.Flush();
cmd1.StandardInput.WriteLine("123456");
cmd1.StandardInput.Flush();
cmd1.StandardInput.WriteLine("123456");
cmd1.StandardInput.Flush();
cmd1.StandardInput.Close();

Console.WriteLine("We're done! You got the key!");
cmd1.Close();

So, I don't know how to send the password required by the private key generetion...I mean that if I run that command in cmd/openssl(eg: openssl genrsa -aes256), it generates the RSA private key, and after that it asks me to introduce a pass phrase and a verification of pass phrase. So, if I want to do this from c#, how can I do it? Because I don't know how can I transmit that pass phrase. That's what I mean.

Paul
  • 79
  • 1
  • 8
  • 1
    These seem relevant: [Run Command Prompt Commands](http://stackoverflow.com/q/1469764) and [How To: Execute command line in C#](http://stackoverflow.com/q/206323). – jww Oct 29 '16 at 09:27
  • 2
    [This](http://stackoverflow.com/questions/437419/execute-multiple-command-lines-with-the-same-process-using-net#437444) will work. It execute multiple command lines with the same process. – Keyur Ramoliya Oct 29 '16 at 09:35
  • using (StreamWriter sw = cmd1.StandardInput) { if(sw.BaseStream.CanWrite) { sw.WriteLine("openssl genrsa -aes256 -out E:\\testing_folder\\urian_test_final4.com.key.pem 2048"); sw.WriteLine("123456"); sw.WriteLine("123456"); } } – Paul Oct 29 '16 at 09:52
  • But it doesn't work, it simply creates the file, but an empty file. – Paul Oct 29 '16 at 09:53
  • @Paul - Please add additional information, like non-trivial code, to your question. Do so by by clicking *Edit* in the lower left hand corner near the tags like ***C#*** and ***Command Prompt***. – jww Oct 29 '16 at 11:30
  • I did it. If you don't understand something, please let me know. Thank you! – Paul Oct 29 '16 at 11:59

1 Answers1

3

You can try:

    Process cmd = new Process();

    cmd.StartInfo.FileName = "cmd.exe";
    cmd.StartInfo.RedirectStandardInput = true;
    cmd.StartInfo.UseShellExecute = false;

    cmd.Start();

    using (StreamWriter sw = cmd.StandardInput)
    {
        if (sw.BaseStream.CanWrite)
        {
            sw.WriteLine("openssl genrsa -aes256 -out E:\\testing_folder\\test_file_com.key.pem 2048");
            sw.WriteLine("123456");
            sw.WriteLine("123456");
        }
    }
Marius Orha
  • 670
  • 1
  • 9
  • 26