I am working on a project to encrypt some data using XOR encryption. (Yes, I know XOR is not a strong encryption scheme, but I'm just getting my feet wet in cryptography and using this as a stepping stone to more complicated things.) Anyways, I wrote a class called Xor that looks like the following:
The encrypt() method takes characters from the given InputStream and writes encrypted data to the OutputStream. Now, I also wrote a helper class called StreamManipulator that does some other transformations on a given InputStream and writes to a given OutputStream:
As the name indicate, removeWhitespace() reads characters from the given InputStream and writes all non-whitespace characters to the given OutputStream. The toLowerCase() method reads characters from the given InputStream, converts them to lower-case, and writes the converted characters to the given OutputStream.
I feel like this is a good design since each method does a small task. Alternatively, Xor.encrypt() could do all the tasks at once, but the logic in that one method would become monstrous. Also, if I create a Encryptor interface, I can easily replace the XOR cipher with a different one. (This will probably happen down the road at some point.)
Now, I want to put these all together: take a InputStream (at the moment this will be a FileInputStream, but I'm coding to the interface not the implementation
) and convert all the characters to lower case, remove all whitespace, then encrypt it using the XOR cipher. One solution is to create temporary files and write each intermediate step to disk. However, I would like to avoid the overhead of file I/O that this would create. I also don't want to read the whole stream into memory as a
String and process it from there. (Hence the decision to take stream paramters instead of Strings.) So instead, I read about PipedInputStream and PipedOutputStream. These seem like a good solution to my problem as long as I can come up with a decent scheme to create the threads for each step in the process. However, I have come up against a few problems, mostly regarding IOExceptions. Let me illustrate with the code that I have come up with so far:
The first problem it doesn't compile since close() throws an IOException. So my first question is how do I deal with this cleanly? Do I add another try...catch statement to my finally clauses? This seems a little ugly to me, so is there a better way to do this? Even more importantly, am I even approaching this in the right way? I don't know a lot about piped streams, so maybe I need to back up and figure them out better. But this
thread is already long enough, so I'll start another thread when I want to ask more general questions about the behavior of PipedInputStream and PipedOutputStream.
Well, that's it for now. I would appreciate any feedback or comments anyone has.
Regards,
Layne