2 Replies Latest reply: Jan 8, 2010 12:34 PM by 843810 RSS

    javax.crypto.IllegalBlockSizeException help?

    843810
      I am making a plain text encrypt/decrypt program for personal use.
      It will compile just fine, but if I try to run the decryption part, it freaks out on me.

      It keeps on saying:
      javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher

      This is my code. I cannot figure out what is wrong...
      import java.io.*;
      import java.util.*;
      import java.security.*;
      import javax.crypto.*;
      import javax.crypto.spec.SecretKeySpec;
      import javax.crypto.spec.IvParameterSpec;
      import static methods.methods.*;
      public class Base
      {
          public static void main()
          {
              Scanner kbReader = new Scanner(System.in);
              
              clear("Method > ");
              String s = kbReader.next();
              if(s.equals("output"))
              {
                  try{output();}
                  catch(Exception e){e.printStackTrace();}
              }
              if(s.equals("input"))
              {
                  try{input();}
                  catch(Exception e){e.printStackTrace();}
              }
              if(s.equals("genKey"))
              {
                  genKey();
              }
              if(s.equals("x"))
              {
                  clear("Well it stopped.");
              }
              else
              {
                  main();
              }
          }
          public static void output()
              throws IOException,
              NoSuchAlgorithmException,
              NoSuchPaddingException,
              InvalidKeyException,
              IllegalBlockSizeException,
              BadPaddingException,
              InvalidAlgorithmParameterException
          {
              Scanner kbReader = new Scanner(System.in);
              Scanner kbr = new Scanner(System.in);
              clear("What is the name of the file?\nName > ");
              fileName = kbReader.next();
              String s2 = "/Users/ewsmith/base/" + fileName;
              FileWriter fw = new FileWriter(s2, true);
              PrintWriter output = new PrintWriter(fw, true);
              
              clear("Key > ");
              byte key[] = new byte[16];
              for(int j=0; j<16; j++)
              {
                  key[j] = kbReader.nextByte();
              }
              
              clear("IV > ");
              byte ivBytes[] = new byte[16];
              for(int j=0; j<16; j++)
              {
                  ivBytes[j] = kbReader.nextByte();
              }
              IvParameterSpec iv = new IvParameterSpec(ivBytes);
              
              String s = new String("");
              String ss = new String("");
              clear("Enter what you want output to a file.\nType 'x' to finish writing.\n\n");
              while(true)
              {
                  s = kbReader.nextLine();
                  if(s.equalsIgnoreCase("x"))
                  {
                      break;
                  }
                  ss = ss + s + "\n";
              }
              byte data[] = ss.getBytes("UTF-8");
              Cipher c = Cipher.getInstance("AES/CBC/PKCS5PADDING");
              SecretKeySpec k = new SecretKeySpec(key, "AES");
              c.init(Cipher.ENCRYPT_MODE, k, iv);
              c.doFinal(data);
              for(int j=0; j<data.length; j++)
              {
                  output.print(data[j] + " ");
              }
              clear("This is your key.\n\n");
              for(int j=0; j<key.length; j++)
              System.out.print(key[j] + "  ");
              println("\n\nType in 'x' and press enter to continue.");
              boolean alive = true;
              while(alive)
              {
                  String s3 = kbr.nextLine();
                  if(s3.equalsIgnoreCase("x"))
                  {
                      alive = false;
                  }
              }
              byte[] IV = c.getIV();
              clear("This is your IV:\n\n");
              for(int j=0; j<IV.length; j++)
              {
                  print(IV[j] + "  ");
              }
              println("\n\nType x to continue.\n");
              String ew = "";
              while(true)
              {
                  ew = kbReader.nextLine();
                  if(ew.equalsIgnoreCase("x"))
                  {
                      break;
                  }
              }
              output.close();
              fw.close();
          }
          public static void input()
              throws IOException,
              NoSuchAlgorithmException,
              NoSuchPaddingException,
              InvalidKeyException,
              IllegalBlockSizeException,
              BadPaddingException,
              InvalidAlgorithmParameterException
          {
              Scanner kbReader = new Scanner(System.in);
              Scanner kbr = new Scanner(System.in);
              Cipher c = Cipher.getInstance("AES/CBC/PKCS5PADDING");
              clear("What is the name of the file?\nName > ");
              fileName = kbr.next();
              String s2 = "/Users/ewsmith/base/" + fileName;
              Scanner sf = new Scanner(new File(s2));
              int num = 1;
              
              clear("Key > ");
              byte key[] = new byte[16];
              for(int j=0; j<16; j++)
              {
                  key[j] = kbReader.nextByte();
              }
              
              clear("IV > ");
              byte ivBytes[] = new byte[16];
              for(int j=0; j<16; j++)
              {
                  ivBytes[j] = kbReader.nextByte();
              }
              IvParameterSpec iv = new IvParameterSpec(ivBytes);
              
              SecretKeySpec k = new SecretKeySpec(key, "AES");
              c.init(Cipher.DECRYPT_MODE, k, iv);
              clear("This text was stored inside the file.\n\n\n");
              int j = 0;
              ArrayList<Byte> ip = new ArrayList<Byte>();
              while(sf.hasNext())
              {
                  ip.add(sf.nextByte());
              }
              byte input[] = new byte[ip.size()];
              for(j=0; j<ip.size(); j++)
              {
                  input[j] = ip.get(j);
              }
              String recovered = new String(c.doFinal(input));
              println(recovered);
              sf.close();
              ip.clear();
              input = null;
              println("\n\nType in 'x' and press enter to continue.");
              boolean alive = true;
              while(alive)
              {
                  String s = kbReader.nextLine();
                  if(s.equalsIgnoreCase("x"))
                  {
                      alive = false;
                  }
              }
          }
          public static void genKey()
          {
              Scanner kbReader = new Scanner(System.in);
              clear();
              Random ranGen = new SecureRandom();
              byte[] aesKey = new byte[16];
              ranGen.nextBytes(aesKey);
              for(int j=0; j<aesKey.length; j++)
              System.out.print(aesKey[j] + "  ");
              println("\n\nType in 'x' and press enter to continue.");
              boolean alive = true;
              while(alive)
              {
                  String s = kbReader.nextLine();
                  if(s.equalsIgnoreCase("x"))
                  {
                      alive = false;
                  }
              }
          }
          public static String fileName = "";
      }
      Thank you in advance for answering.

      EDIT:
      It now says:
      +     at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)+
      +     at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)+
      +     at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA12275)+
      +     at javax.crypto.Cipher.doFinal(DashoA12275)+
      +     at Base.input(Base.java:184)+
      +     at Base.main(Base.java:23)+
      +     at __SHELL3.run(__SHELL3.java:6)+
      +     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)+
      +     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)+
      +     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)+
      +     at java.lang.reflect.Method.invoke(Method.java:585)+
      +     at bluej.runtime.ExecServer$3.run(ExecServer.java:819)+
      Could this be an IV error?
      Can negative IV's be used?

      Edited by: eric_smith on Jan 6, 2010 9:25 PM

      Edited by: eric_smith on Jan 7, 2010 9:51 AM