Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-16-2007, 06:38 PM
Member
 
Join Date: Jul 2007
Posts: 26
paul is on a distinguished road
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.
Thanks
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 06:06 AM
Member
 
Join Date: Jul 2007
Posts: 39
coco is on a distinguished road
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).

Code:
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();
Look up FileInputStream in the Java API documentation.
Greetings.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implements MyClass extends JFrame coco AWT / Swing 1 08-06-2007 04:43 AM


All times are GMT +3. The time now is 02:13 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org