title: The data to be decrypted exceeds the maximum for this modulus of 128 bytes while encrypting data in Android and Decrypting in WCF Web Service
body: <p>I am an Android Developer. The task at hand is to encrypt data in my Android Application using the RSA algorithm via the Public Key modulus and Exponent provided to me and then send it over the network via JSON String to a .Net Web Service. This Web Service will decrypt the data and further use it.</p>
  
  <p>The code that I use to encrypt the data is as follows:</p>
  
  <pre><code>public String RSAEncrypt (final String plain) throws Exception 
  {
      try{
          String strModulus = "tr82UfeGetV7yBKcOPjFTWs7pHqqr/5YKKWMUZ/HG4HnCmWrZsOhuR1FBnMZ/g2YiosoSlu0zd7Ukz9lX7wv2RLfWXfMvZYGpAAvfYWwzbyQ2i1q+tKE/thgKNscoSRellDD+uJcYn1H4hnaudVyYJH9miVhOKhKlExMzw8an6U=";
          String strExponent = "AQAB";
          byte[] modulusBytes = strModulus.getBytes();
          byte[] exponentBytes = strExponent.getBytes();
          BigInteger modulus = new BigInteger(1, modulusBytes );               
          BigInteger exponent = new BigInteger(1, exponentBytes);
  
          RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulus, exponent);
          KeyFactory fact = KeyFactory.getInstance("RSA");
          PublicKey pubKey = fact.generatePublic(rsaPubKey);
  
          Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
          cipher.init(Cipher.ENCRYPT_MODE, pubKey);
  
          byte[] plainBytes = new String("Manchester United").getBytes("UTF-8");
          byte[] cipherData = cipher.doFinal(plainBytes);
          encryptedString = Base64.encodeToString(cipherData, Base64.NO_PADDING);
      }
      catch(Exception e){
          Log.e("Error", e.toString());
      }
  
      return encryptedString;
  }
  </code></pre>
  
  <p>The above code gives me a string that I pass to the .Net Web Service. But for some reason the Web Service is not being able to decrypt this data appropriately and presents the error:
  The data to be decrypted exceeds the maximum for this modulus of 128 bytes</p>
  
  <p>I tried to help out but I cannot understand what is happening.</p>
  
  <p>I made the following alterations in my code:
  Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); instead of 
  Cipher cipher = Cipher.getInstance("RSA");</p>
  
  <p>Also provided 'Base64.NO_PADDING' while encrypting the data.</p>
  
  <p>The ASP.Net code is as follows:</p>
  
  <pre><code>            const int PROVIDER_RSA_FULL = 1;
              const string CONTAINER_NAME = "Tracker";
  
              CspParameters cspParams;
              cspParams = new CspParameters(PROVIDER_RSA_FULL);
              cspParams.KeyContainerName = CONTAINER_NAME;
              RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider(cspParams);
              rsa1.FromXmlString("&lt;RSAKeyValue&gt;&lt;Modulus&gt;tr82UfeGetV7yBKcOPjFTWs7pHqqr/5YKKWMUZ/HG4HnCmWrZsOhuR1FBnMZ/g2YiosoSlu0zd7Ukz9lX7wv2RLfWXfMvZYGpAAvfYWwzbyQ2i1q+tKE/thgKNscoSRellDD+uJcYn1H4hnaudVyYJH9miVhOKhKlExMzw8an6U=&lt;/Modulus&gt;&lt;Exponent&gt;AQAB&lt;/Exponent&gt;&lt;P&gt;6e0nv7EBFBugtpoB+ozpg1J4js8E+DVyWCuBsERBPzqu4H7Z/oeLIRSC8Gi5GZgrCpBf3EvyIluM7rzaIfNThQ==&lt;/P&gt;&lt;Q&gt;x/29X9ns1WcXC42IJjLDjscz5ygdVh79dm8B2tQVbqwyhDsQ6OIOQdu5+eHf4hUMoTrM9KkS2F6FGlLXuaOFoQ==&lt;/Q&gt;&lt;DP&gt;kTS2LMaJ/dpce5zDx6w6s1q5HSSiWBSNIu/2s9zah448yXvUg6vNkD40PVk0NRAA/7C44H2AExWzOOqfmN17JQ==&lt;/DP&gt;&lt;DQ&gt;xtAx9drQPWnpl/uQUOEAVa0kpPTVDStrr9Q1FNTnpYkcAyYw7kLkB4anAIoSpk9kqdeprsNxz5VPXtbiTFMKYQ==&lt;/DQ&gt;&lt;InverseQ&gt;O0594NMjnjSp+/NAa1kQxoQNzn1qqq+p1Zb+kT3/jRc/0d7ZnqSSpxFMXfxx3yZkNAOPDOdbckPQbRZ13RKBHg==&lt;/InverseQ&gt;&lt;D&gt;bjVEagwvkrZaTt9CTW1hd3362weLFlX6DpE/3R3RcrpVfkSwKGpEhqGrNeeGPlsuqiaf5rAFir4eTqrF1QVliKsU4XE0RyzP5lHGc7dlX4DOHMjs2R9nNWv8QOTPoaRuLrLGorqBXlw/jQPxFI6gQzkIIjzuf//lDVnFam3dw4E=&lt;/D&gt;&lt;/RSAKeyValue&gt;");
  
              string data2Decrypt = "SeuUUjdSbFQlcwaCFVZ9fN0t3aXpItXU4+pkuztVgky77SBokBATKj+56+irtCfT1lSGRZbbzgQTd8zpcjPsT6J+7AyBwRwuv418JyKINNxPsDVFKfupViu8MOoxWzjmjZE5p54AjByA6dzGSR9UogFHhFSMZWJ5OELwZFSz5wFHyMaeIm1UnEiRDDjPTY/aIuh56WnZPXUmP3D54bAwDXPFtY0JjjiyxZetnA";
  
              byte[] encyrptedBytes = Convert.FromBase64String(data2Decrypt);
  
              byte[] plain = rsa1.Decrypt(encyrptedBytes, false);
              string decryptedString = System.Text.Encoding.UTF8.GetString(plain);
  </code></pre>
  
  <p>The error occurs here:
  byte[] plain = rsa1.Decrypt(encyrptedBytes, false);</p>
  
  <p>Decrypt is a function of the RSACryptoProvider class in .Net itself. </p>
  
  <p>I have checked a few links like:
  <a href="https://stackoverflow.com/questions/21604819/the-data-to-be-decrypted-exceeds-the-maximum-for-this-modulus-of-128-bytes-rsa">The data to be decrypted exceeds the maximum for this modulus of 128 bytes. RSA DECRYPTION c#</a></p>
  
  <p><a href="https://stackoverflow.com/questions/18783737/the-data-to-be-decrypted-exceeds-the-maximum-for-this-modulus-of-128-bytes">The data to be decrypted exceeds the maximum for this modulus of 128 bytes</a></p>
  
  <p>However, I am not being able to implement the suggestions. Any help will be appreciated. Thank you!</p>