title: RSA Encryption and Decryption in C#.NET
body: <p>I have below code to encrypt and decrypt the message in c#. when i am trying to run it is giving an exception ie  "The data to be decrypted exceeds the maximum for this modulus of 256 bytes"</p>
  
  <pre><code> public static void Main(string[] args)
      {
          X509Certificate2 cert = new X509Certificate2(@"C:\Data\ABC-rsa-public-key-certificate.cer");
          string encryptedText = EncrypIt("Hello", cert);
          string decryptedText = DecrptIt(encryptedText, cert);
          System.Console.WriteLine(decryptedText);
  
  
      }
  
      public static string EncrypIt(string inputString, X509Certificate2 cert)
      {
          RSACryptoServiceProvider publicKey = (RSACryptoServiceProvider)cert.PublicKey.Key;
          byte[] plainBytes = Encoding.UTF8.GetBytes(inputString);
          byte[] encryptedBytes = publicKey.Encrypt(plainBytes, false);
          string encryptedText = Encoding.UTF8.GetString(encryptedBytes);
          return encryptedText;      
      }
  
      public static string DecrptIt(string encryptedText, X509Certificate2 cert)
     {
         RSACryptoServiceProvider privateKey = (RSACryptoServiceProvider)cert.PublicKey.Key;
         byte[] encryptedBytes = Encoding.UTF8.GetBytes(encryptedText);
         byte[] decryptedBytes = privateKey.Decrypt(encryptedBytes, false);
         string decryptedText = Encoding.UTF8.GetString(decryptedBytes);
         return decryptedText;
     }
  </code></pre>