1 Reply Latest reply: Jan 11, 2010 9:10 AM by 843810 RSS

    IllegalBlockSizeException help?

    843810
      I have a program that encrypts text and puts it on a text file and pulls it from the text file and decrypts it.
      However, the program gives me this:
      javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
      +     at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)+
      +     at com.sun.crypto.provider.SunJCE_h.b(DashoA12275)+
      +     at com.sun.crypto.provider.SunJCE_af.b(DashoA12275)+
      +     at com.sun.crypto.provider.PBEWithMD5AndDESCipher.engineDoFinal(DashoA12275)+
      +     at javax.crypto.Cipher.doFinal(DashoA12275)+
      +     at Encrypter.decryption(Encrypter.java:52)+
      +     at encryptDecrypt.passDecrypt(encryptDecrypt.java:75)+
      +     at encryptDecrypt.main(encryptDecrypt.java:117)+

      Encrypter Code
      import javax.crypto.*;
      import javax.crypto.spec.*;
      import java.security.*;
      import java.security.spec.*;
      import java.io.*;
      import java.util.*;
      import static methods.methods.*;
      public class Encrypter
      {
          public static Cipher ecipher;
          public static Cipher dcipher;
          
          public Encrypter(String passPhrase) throws
                  InvalidKeySpecException,
                  NoSuchPaddingException,
                  NoSuchAlgorithmException,
                  InvalidKeyException,
                  InvalidAlgorithmParameterException
          {
              byte salt[] = {
                  (byte)0xA9, (byte)0x9B, (byte)0xC8, (byte)0x32,
                  (byte)0x56, (byte)0x34, (byte)0xE3, (byte)0x03
              };
              int iteration = 7;
              
              KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iteration);
              SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
              AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iteration);
              ecipher = Cipher.getInstance(key.getAlgorithm());
              dcipher = Cipher.getInstance(key.getAlgorithm());
              ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
              dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
          }
          
          public String encryption(String str) throws
                  BadPaddingException,
                  IllegalBlockSizeException,
                  UnsupportedEncodingException,
                  IOException
          {
              byte utf8[] = str.getBytes("UTF8");
              byte encoded[] = ecipher.doFinal(utf8);
              return new sun.misc.BASE64Encoder().encode(encoded);
          }
          public String decryption(String str) throws
                  BadPaddingException,
                  IllegalBlockSizeException,
                  UnsupportedEncodingException,
                  IOException
          {
              byte dec[] = new sun.misc.BASE64Decoder().decodeBuffer(str);
              byte utf8[] = dcipher.doFinal(dec);
              return new String(utf8, "UTF8");
          }
      }
      pulls the Encrypter class.
      import java.io.*;
      import java.util.*;
      import java.security.*;
      import java.security.spec.*;
      import javax.crypto.*;
      import javax.crypto.spec.*;
      import static methods.methods.*;
      public class encryptDecrypt
      {
          public static String password = null;
          public static String fileName = null;
          
          public static void passEncrypt() throws
                  BadPaddingException,
                  IllegalBlockSizeException,
                  UnsupportedEncodingException,
                  NoSuchAlgorithmException,
                  InvalidAlgorithmParameterException,
                  InvalidKeySpecException,
                  InvalidKeyException,
                  NoSuchPaddingException,
                  IOException
          {
              Scanner kbReader = new Scanner(System.in);
              clear("File Name > ");
              fileName = kbReader.nextLine();
              String filePath = "/Users/ewsmith/base/";
              String fp = filePath + fileName;
              FileWriter fw = new FileWriter(fp, true);
              PrintWriter output = new PrintWriter(fw, true);
              
              String s = "";
              String text = "";
              clear("Enter what you what stored. Type 'x' to finish.\n");
              while(true)
              {
                  s = kbReader.nextLine();
                  if(s.equals("x"))
                  {
                      break;
                  }
                  text = text + s + "\n";
              }
              
              Encrypter desEncrypter = new Encrypter(password);
              String desEncrypt = desEncrypter.encryption(text);
              output.print(desEncrypt);
              output.close();
              fw.close();
              main(password);
          }
          
          public static void passDecrypt() throws
                  BadPaddingException,
                  IllegalBlockSizeException,
                  UnsupportedEncodingException,
                  NoSuchAlgorithmException,
                  InvalidAlgorithmParameterException,
                  InvalidKeySpecException,
                  InvalidKeyException,
                  NoSuchPaddingException,
                  IOException
          {
              Scanner kbReader = new Scanner(System.in);
              clear("File Name > ");
              fileName = kbReader.nextLine();
              String filePath = "/Users/ewsmith/base/";
              String fp = filePath + fileName;
              Scanner fs = new Scanner(new File(fp));
              Encrypter desEncrypter = new Encrypter(password);
              clear("This is what is stored inside the file.\n\n\n");
              String re = "";
              while(fs.hasNext())
              {
                  re = (desEncrypter.decryption(fs.nextLine()));
                  print(re);
              }
              clear("This is what is stored inside the file.\n\n\n");
              println("\n\nType 'x' to continue.");
              String s = "";
              while(true)
              {
                  s = kbReader.nextLine();
                  if(s.equals("x"));
                  {
                      break;
                  }
              }
              main(password);
          }
          public static void main(String pass) throws
                  BadPaddingException,
                  IllegalBlockSizeException,
                  UnsupportedEncodingException,
                  NoSuchAlgorithmException,
                  InvalidAlgorithmParameterException,
                  InvalidKeySpecException,
                  InvalidKeyException,
                  NoSuchPaddingException,
                  IOException
          {
              Scanner kbReader = new Scanner(System.in);
              clear("Method > ");
              String s = null;
              s = kbReader.next();
              password = pass;
              if(s.equals("x"))
              {
                  clear("It stopped.");
              }
              if(s.equals("output"))
              {
                  passEncrypt();
              }
              if(s.equals("input"))
              {
                  passDecrypt();
              }
              if(s == null)
              {
                  clear("Oops. Something went wrong.");
              }
          }
      }
      file text the password is ericSmith
      wfEjcfruJ/i86i6caJ/oVyl/IPjkSGrWO3LbJjywBo7JmcB9x1oh2ERaZTGnZv/01r3MAa2AkJUB
      VUlkAteldt9DTvDHigOIZ/rpOSQdp8zJYue5zmTS7veE1TLsp/5haBSMTw3fOv0GAqazMkyviQ==

      Help will be greatly appreciated.
        • 1. Re: IllegalBlockSizeException help?
          843810
          Couldn't fit the methods.methods class into the previous post.
          package methods;
          
          public class methods
          {
              public static void print(String s)
              {System.out.print(s);}
              public static void print(int i)
              {System.out.print(i);}
              public static void print(double d)
              {System.out.print(d);}
              public static void print(byte b)
              {System.out.print(b);}
              public static void print(long l)
              {System.out.print(l);}
              public static void print(short s)
              {System.out.print(s);}
              public static void print(float f)
              {System.out.print(f);}
              public static void println(String s)
              {System.out.println(s);}
              public static void println(int i)
              {System.out.print(i);}
              public static void println(double d)
              {System.out.print(d);}
              public static void println(byte b)
              {System.out.print(b);}
              public static void println(long l)
              {System.out.print(l);}
              public static void println(short s)
              {System.out.print(s);}
              public static void println(float f)
              {System.out.print(f);}
              public static void clear(String s)
              {
                  System.out.print("\f");
                  System.out.print(s);
              }
              public static void clear()
              {System.out.print("\f");}
          }