Using OpenSSL for encrytion in C#

0

I really need your help with OpenSSL! I have this code:

echo "Generating license key.."
echo "First sign the license"
openssl dgst -sha256 - passin pass: "$RSA_ENCRYPT_PASSPHRASE" - sign "$PRIVATE_KEY" -out "$SIGNATURE" "$LICENSE_CONTENT"
echo "Now, create a license package: license content + signature"
tar - cvf "$LICENSE_PACKAGE" "$LICENSE_CONTENT" "$SIGNATURE"
echo "Finally encrypt with symmetric key"
openssl aes-256 - cbc - pass file: "$SYMMETRIC_KEY" - a - e - salt - md md5 -in "$LICENSE_PACKAGE" -out "$LICENSE_KEY"
echo "Generated license key.."

This I'm trying to convert into C# but I don't exactly know what to do! There are several ways described in different forums and here, but I don't exactly know what to do.

Some just say I should use Process.Start and run the code in CMD. Is this really the best way? Like this:

string Path_RootKeys = @"C:\Temp\Keys";

string RSA_ENCRYPT_PASSPHRASE = "123456";

string PUBLIC_KEY = "public.pem";
string PRIVATE_KEY = "private.pem";
string SYMMETRIC_KEY = "symmetric";

string LICENSE_CONTENT = "license.json";
string LICENSE_KEY = "license.key";
string SIGNATURE = "signature.txt";
string LICENSE_PACKAGE = "/tmp/license.tar";

var process = new Process();
var psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.WorkingDirectory = Path_Root;
process.StartInfo = psi;
process.Start();
process.OutputDataReceived += (sender, e) => { Console.WriteLine(e.Data); };
process.ErrorDataReceived += (sender, e) => { Console.WriteLine(e.Data); };
process.BeginOutputReadLine();
process.BeginErrorReadLine();
using (StreamWriter sw = process.StandardInput)
{
    Signing the license
    string LicenseSigning = String.Format("openssl dgst -sha256 - passin pass: {0} - sign {1} -out {2} {3}", RSA_ENCRYPT_PASSPHRASE, PRIVATE_KEY, SIGNATURE, LICENSE_CONTENT);
    sw.WriteLine(LicenseSigning);
}
process.WaitForExit();

So far I tried this:

//https://csharp.hotexamples.com/de/examples/OpenSSL.Crypto/MessageDigestContext/Update/php-messagedigestcontext-update-method-examples.html
using (MessageDigestContext ctx = new MessageDigestContext(MessageDigest.SHA256))
{
      byte[] LicenseFile = File.ReadAllBytes(Path_RootKeys + @"\" + LICENSE_CONTENT);
}

But the MessageDigest.SHA256 is always throwing an exception with two inner exceptions:

System.TypeInitializationException: "Der Typeninitialisierer für "OpenSSL.Crypto.MessageDigest" hat eine Ausnahme verursacht."

TypeInitializationException: Der Typeninitialisierer für "OpenSSL.Core.Native" hat eine Ausnahme verursacht.

BadImageFormatException: Es wurde versucht, eine Datei mit einem falschen Format zu laden. (Ausnahme von HRESULT: 0x8007000B)

Can anybody help me with this?

Thank you very much!

c#
openssl
asked on Stack Overflow Jun 30, 2020 by SAR-71

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0