title: Encrypt and decrypt contents of a text file (Java)
body: <p>I'm looking for some help with my program here. This program can encrypt and decrypt <strong>a phrase</strong> using DES symmetric cipher, based on CBC mode of operation.</p>
  
  <p>What I am trying to do now is it to change it such that it can encrypt and decrypt <strong>the content of a text file</strong> using DES symmetric cipher using CBC mode of operation. </p>
  
  <p>Can anyone help me out with this please?</p>
  
  <p>Thank you!</p>
  
  <pre><code>import java.security.*;
  
  import javax.crypto.*;
  
  import java.security.spec.*;
  
  import javax.crypto.spec.*;
  
  import javax.crypto.spec.IvParameterSpec;
  
  
  
  public class myProgram
  {
    public static void main (String[] args) throws Exception
    {
  
      String text = "Hello World;
  
      SecureRandom sr = new SecureRandom();
      byte [] iv = new byte[8];
      sr.nextBytes(iv);
      IvParameterSpec IV = new IvParameterSpec(iv);
      KeyGenerator kg = KeyGenerator.getInstance("DES");
      Key mykey = kg.generateKey();
      Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
      cipher.init(Cipher.ENCRYPT_MODE, mykey,IV);
  
      byte[] plaintext = text.getBytes("UTF8");
  
  
  
      byte[] ciphertext = cipher.doFinal(plaintext);
  
      System.out.println("\n\nCiphertext: ");
      for (int i=0;i&lt;ciphertext.length;i++) {
  
          if (chkEight(i)) {
              System.out.print("\n");
          }
          System.out.print(ciphertext[i]+" ");
      }
  </code></pre>