• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
  • Piet Souris
  • Mikalai Zaikin

Please help : IllegalBlockSizeException

Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings people,
I have devised an application which reads the value from file and encrypts it (using Blowfish)......The file is then read and decrypted accordingly..............
Problem : I seem to be able to encrypt and further decrypt file(s) which size is below 100 bytes. Anything beyond that will trigger the following :
javax.crypto.IllegalBlockSizeException: Input length (with padding) not multiple of 8 bytes
at com.sun.crypto.provider.SunJCE_h.a(DashoA6275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)
at com.sun.crypto.provider.SunJCE_h.b(DashoA6275)
at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA6275)
at javax.crypto.Cipher.doFinal(DashoA6275)
at LockDownBase.decrypt(LockDownBase.java:144)
I really need to know whether the limitation lies with me using BufferReader.readline() in method decrypt or is this an inherit limitation with the tool. (ps. It is necessary for me to retain the usage of the Base64 Encoder as I am conducting a study on the usage of this Encoder).
Thanking y'all in advance and here's hoping y'all can help me out
---------------------start source LockDownBase.java------------------------------------------------------------
import java.io.*;
import java.math.*;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import sun.misc.*;

public class LockDownBase {
private static String keyFile = "c:\\encode\\blowfishbase.txt";
public static String new_key (String me) throws Exception {
//Get a blowfish key
KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
SecretKey key = keyGenerator.generateKey();
byte [] encoded = key.getEncoded();
FileOutputStream fos = new FileOutputStream(keyFile);
return me;
public static Key initKey() throws Exception{
FileInputStream in = new FileInputStream(keyFile);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i;
while((i=in.read()) != -1){
byte [] keys = baos.toByteArray();
SecretKeySpec key = new SecretKeySpec(keys,"Blowfish");
Key keyword = key;
return key;

public static String encrypt (String location) throws Exception{

//FileOutputStream outFile1 = new FileOutputStream("C:\\encode\\dirt3.txt");

//PrintStream is deprecated, but works fine in jdk1.1.7b
//PrintStream output1 = new PrintStream(outFile1);

String testme = "dummy";
Key key = initKey();
Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key);
// read in the input file
String line;
StringBuffer buffer = new StringBuffer();
FileInputStream fis = new FileInputStream(location);
InputStreamReader isr = new InputStreamReader(fis);
Reader in = new BufferedReader(isr);
int ch;
while ((ch = in.read()) > -1) {
line = buffer.toString();

byte [] cipherText = cipher.doFinal(line.getBytes("UTF8"));
//output1.print(" ");
// output1.println("ciphertext.length = " + cipherText.length);
// print out representation of ciphertext to general output file
BASE64Encoder encoding = new BASE64Encoder();
String feed = encoding.encode(cipherText);
FileOutputStream outFile2 = new FileOutputStream(location);
PrintStream output2 = new PrintStream(outFile2);
String dir = location;
FileOutputStream outFile3 = new FileOutputStream(dir);
PrintStream output3 = new PrintStream(outFile3);
return location;
public static String decrypt (String location) throws Exception{

Key key = initKey();

String line;
String dir = location;
BufferedReader stdin = new BufferedReader(new FileReader (dir));
line = stdin.readLine();

BASE64Decoder decoding = new BASE64Decoder();
byte[] decrypted = decoding.decodeBuffer(line);

Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
byte [] decryptedtext = cipher.doFinal(decrypted);
String output = new String (decryptedtext,"UTF8");
FileOutputStream outFile1 = new FileOutputStream(location);

PrintStream output1 = new PrintStream(outFile1);
return location;
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are only reading in the first line of the encrypted text instead of the entire file.
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic