Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8

843811Jan 27 2010 — edited Jan 28 2010
I am trying to Decrypt a file from user key, but while doing decryption im getting
the error i have no idea to solve anyone please help me
javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
at com.sun.crypto.provider.SunJCE_f.b(DashoA13..)*
at com.sun.crypto.provider.SunJCE_f.b(DashoA13..)*

 try {
      FileInputStream fin = new FileInputStream(inputFile);
      FileOutputStream fout = new FileOutputStream(f.getAbsolutePath()+"/"+inputFileName.getName());

      // Create a key.
      byte[] desKeyData = userKey.getBytes();
      DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
      SecretKey desKey = keyFactory.generateSecret(desKeySpec);

      // Read the initialization vector.
      DataInputStream din = new DataInputStream(fin);
      System.out.println("DataInputStream-"+din==null);

      int ivSize = din.readInt();
      byte[] iv = new byte[ivSize];


      din.readFully(iv);
      IvParameterSpec ivps = new IvParameterSpec(iv);

      // Use Data Encryption Standard.
      Cipher des = Cipher.getInstance("DES/CBC/PKCS5Padding");
      des.init(Cipher.DECRYPT_MODE, desKey, ivps);

      byte[] input = new byte[256];
      int bytesRead;
      while ( (bytesRead= fin.read(input))!=-1) {
        byte[] output = des.update(input, 0, bytesRead);
        if (output != null) fout.write
(output);
}

byte[] output = des.doFinal();
if (output != null) fout.write(output);
fin.close();
fout.flush();
fout.close();
}
catch (GeneralSecurityException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
System.err.println(ex);
ex.printStackTrace();
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 25 2010
Added on Jan 27 2010
6 comments
1,471 views