File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes FileOutputStream w/ Cipher adds extra lines unnecessarily Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Reply Bookmark "FileOutputStream w/ Cipher adds extra lines unnecessarily " Watch "FileOutputStream w/ Cipher adds extra lines unnecessarily " New topic
Author

FileOutputStream w/ Cipher adds extra lines unnecessarily

john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

The following code seems prints to "login.encrypted" empty lines, then the value of "x". Does this do this on it's own or is another part of my 1.2k + lined program that is buggy?


Thanks,
John Price


“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
I've tried your code, and I've got this content in the output file:

It seems to be encrypted, I don't see any empty lines in it.
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

When I read the file back in, I see the empty lines. With CipherInputStream and a BufferedReader. I use this over my program in other places and don't see this issue until after it is written to, adding a line to the end (which adds the blank lines for some reason).

Thanks,
cc11rocks
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 13842

But that is an entirely different question. Now you are saying that encryption followed by decryption causes extra lines to be added. Obviously there is the possibility that the decryption process is the problem. At any rate I'm sure the answer is that some part of your program is buggy.

However you don't have to debug 1200 lines of code. You just have to extract the encrypting and decrypting parts and put them into a 50-line test program.
john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

Here is the read in where the lines display extra lines. Note this does not happen if something hasn't been appended to the file.


Output gives somthing like this :


If I put in a test case, I get this :


Which displays this :


Thanks,
John Price
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
john price wrote:Here is the read in where the lines display extra lines. Note this does not happen if something hasn't been appended to the file.


Something is appended to the encrypted file ?
Does it mean, that something unencrypted has been appended to encrypted contents ?
If yes, then the problem is here - the encrypted file doesn't have
neither information of the content's size nor any checksum. The cipher doesn't detect the end of the encrypted output,
and it doesn't detect if contents has been modified.
It dencrypts the whole content - if something is appended to it,
it decrypts this part too.



john price
Ranch Hand

Joined: Feb 24, 2011
Posts: 495

I was trying to append to the whole file. I was adding encrypted information to the file. I had to release the next version of a program so this is no longer a problem. I used a more efficient way by rewriting the whole file and adding the specific item to memory. Thanks for your help.

John Price
 
 
subject: FileOutputStream w/ Cipher adds extra lines unnecessarily
 
Threads others viewed
AES Encryption/Decrypton
Overhead IO : The cryptic case of the extra bytes
AES decryption - InvalidKeyException: Parameters missing
OutputStreamWriter
Problem encrypting URL with AES
IntelliJ Java IDE