title: How to verify digital signature in C#
body: <p>I am new here.</p>
  
  <p>I am learning the digital signature in C#. The certificates are generated followed by <a href="https://docs.microsoft.com/en-us/azure/vpn-gateway/vpn-gateway-certificates-point-to-site" rel="nofollow noreferrer">this document</a>. Other documents I read: <a href="https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.rsacng?view=netcore-2.1#methods" rel="nofollow noreferrer">RSACng</a>,
  <a href="https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.x509certificates.x509certificate2?view=netcore-2.1#constructors" rel="nofollow noreferrer">X509Certificate2</a>.</p>
  
  <p>I am working on Windows 10 Pro 1809, .Net Core 2.1, VSCode.</p>
  
  <pre class="lang-cs prettyprint-override"><code>    class Program
      {
          static void Main(string[] args)
          {
              var passwd = "password";
  
              // Get client certificate.
              var clientCertPath = @"./Certificates/test.pfx";
              var clientCert = new X509Certificate2(clientCertPath, passwd);
  
              // Get server certificate.
              var serverCertPath = @"./Certificates/test.cer";
              var serverCert = new X509Certificate2(serverCertPath);
  
              // Generate data.
              var translateResultData = BuildData();
              var content = String.Join('&amp;', translateResultData.Select(p =&gt; String.Join('=', p.Key, p.Value)));
  
              // Sign
              var sign = SignatureUtil.Sign(data: content, clientCert: clientCert);
              // translateResultData.TryAdd(key: "sign", value : sign);
  
              // Copy content ONLY for test.
              var checkSign = sign;
              var checkContent = content;
  
              // Verify
              var valid = SignatureUtil.Verify(data: checkContent, signature: checkSign, serverCert: serverCert);
              System.Console.WriteLine(valid);
          }
  }
  </code></pre>
  
  <pre class="lang-cs prettyprint-override"><code>    public class SignatureUtil
      {
          public static string Sign(string data, X509Certificate2 clientCert)
          {
              using(var privateKey = clientCert.GetRSAPrivateKey())
              {
                  var dataByteArray = Encoding.UTF8.GetBytes(data);
                  var signatureByteArray = privateKey.SignData(
                      data: dataByteArray,
                      hashAlgorithm: HashAlgorithmName.SHA256,
                      padding: RSASignaturePadding.Pkcs1);
                  return Convert.ToBase64String(signatureByteArray);
              }
          }
  
          public static bool Verify(string data, string signature, X509Certificate2 serverCert)
          {
              try
              {
                  using(var publicKey = serverCert.GetRSAPublicKey())
                  {
                      var dataByteArray = Encoding.UTF8.GetBytes(data);
                      var signatureByteArray = Convert.FromBase64String(signature);
                      return publicKey.VerifyData(
                          data: dataByteArray,
                          signature: signatureByteArray,
                          hashAlgorithm: HashAlgorithmName.SHA256,
                          padding: RSASignaturePadding.Pkcs1);
                  }
              }
              catch (System.Exception)
              {
                  return false;
              }
          }
      }
  </code></pre>
  
  <p>Expected result: <code>valid</code> should be <code>true</code> because I am checking the original data.<br>
  Fact: The <code>Verify</code> method always returns <code>false</code> even the original data are passed. </p>
  
  <p>Can you tell me what I did wrong?</p>