Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java JCE : IllegalBlockSize Exception

Posted on 2003-11-03
5
Medium Priority
?
746 Views
Last Modified: 2012-06-21
Hi guyz,
  Could somebody please help me out with this........I cant for the life of me figure out why an IllegalBlockSize Exeception is being thrown.........



package Crypto;

import java.security.*;
import javax.crypto.*;
import sun.misc.*;

public class KryptonTest {
   
   
   
public static void main (String[] args) throws Exception {
    String ClearText = "dazzman123";
    System.out.print("Attempting to get a Blowfish key");
    KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
    keyGenerator.init(128);
    SecretKey key = keyGenerator.generateKey();
    System.out.println("OK");
   
    System.out.print("Attempting to get a cipher and encrypt");
    Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    byte [] cipherText = cipher.doFinal(ClearText.getBytes("ASCII"));
           
    String playitbaby = cipherText.toString();
    System.out.println(playitbaby);
   

   
    byte [] decrypted = playitbaby.getBytes("ASCII");
   
     
    cipher.init(Cipher.DECRYPT_MODE, key);
   
   
    byte [] decryptedtext = cipher.doFinal(decrypted);


   
    String output = decryptedtext.toString();
   
    System.out.println (output);
 
   
    System.out.println("Test Completed Successfully");
----------------------------------------------------------------------------




Attempting to get a Blowfish keyOK
Attempting to get a cipher and en
javax.crypto.IllegalBlockSizeException: Input length (with padding) not multiple of 8 bytes
        at com.sun.crypto.provider.BlowfishCipher.a(DashoA6275)
        at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA6275)
        at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA6275)
        at javax.crypto.Cipher.doFinal(DashoA6275)
        at Crypto.KryptonTest.main(KryptonTest.java:60)crypt[B@5d56d5
 
0
Comment
Question by:dazz_man
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 3

Expert Comment

by:savalou
ID: 9672167
You should decrypt cipherText, not playitbaby.

cipherText.toString() isn't going to give you a string representation of the array, that gives you the object reference of the array.   Your encrypted message is not text, it's binary and may not be printable text.  If you want to know what the bytes are, print them out one-by-one in a for loop and you'd have some idea.

0
 
LVL 3

Accepted Solution

by:
savalou earned 100 total points
ID: 9672181
Sorry, decrypt cipherText, not decrypted.

So
 byte [] decryptedtext = cipher.doFinal(decrypted); // no
becomes
 byte [] decryptedtext = cipher.doFinal(cipherText); //
0
 

Assisted Solution

by:Ananthalakshmi
Ananthalakshmi earned 100 total points
ID: 9676407
Hi,
Visit http://forum.java.sun.com/thread.jsp?thread=454515&forum=9&message=2075112.

The following is an extract from the above site.

========================================================
IllegalBlockSize is the result of not using padding and or providing an invalid input length.

DES is a block cipher which requires at least 8 bytes of data and a total divisable by 8 bytes. That is unless you use a padding scheme which ensures it is a multiple of 8 bytes.

AES has a 16 byte block size. It must have 16 bytes of data input to do a single crypt operation. Total input length must be a multiple of 16 bytes.

The easiest way to deal with this is to use padding. PKCS5Padding as an example. Now padding will result in your output ciphertext being larger than your input plaintext (because of the pad bytes) but it will ensure you never get that error. Another solution is to use a stream mode such as CFB8 or CTR mode. This does not require padding and allows a block cipher such as DES, DESede, AES, etc to behave like a stream cipher. (aka doesn't care how much input you give it)... And finally, you can use a Stream cipher. Something like RC4 for example.

============================================================

Thanks,
H.Ananthalakshmi.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question