• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Very urgent please help : JCE problem

Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Greetings people,

My name is Sathya......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).

Thank you for the help
---------------------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{

//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;
FileReader far = new FileReader (dir);
BufferedReader stdin = new BufferedReader(far,8192);
String line = null;

while ((line = stdin.readLine()) != null){

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: 11962
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple of things:
1) Would you mind editing out the snippet where you blurt that your name actually isn't "Das Sats"? See this thread for some background on why it isn't acceptable to publicize that your display name is fake.
2) It would help other ranchers in helping you, if you'd use the UBB [CODE][/CODE] tags around your code snippets.
I'm sorry that I can't help with your actual problem, but I'm sure someone else will.
Das Sats
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as ubb tags are concerned, I am sorry for not using them....Concerning my name on the other hand :
Surname: Das
First Name : Sathya (Sats) for short
I hope this helps clear things up
    Bookmark Topic Watch Topic
  • New Topic