How to implements cryptation in a java
Im trying to implement s-des cryptation in a java application.
Basically my program will open a file read the content, cryptate it, and write the cryptated information back. It will also be able to decrypt the same file.
The problem so far isn't the algorithms, but how to handle the data without losing information etc. When I read the file the content is saved in a string.
The program is supposed to cryptate the file 1 byte (8 bits) at a time. I have not yet found a good way to extract the first 8 bits of the file, run the algorithms on them, save the result somewhere without losing information, repeat the process on the next 8 bits until the whole string has been cryptated, and then write the cryptated string into a new file.
Any suggestions to how I can do this?
Here's what didnt work very well:
Read the file into a string. Turn the string into an array of bytes. Take one byte, change it to a BinaryString, run the algorithms (which rearranges the bits (characters) in the BinaryString), parse the BinaryString into an Integer, parse the Integer into a byte, and write it back into an array of bytes which will be turned into a string and written back to the file after all the bytes have been run through the algorithms.
It seems that bits get lost when I try to change the cryptated BinaryString to an Integer and then to a Byte. I haven't found a different way to do this yet.
How can I extract 1 byte (8 bits) at a time from a file, and then put the bytes back together again without losing bytes/bits or adding extra bytes/bits? I also need to be able to flip the individual bits in the byte.