title: Rsa C# Encryption and java decryption
body: <p>I am building two applications, one in C# and one in Java, both will use RSA for encryption and decryption.</p>
  
  <p>when I encrypt using JAVA and decrypt  using C# it works flawlessly, the problem occurs when the encryption is done on the C# app and decryption in Java which throws this error:</p>
  
  <pre><code>javax.crypto.IllegalBlockSizeException: Data must not be longer than 64 bytes
  </code></pre>
  
  <p>I'm using RSA with 512 bit RSA to create the keys in both sides, increasing to 1048 changes the error indicating that the size can not be longer than 128 bytes.</p>
  
  <p>Java:</p>
  
  <pre><code>public String decrypt(String message, RSAPrivateKey key) {
      try {
  
          Cipher cipher = Cipher.getInstance("RSA");
          cipher.init(Cipher.DECRYPT_MODE, key);
  
          byte[] dataEncripted =Base64.decodeBase64(message.getBytes());
          byte[] data = cipher.doFinal(dataEncripted);
          return new String(data,"UTF-8");
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>C#:</p>
  
  <pre><code>public static string Encrypt(string data, string publicKey)
  {
      var sb = string.Empty;
      try
      {
          var rsa = new RSACryptoServiceProvider();
          rsa.FromXmlString(publicKey);
          byte[] encryptedByteArray;
          byte[] dataToEncrypt;
  
          dataToEncrypt = Encoding.ASCII.GetBytes(data);
          encryptedByteArray = rsa.Encrypt(dataToEncrypt, false);
          sb = Convert.ToBase64String(encryptedByteArray);
      }
  }
  </code></pre>
  
  <p>What I have noticed is that the process that works, JAVA --> C#, when encrypted, the array goes from 0 to 63 (64 bytes), but in the other process C# --> JAVA, the encrypted data goes from 0 to 64 (65 bytes)</p>
  
  <p>Here a image to show in java after decode the arrayBytes number :</p>
  
  <p><a href="http://i.stack.imgur.com/ARUWc.jpg" rel="nofollow">Bytes java</a></p>
  
  <p>How can I make the RSA C# provider to generate an array of bytes equal to the one in Java? or any insight on how to solve this?</p>