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.

6
  • 1
    These seem relevant: Run Command Prompt Commands and How To: Execute command line in C#. Commented Oct 29, 2016 at 9:27
  • 2
    This will work. It execute multiple command lines with the same process. Commented Oct 29, 2016 at 9: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"); } } Commented Oct 29, 2016 at 9:52
  • But it doesn't work, it simply creates the file, but an empty file. Commented Oct 29, 2016 at 9: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. Commented Oct 29, 2016 at 11:30

1 Answer 1

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");
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.