Results 1 to 2 of 2
  1. #1
    paul is offline Member
    Join Date
    Jul 2007
    Rep Power

    Default 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.

  2. #2
    coco is offline Member
    Join Date
    Jul 2007
    Rep Power


    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 encoding.

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

    Java Code:
    InputStream in = new FileInputStream("myfile.dat");
    byte[] buffer = new byte[1024];
    int count =;
    System.out.println("Read " + count + " bytes from the file");
    Look up FileInputStream in the Java API documentation.

Similar Threads

  1. Implements MyClass extends JFrame
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 03:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts