title: Error occurred while decoding OAEP Padding C#
body: <p>Below is my code and error is in the Title. I encrypted using private key but I decrypt using the same private key I ran into the error mentioned in the title, not really sure what is the underlying cause of the error. Any help in that regard will be highly appreciated.</p>
  
  <pre><code>Class Program
  {
      static void Main(string[] args)
      {
         string thumbPrint = "SomeValue";
         EncryptUserName("Steve", thumbPrint, true, true);
      }
      public static void EncryptUserName(string textToEncript, string certificateThumbprint, bool searchLocalMachine, bool searchUser)
      {
          X509Certificate2 cert = FindCertificate(certificateThumbprint, searchLocalMachine, searchUser);
          RSACryptoServiceProvider rsaEncryptor = (RSACryptoServiceProvider)cert.PublicKey.Key;
          byte[] cipherData = rsaEncryptor.Encrypt(Encoding.UTF8.GetBytes(textToEncript), true);
          var encryptedString  = Convert.ToBase64String(cipherData);
          Console.WriteLine(encryptedString);
      }
      public static byte[] Decrypt(byte[] encryptedData, bool fOAEP, X509Certificate2 certificate)
      {
          if (encryptedData == null)
          {
              throw new ArgumentNullException("encryptedData");
          }
          if (certificate == null)
          {
              throw new ArgumentNullException("certificate");
          }
          if (certificate.PrivateKey == null)
          {
              throw new ApplicationException("Certificate provided has no private key");
          }
          Console.WriteLine(certificate.PrivateKey);
          using (RSACryptoServiceProvider provider = (RSACryptoServiceProvider)certificate.PrivateKey)
          {
              return provider.Decrypt(encryptedData, fOAEP);
          }
      }
  
      public static string CertificateDecrypt(string textToDecript, string certificateThumbprint, bool searchLocalMachine, bool searchUser)
      {
          X509Certificate2 certificate = FindCertificate(certificateThumbprint, searchLocalMachine, searchUser);
          byte[] bytesArray = Convert.FromBase64String(textToDecript);
          //Decrypt(bytesArray, true, certificate);
          return Encoding.UTF8.GetString(Decrypt(bytesArray, true, certificate));
      }
      public static X509Certificate2 LoadCertificate(StoreName storeName, StoreLocation storeLocation, string thumbprint)
      {
          X509Store store = null;
          X509Certificate2 certificate2;
          try
          {
              store = new X509Store(storeName, storeLocation);
              store.Open(OpenFlags.ReadOnly);
              X509Certificate2Enumerator enumerator = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false).GetEnumerator();
              X509Certificate2 current = null;
              while (enumerator.MoveNext())
              {
                  current = enumerator.Current;
              }
              certificate2 = current;
          }
          finally
          {
              if (store != null)
              {
                  store.Close();
              }
          }
          return certificate2;
      }
      private static X509Certificate2 FindCertificate(string certificateThumbprint, bool searchLocalMachine, bool searchUser)
      {
          certificateThumbprint = certificateThumbprint.Replace(" ", "");
          X509Certificate2 certificate = null;
          if (searchUser)
          {
              certificate = LoadCertificate(StoreName.My, StoreLocation.CurrentUser, certificateThumbprint);
          }
          if (searchLocalMachine &amp;&amp; (certificate == null))
          {
              certificate = LoadCertificate(StoreName.My, StoreLocation.LocalMachine, certificateThumbprint);
          }
          if (certificate == null)
          {
              throw new ApplicationException($"Certificate with thumbprint {certificateThumbprint} cannot be loaded (not found)");
          }
          return certificate;
      }
  }
  </code></pre>