简体   繁体   English

从 C# 执行多个命令提示符命令

[英]Execute multiple command prompt commands from c#

I'm trying to generate a private key with OpenSSL from c#.我正在尝试从 c# 使用 OpenSSL 生成私钥。 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.我提到我想在私钥上使用 aes256 加密,为了实现这一点,它需要一个密码,所以,我的问题是我不知道如何将该密码传输到进程,我的意思是我创建了在此过程中,我运行使用 aes256 加密的 OpenSSL 创建密钥的命令,并且我必须输入密码和确认密码。

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.所以,我不知道如何发送私钥生成所需的密码......我的意思是,如果我在 cmd/openssl(例如:openssl genrsa -aes256)中运行该命令,它会生成 RSA 私钥,并且之后它要求我介绍密码短语和密码短语的验证。 So, if I want to do this from c#, how can I do it?所以,如果我想从 C# 中做到这一点,我该怎么做? 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");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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