Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何移植php';s openssl#U签名到C#?_C#_Php - Fatal编程技术网

如何移植php';s openssl#U签名到C#?

如何移植php';s openssl#U签名到C#?,c#,php,C#,Php,我需要将下面的代码从php翻译成C。我需要使用哪些库/名称空间 function sign($method, $uuid, $data) { $merchant_private_key = openssl_get_privatekey(file_get_contents('merchant_private_key.pem')); $plaintext = $method . $uuid . serialize_data($data); **openssl_sign($pl

我需要将下面的代码从php翻译成C。我需要使用哪些库/名称空间

function sign($method, $uuid, $data) {
    $merchant_private_key = openssl_get_privatekey(file_get_contents('merchant_private_key.pem'));
    $plaintext = $method . $uuid . serialize_data($data);
    **openssl_sign($plaintext, $signature, $merchant_private_key);**
    return base64_encode($signature);
}
api可在上找到。我从哪里开始?我不知道php中的open_ssl登录在封面后面做了什么

任何一个既懂php又懂C#的人,或者至少有人能解释一下openssl#u sign在后台做了什么,这样我就可以移植它了


编辑:2010-08-18我找不到使用openssl的方法,它一直说它无法加载managedopenssl.dll。我想这是因为我的机器是x64

这里的困难在于读取和解码私钥。你可以用这个。引用后,您可以尝试以下操作:

public static string Sign(string method, string uuid, string data)
{
    string merchantPrivateKey = File.ReadAllText("merchant_private_key.pem");
    byte[] der = opensslkey.DecodePkcs8PrivateKey(merchantPrivateKey);
    using (RSACryptoServiceProvider rsa = opensslkey.DecodePrivateKeyInfo(der))
    using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
    {
        // This will contain the data to sign
        byte[] dataToSign = Encoding.Default.GetBytes(method + uuid + data);
        byte[] signature = rsa.SignData(dataToSign, md5);
        return Convert.ToBase64String(signature);
    }
}

在本例中,我使用了MD5,但您可以使用适合您的场景的任何东西。我认为默认情况下使用SHA1。

谢谢您的建议


我已经找到了解决问题的办法。我们将使用一个名为的图书馆。找到了答案。不幸的是,这在我的x64机器上不起作用。幸运的是,我们的服务器仍然都是x86,因此我们有更多的时间研究解决方案。

谢谢您的建议!不幸的是,我遇到了openssl的问题,我认为这与我的机器是x64有关。也找不到适用于x64的任何openssl.net。@CatZ:您可以通过编译选项强制应用程序以32位模式运行。这应该允许它运行32位DLL。@CatZ:或者,在这个页面上有OpenSSL的Win64版本:@R.Bemrose:是的,这就是desco的链接吸引我的地方。