title: Encrypt long String with RSA (Java)
body: <p>I'm having problems with an RSA application I have to do in Java.</p>
  
  <p>I have to read a string from a file, encrypt it, and then save the encrypted string in a new file.</p>
  
  <p>My RSA key is 1024 bits long.</p>
  
  <p>The code's part where the problem is, is the following:</p>
  
  <pre><code>            readBytes = in.read(bytesToBeEncoded, 0, bytesToBeEncoded.length);
  
              while(readBytes != -1){
                  encodedContent = ciph.update(bytesToBeEncoded, 0, readBytes);
                  out.write(encodedContent);
                  bytesToBeEncoded= new byte[(KEY_SIZE/8)-11];
                  readBytes = in.read(bytesToBeEncoded, 0, bytesToBeEncoded.length);                  
              }
  
              encodedContent = ciph.doFinal();
              out.write(encodedContent);
  </code></pre>
  
  <p>Where the variables are defined like this:</p>
  
  <pre><code>        byte[] bytesToBeEncoded = new byte[(KEY_SIZE/8)-11]; 
  
          FileInputStream in = new FileInputStream(new File(file1));
          FileOutputStream out = new FileOutputStream(new File(file2));
          int readBytes;
  </code></pre>
  
  <p>The point is that when I encrypt a less-than-117 bytes string, it works perfectly (encrypt and then decrypt well), but when the size is larger, the application throws this exception:</p>
  
  <pre><code>javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes
  </code></pre>
  
  <p>Thrown by:</p>
  
  <pre><code>encodedContent = ciph.doFinal();
  </code></pre>
  
  <p>I don't know where the problem is and what I have to do.</p>
  
  <p>Could anyone help me? Thank you.</p>
  
  <p>Sorry about my english.</p>