title: DES algorithm in java
body: <p>I'm trying to create a program where you can encrypt/decrypt text and file with DES algorithm in Java. The file decryption part is not working and I'm getting this exception : &quot;javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher&quot;.
  Can anybody help me?</p>
  <pre><code>
  
  import javax.crypto.*;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileWriter;
  import java.nio.charset.StandardCharsets;
  import java.security.InvalidKeyException;
  import java.security.Key;
  import java.security.NoSuchAlgorithmException;
  import java.util.Base64;
  import java.util.Scanner;
  
  public class Main {
     public static void main(String[] args) {
         try {
             Scanner input = new Scanner(System.in);
             KeyGenerator desKeyGenerator = KeyGenerator.getInstance(&quot;DES&quot;);
             Cipher desCipherEncrypter = Cipher.getInstance(&quot;DES&quot;);
             Cipher desCipherDecrypter = Cipher.getInstance(&quot;DES&quot;);
  
             SecretKey key = desKeyGenerator.generateKey();
             desCipherEncrypter.init(Cipher.ENCRYPT_MODE, key);
             desCipherDecrypter.init(Cipher.DECRYPT_MODE, key);
  
             System.out.print(&quot;Plaintext: &quot;);
             String message = input.nextLine();
             String encrypted = encrypt(desCipherEncrypter, message);
             System.out.println(&quot;Encrypted text: &quot; + encrypted);
             String decrypted = decrypt(desCipherDecrypter, encrypted);
             System.out.println(&quot;Decrypted text: &quot; + decrypted);
             
  
             System.out.print(&quot;The file you want to encrypt: &quot;);
             String path = input.nextLine();
             System.out.print(&quot;The file where you want to save the encrypted one: &quot;);
             String fileenc = input.nextLine();
             fileencrypt(desCipherEncrypter,path,fileenc);
             System.out.print(&quot;The file where you want to save the decrypted one: &quot;);
             String filedec = input.nextLine();
             filedecrypt(desCipherDecrypter, fileenc,filedec );
  
         }
         catch (NoSuchAlgorithmException noSuchAlgorithmException) {
             noSuchAlgorithmException.printStackTrace();
         } catch (InvalidKeyException invalidKeyException) {
             invalidKeyException.printStackTrace();
         } catch (NoSuchPaddingException noSuchPaddingException) {
             noSuchPaddingException.printStackTrace();
         } catch (BadPaddingException badPaddingException) {
             badPaddingException.printStackTrace();
         } catch (IllegalBlockSizeException illegalBlockSizeException) {
             illegalBlockSizeException.printStackTrace();
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         } catch (Exception e) {
             e.printStackTrace();
         }
  
     }
  
     public static void fileencrypt(Cipher cipher,String path,String encryptedfile) throws FileNotFoundException,Exception{
         Scanner inputi=new Scanner(System.in);
         Scanner finput = null;
         File file = new File(path);
         if (!file.exists()) throw new FileNotFoundException();
         if (!file.canRead()) throw new Exception(&quot;Cannot read &quot; + path);
         if (!file.canWrite()) throw new Exception(&quot;Cannot write &quot; + path);
         finput = new Scanner(file);
         FileWriter write =new FileWriter(encryptedfile);
         while (finput.hasNext()) {
             String str = finput.nextLine();
             String encrypt = encrypt(cipher, str);
             write.write(encrypt);
             write.write(&quot;\n&quot;);
         }
         System.out.println(&quot;Encrypted text&quot;);
         write.close();
     }
     public static void filedecrypt(Cipher cipher,String filetodecrypt,String dencryptedfile) throws FileNotFoundException,Exception{
         Scanner dinput=null;
         FileWriter write1 =new FileWriter(dencryptedfile);
         File filefordecrypt=new File(filetodecrypt);
         dinput=new Scanner(filetodecrypt);
         while (dinput.hasNext()) {
             String str = dinput.nextLine();
             String decrypt = decrypt(cipher, str);
             write1.write(decrypt + &quot; &quot;);
         }
         write1.close();
         System.out.println(&quot;Decrypted text&quot;);
     }
     public static String encrypt(Cipher cipher, String plainStr) throws BadPaddingException,IllegalBlockSizeException
     {
         byte[] plainBytes;
         byte[] encryptedBytes;
         plainBytes=plainStr.getBytes(StandardCharsets.ISO_8859_1);
         encryptedBytes=cipher.doFinal(plainBytes);
         return Base64.getEncoder().encodeToString(encryptedBytes);
     }
  
     public static String decrypt(Cipher cipher,String encryptedStr) throws BadPaddingException,IllegalBlockSizeException
     {
         byte[] encryptedBytes=Base64.getDecoder().decode(encryptedStr);
         byte[] decryptedBytes=cipher.doFinal(encryptedBytes);
         return new String(decryptedBytes,StandardCharsets.ISO_8859_1);
     }
  
  
  } ```
  </code></pre>