title: Verifying PHP OpenSSL signature in C# with RSACryptoProvider
body: <p>I am attempting to verify an <em><code>OpenSSL</code></em> signature <code>(created using openssl_sign with SHA1 in PHP)</code> using C# <strong><code>RSACryptoProvider.VerifyData</code></strong>. It is returning false using the correct public key certificate.</p>
  
  <p>Any idea about how to do this successfully?</p>
  
  <p>EDIT:</p>
  
  <p>I attempted to verify the OpenSSL SHA1 signature using BouncyCastle with the following code but verification is failing. Are the signatures calculated differently? How can I create a signature with OpenSSL that is verifiable by .NET?</p>
  
  <pre><code>byte[] signatureBytes = UTF8Encoding.Default.GetBytes(signature);
    byte[] dataBytes = UTF8Encoding.Default.GetBytes(data);
  
    StreamReader sr = new StreamReader(Path.Combine(@"C:\test", @"test\test.crt"));
    PemReader pr = new PemReader(sr);
  
    Org.BouncyCastle.X509.X509Certificate cert = (Org.BouncyCastle.X509.X509Certificate)pr.ReadObject();
  
    ISigner sig = SignerUtilities.GetSigner("SHA1WithRSAEncryption");
    sig.Init(false, cert.GetPublicKey());
    sig.BlockUpdate(dataBytes, 0, dataBytes.Length);
    if (sig.VerifySignature(signatureBytes)) {
      Console.WriteLine("all good!");
    }
  </code></pre>
  
  <p>PHP Code:</p>
  
  <pre><code>function signTokenWithPrivateKey($message, $keyLocation) {
    try {
      if (file_exists($keyLocation)) {
        $privateKey= openssl_get_privatekey(file_get_contents($keyLocation));
  
        $signature = '';
        if (!openssl_sign($message, $signature, $privateKey)) {
          die('Failed to encrypt');
        }
  
        openssl_free_key($privateKey);
      }
    }
    catch (Exception $ex) {
  
    }
  
    return $signature;
  }
  </code></pre>