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!

Encrypting/Decrypting blowfish issues?

843810Dec 3 2001 — edited Jan 12 2004
Hiyas,

I'm using jdk 1.4 beta 3 to encrypt various files with 128-bit blowfish encryption. I use the Cipher class to generate a new blowfish instance, open a FileInputStream for the file to be encrypted, read in 8 bytes at a time into an array (padding arrays w/o 8 bytes), then write the array to a FileOuputStream after encrypting with the Cipher.doFinal() method. Before all encrypting, I have 37251 bytes to encrypt. After encryption, the encrypted file has 66224 bytes available. I figure this is due to the fact that encrypting the bytes has done something to make them larger (newbie alert!)

Now the problem is when trying to decrypt the file. In my program, I open a FileInputStream using the encrypted file created before. Then I read in 8 bytes from the file input stream and call the Cipher.doFinal() method on it. Then I write the decrypted bytes to another file.

When I do this, I keep getting the Exception: javax.crypto.BadPaddingException: Given final block not properly padded error. I've tried like everything and can't figure out how to fix this problem. I can't read the encrypted file byte by byte because you have to decrypt in blocks that are multiples of 8 since it is blowfish encryption. So I'm basically lost on how do encrypt the file with blowfish then decrypt the same file with blowfish at a later date (the SecretKeySpec is written to a file as well in order to initialize the Cipher object at any later date).

Any help folks can offer is greatly appreciated. Or if my methodology is the long way around, any other ideas folks may have to accomplish this task are also welcome =) Thanks!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 9 2004
Added on Dec 3 2001
3 comments
357 views