Results 1 to 10 of 10
Thread: Need help with Cryptosystem
- 01-18-2012, 05:38 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
Need help with Cryptosystem
Here is how the crypto system is defined:
1) Keys are 16-bit values.
2) Messages are strings with an even number of characters. If the input string is odd, add a blank at the
end of the string.
3) The encryption of a message M of length n (in bytes) is given by:
Ek (M) = M XOR (K || K || …)
where the key K is repeated n/2 times.
4) The decryption algorithm for a ciphertext C is the same as the encryption algorithm:
Dk (M) = M XOR (K || K || …).
The part that I am having trouble with is after reading in the text file is converting the strings into the ASCII binary values. I have tried for a few days to figure out how to do so.
Any help would be much appreciated.
Thanks
- 01-18-2012, 06:41 PM #2
Re: Need help with Cryptosystem
Can you show an example of this? Show an input String and what you want to get from that input.after reading in the text file is converting the strings into the ASCII binary values.
Did you try any of the String class's methods?
- 01-23-2012, 12:22 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
Re: Need help with Cryptosystem
Ok an example of an input string would be "This is a java test."
What I need to do is convert each char in the string to its ASCII value.
Here is the code that I have
Instead of returning the ASCII value for the char it returns its position in the string.//Create a JFileChooser
//JFileChooser used to get and set the selected file
JFileChooser chooser = new JFileChooser();
File f = new File(new File("filename.txt").getCanonicalPath());
chooser.setSelectedFile(f);
chooser.showOpenDialog(null);
File inputFileName = chooser.getSelectedFile();
FileReader reader = new FileReader(inputFileName);
Scanner in = new Scanner(reader);
while (in.hasNextLine())
{
String line = in.nextLine();
for (int i = 0; i < line.length(); i++)
System.out.println(line.charAt(i) + " as ASCII value " + (int)i);
}//end while
- 01-23-2012, 12:37 AM #4
Re: Need help with Cryptosystem
Your code uses the charAt() method to get each character.What I need to do is convert each char in the string to its ASCII value.
Given a character you have its ASCII value. The value of a char is its ASCII value.
When you print it you have many choices of how to see its value:
as a char
as an int
as two hex digits
as binary digits
What format do you want to see the value in: char, int, hex or binary?Last edited by Norm; 01-23-2012 at 01:39 AM.
- 01-23-2012, 12:51 AM #5
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
Re: Need help with Cryptosystem
I need to see the ASCII binary value of each character.
- 01-23-2012, 01:39 AM #6
Re: Need help with Cryptosystem
To print out the value of a char in binary use the Integer class's toBinaryString() method.see the ASCII binary value of each character.
- 01-23-2012, 07:10 PM #7
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
Re: Need help with Cryptosystem
I need some help figuring out how to get XOR to work right here is the code that I have
Java Code://Create a JFileChooser //JFileChooser used to get and set the selected file JFileChooser chooser = new JFileChooser(); File f = new File(new File("filename.txt").getCanonicalPath()); chooser.setSelectedFile(f); chooser.showOpenDialog(null); File inputFileName = chooser.getSelectedFile(); // create FileInputStream object // fin = file input FileInputStream fin = new FileInputStream(inputFileName); /** * Create byte array large enough to hold the content of the file * use File.length to determine size of the file in bytes */ byte[] fileContent = new byte[(int)f.length()]; /** * To read content of the file in byte array, use * int read(byte[] byteArray) method of java FileInputStream class. */ fin.read(fileContent); System.out.println(fileContent); // get the key from the user BufferedReader myBuffer2 = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Please enter 16 bit key"); key = Integer.parseInt(myBuffer2.readLine()); // System.out.println(key); // encrypts the file // must cast the byteArray back into a byte because it was converted to an int by XOR for(int i = 0; i < fileContent.length; i++) { fileContent[i] = (byte)( key ^ fileContent[i]); } System.out.println(new String(fileContent));Last edited by Norm; 01-23-2012 at 07:25 PM. Reason: changed quote to code
- 01-23-2012, 07:25 PM #8
Re: Need help with Cryptosystem
Please explain what you are trying to do. Show what you want to input and the desired output.how to get XOR to work
For testing use a small array that is in the program instead of data read from a file. That makes it easier to debug.Last edited by Norm; 01-23-2012 at 07:27 PM.
- 01-23-2012, 07:40 PM #9
Member
- Join Date
- Oct 2010
- Posts
- 20
- Rep Power
- 0
Re: Need help with Cryptosystem
I am trying to xor the key that the user providers and fileContent which is the content that the input file contained.
I need an output of 1's and 0's after I XOR the key and fileContent.
Thank you for all the help.
- 01-23-2012, 07:46 PM #10
Re: Need help with Cryptosystem
Please show a sample input byte array, a key and the desired output..
What is wrong with your current code? Explain why the output is not what you want and show what you want the output to be.
Everything in a computer is in 1s and 0s. There are many different ways to display the contents of a byte: as int, in hexadecimal, in binary, as a char.I need an output of 1's and 0's
How you display a byte's contents does NOT change its contents.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks