title: encrypting and decryption large file using rsa in java
body: <p>I am using RSA algorithm for encryption and decryption of a file with size more than rsa key size. </p>
  
  <p>In the code below for encryption, i am reading file content in block-wise and converting into cipher text. Block-size is 32 bytes.</p>
  
  <pre><code>FileInputStream fin1 = new FileInputStream(genfile);
  
  FileOutputStream fout = new FileOutputStream(seedcipher);
  
  byte[] block = new byte[32];
  int i;
  while ((i = fin1.read(block)) != -1)
  {
      byte[] inputfile= cipher.doFinal(block);
      fout.write(inputfile);
  }
  
  fin1.close();
  </code></pre>
  
  <p>At decryption part, same block-wise decryption is done in the code where i have mentioned the block size as 128 bytes</p>
  
  <pre><code>FileInputStream fin1 = new FileInputStream(encryptedfile);
  FileOutputStream fout = new FileOutputStream(seedcipher);
  
  DataInputStream dos =new DataInputStream(fin1);
  DataOutputStream dosnew =new DataOutputStream(fout);
  byte[] block = new byte[128];
  int i;
  while ((i = fin1.read(block)) != -1)
  {
      byte[] inputfile= cipher.doFinal(block);
        fout.write(inputfile);
  }
  </code></pre>
  
  <p>Input file size is 81.3 kB and file contains</p>
  
  <pre>
  0
  1
  2
  3
  4.....29000 
  </pre>
  
  <p>After the file is decrypted,output contain some extra values which are not relevant. why is that extra data in the result?</p>