简体   繁体   中英

Execute multiple command prompt commands from c#

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.

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");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM