当前的php函数如何解释为c#?

时间:2018-09-18 08:24:31

标签: c# php openssl

我有php func,

private function signData($data) {
$descriptorspec = array(
    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
);
$descriptorspec[2] = $descriptorspec[1];
try {
    $opensslCommand = 'openssl smime -sign -signer ' . $this->settings->mws_cert .
        ' -inkey ' . $this->settings->mws_private_key .
        ' -nochain -nocerts -outform PEM -nodetach -passin pass:'.$this->settings->mws_cert_password;

    $process = proc_open($opensslCommand, $descriptorspec, $pipes);
    if (is_resource($process)) {
        fwrite($pipes[0], $data);
        fclose($pipes[0]);
        $pkcs7 = stream_get_contents($pipes[1]);            
        fclose($pipes[1]);
        $resCode = proc_close($process);
        if ($resCode != 0) {
            $errorMsg = 'OpenSSL call failed:' . $resCode . '\n' . $pkcs7;                
            throw new \Exception($errorMsg);
        }
        return $pkcs7;
    }
} catch (\Exception $e) {        
    throw $e;
}}

我对如何在c#中运行$ opensslCommand命令感兴趣。

我的代码c#:

var startInfo = new ProcessStartInfo(@"C:\OpenSSL\bin\openssl.exe", 
@"smime -sign -signer d:\smime.p7s -inkey d:\private.key -nochain -nocerts -outform PEM -nodetach -passin pass:my_pass");
Process.Start(startInfo);

但是如何发送此变量 $ descriptorspec
以及如何在c#中执行此代码:

fwrite($pipes[0], $data);
$pkcs7 = stream_get_contents($pipes[1]);

1 个答案:

答案 0 :(得分:0)

我找到了解决方法。

private bool TryExecuteOpenSslCommand(string pathApp, string arguments, string content, out string response)
{
    Process process = null;
    var status = false;

    try
    {
        var psi = new ProcessStartInfo(pathApp)
        {
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            WindowStyle = ProcessWindowStyle.Hidden,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        Process.Start(psi);
        psi.Arguments = arguments;

        process = Process.Start(psi);
        if (process == null)
            throw new Exception("Process is not started");

        var myWriter = process.StandardInput;
        var myOutput = process.StandardOutput;
        var myErrors = process.StandardError;

        myWriter.AutoFlush = true;
        myWriter.Write(content);
        myWriter.Close();

        var result = myOutput.ReadToEnd();
        var errors = myErrors.ReadToEnd();

        response = string.IsNullOrEmpty(result) ? errors : result;
        status = true;
    }
    catch (Exception ex)
    {
        response = ex.ToString();
    }
    finally
    {
        process?.Close();
    }

    return status;
}

执行功能:

var openSslApplicationPath = @"c:\openssl\bin\openssl.exe";
var arguments = "smime -sign -signer ./cert.cer -inkey ./private.key -nochain -nocerts -outform PEM -nodetach -passin pass:private_key_pass";
var content = "my_variable_text";

var result = TryExecuteOpenSslCommand(openSslApplicationPath, arguments, content, out var response);

这很有用...