Input length (with padding) not multiple of 8 bytes - Lost Bytes ?

Hello,

I have implemented a small prog, which has to encryt a message and then to decrypt it. I use Blowfish with JDK1.4. Everything works fine.

Then I want to do this with a file. Encrypt a sourcefile and store the encryption in a destination. Then decrypt the destination.

But I get this exception: Input length (with padding) not multiple of 8 bytes

The strange thing is that I write 840 bytes and read 832 bytes:

Administrator@DIPCML01 /cygdrive/d/juraj/projekte/java/blowfish/ibm crypt

$ java Encrypt1 ../test.txt test3.txt key.txt

geschrieben: 840

Administrator@DIPCML01 /cygdrive/d/juraj/projekte/java/blowfish/ibm crypt

$ java Decrypt1 test3.txt key.txt test.txt

gelesen: 832

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length

(with padding) not multiple of 8 bytes

at com.sun.crypto.provider.BlowfishCipher.a(DashoA1275)

at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA1275)

at com.sun.crypto.provider.BlowfishCipher.engineDoFinal(DashoA1275)

at javax.crypto.Cipher.doFinal(DashoA6275)

at Decrypt1.main(Decrypt1.java:47)

The read part:

byte[] encText1 = new byte[len1];

byte[] buff = new byte[len1];

// update the cipher with the data to be decrypted

while (bis.available() != 0)

{

len1 = bis.read(buff);

int countjlt = jceCipher.update(buff, 0, len1, encText1);

System.out.println("gelesen: " + countjlt);

}

The file stores only one row.

I use FileoutputStream to store the data and FileInputStream/BufferedInputStream to read the data. Can someone give me a answer where the exception causes or why I lost 8 bytes?

thanks

juraj

[1814 byte] By [nort_de] at [2007-9-27 1:56:09]
# 1
to make file encryption use javax.crypto.CipherOutputStream and javax.crypto.CipherInputStream.you'll findsample code with jce documentation.
amelin at 2007-7-4 20:35:45 > top of Java-index,Security,Cryptography...
# 2
Hi,tanks for your answer. I have tried it and it works fine. But there is sometimes a constellation that my string in the file is cut. I think that happens if the last block is not %8. Is that right? So I put 8 blanks to the end of the file to avoid this. juraj
nort_de at 2007-7-4 20:35:45 > top of Java-index,Security,Cryptography...
# 3
it depend of your algorithm. What kind of padding are you using ?
amelin at 2007-7-4 20:35:45 > top of Java-index,Security,Cryptography...
# 4

those extra bytes are never being sent. Blowfish is a block cipher that does things in increments of 8 bytes (though you can change this I believe to higher). So any data you try to cipher and does not equal a multiple of 8 and you have nothing else to send then you need to close the Cipher Stream which calls a doFinal on the cipher and clears the cipher buffer. What it will output is the bits of data that have not been sent yet and padding bytes to make it be a multiple of 8.

ganeytd at 2007-7-4 20:35:45 > top of Java-index,Security,Cryptography...