Generate Openssl signature

78 views Asked by At

I'm trying to generate a signature file using my private key for a source file. the command works fine when I run it in command prompt. But when I try to do the same through the code, signature is not generating.

This is the code I'm using

    static void CreateOpenSSLSignature(string sourceFile, string sslPrivateKeyPath)
    {
        // Get required values
        if (File.Exists(signatureFilepath))
        {
            File.Delete(signatureFilepath);
        }
        var sigFile = File.Create(signatureFilepath);
        sigFile.Close();
        try
        {
            // Start decryption process
            Process myProcess = new Process();
            myProcess.StartInfo.FileName = openSSLPath + "\\bin\\openssl.exe";
            myProcess.StartInfo.WorkingDirectory = openSSLPath + "/bin/";
            myProcess.StartInfo.RedirectStandardInput = true;
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.RedirectStandardOutput = true;
            myProcess.StartInfo.CreateNoWindow = true;
            myProcess.StartInfo.RedirectStandardError = true;
            myProcess.StartInfo.ErrorDialog = true;
            string arg = dgst -sha1 -sign sslPrivateKeyPath -binary sourcefilepath | openssl enc -base64             
            >> signaturefilepath
            myProcess.StartInfo.Arguments = arg.ToString();
            myProcess.Start();
            myProcess.WaitForExit(10000);            }
        catch (Exception ex)
        {
        }
    }
1

There are 1 answers

6
Ramin Alirezaee On

check this way. "set your openssl path":

string openSSlPath = "C:\\Program Files\\Git\\mingw64\\bin";
try
{
    Process obj = new Process
    {
        StartInfo =
                {
                    FileName = openSSlPath + "\\openssl.exe",
                    WorkingDirectory = (openSSlPath ?? "")
                }
    };
    ProcessStartInfo startInfo = obj.StartInfo;
    DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(115, 2);
    defaultInterpolatedStringHandler.AppendLiteral("dgst -sha1 -sign ");
    defaultInterpolatedStringHandler.AppendFormatted($"{sslPrivateKeyPath}");
    defaultInterpolatedStringHandler.AppendLiteral(" -binary ");
    defaultInterpolatedStringHandler.AppendFormatted($"{sourcefilepath}");
    defaultInterpolatedStringHandler.AppendLiteral(" | openssl enc -base64 >> ");
    defaultInterpolatedStringHandler.AppendFormatted($"{signaturefilepath}");
    startInfo.Arguments = defaultInterpolatedStringHandler.ToStringAndClear();
    obj.Start();
    obj.WaitForExit();
}
catch (Exception ex)
{
    Console.WriteLine(ex.ToString());
}