title: How to properly verify data with rsa?
body: <p>I want to sign a message with a private key and verify it with a public key, but I can't get it to work..</p>
  
  <p>Here is how I sign the data (edited, but still not working):</p>
  
  <pre><code>public static string SignData(string message, string privateKey) {
  
          byte[] plainText = ASCIIEncoding.Unicode.GetBytes(message);
  
          var rsaWrite = new RSACryptoServiceProvider();
          rsaWrite.FromXmlString(privateKey);
  
          byte[] signature = rsaWrite.SignData(plainText, new SHA1CryptoServiceProvider());
  
          return Convert.ToBase64String(signature);
      }
  </code></pre>
  
  <p>Here is how I test the data (edited, still not working):</p>
  
  <pre><code>public static bool VerifyData(string sign, string publicKey, string orig) {
  
          byte[] signature = Convert.FromBase64String(sign);
          byte[] original = ASCIIEncoding.Unicode.GetBytes(orig);
  
          var rsaRead = new RSACryptoServiceProvider();
          rsaRead.FromXmlString(publicKey);
  
          if (rsaRead.VerifyData(original, new SHA1CryptoServiceProvider(), signature)) {
              return true;
          } else {
              return false;
          }
      }
  </code></pre>
  
  <p>I store the keypair as an xml string inside my account class. This function is executed in the constructor of account.cs:</p>
  
  <pre><code>public void addKeys() {
  
      RSACryptoServiceProvider provider = new RSACryptoServiceProvider(1024);
  
      privateKey = provider.ToXmlString(true);
      publicKey = provider.ToXmlString(false);
  }
  </code></pre>
  
  <p>I test the overall thing with this:</p>
  
  <pre><code>string signedHash = Utility.SignData("test" ,account.privateKey);
  
  if (Utility.VerifyData(signedHash, account.publicKey, "test")) {          
      Console.WriteLine("WORKING!");
  } else {
      Console.WriteLine("SIGNING NOT WORKING");
  }
  </code></pre>
  
  <p>Why isn't the overall thing working? My guess is that it doesn't work because of some encoding stuff.</p>