PDA

View Full Version : Cryptation with Java - How do I change bits in byte?


Sharagoz
09-28-2006, 08:54 PM
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 isnt 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.
Yep, its ridiculous. Suggestions would be much apprechiated.
Again: What I need is to know how to manage the bytes without losing information. It seems that bits get lost when I try to change the cryptated BinaryString to an Integer and then to a Byte. I havent found a different way to do this yet.

EDIT:
Let me just restate the question a little: 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.

jesper
10-04-2006, 01:34 PM
How exactly are you reading the data from the file? It sounds like you're doing it in a much too complicated way ("read the file into a string, turn it into an array of bytes, ...").

In Java, you use Readers and Writers to read and write text files. Readers and Writers take care of decoding and encoding the data to and from Strings, using character encodings.

To read binary data, you should use an InputStream (for files, a FileInputStream).

InputStream in = new FileInputStream("myfile.dat");

byte[] buffer = new byte[1024];
int count = in.read(buffer);
System.out.println("Read " + count + " bytes from the file");

in.close();

Lookup FileInputStream in the Java API documentation.

Gox
10-05-2006, 01:28 AM
Just to add to jesper's post, I'll give you the link to the API
http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html

read

public int read(byte[] b)
throws IOException

Reads some number of bytes from the input stream and stores them into
the buffer array b. The number of bytes actually read is returned as an
integer. This method blocks until input data is available, end of file is detected,
or an exception is thrown.

If b is null, a NullPointerException is thrown. If the length of b is zero, then
no bytes are read and 0 is returned; otherwise, there is an attempt to read atleast one byte.
If no byte is available because the stream is at end of file, the value -1 is returned;
otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[0], the next one into b[1], and
so on. The number of bytes read is, at most, equal to the length of b. Let k be
the number of bytes actually read; these bytes will be stored in elements b[0]
through b[k-1], leaving elements b[k] through b[b.length-1] unaffected.

If the first byte cannot be read for any reason other than end of file, then
an IOException is thrown. In particular, an IOException is thrown if the input
stream has been closed.

The read(b) method for class InputStream has the same effect as:

read(b, 0, b.length)

Parameters:
b - the buffer into which the data is read.
Returns:
the total number of bytes read into the buffer, or -1 is there is no more
data because the end of the stream has been reached.
Throws:
IOException - if an I/O error occurs.
NullPointerException - if b is null.
See Also:
read(byte[], int, int)