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
Re: Need help with Cryptosystem
Quote:
after reading in the text file is converting the strings into the ASCII binary values.
Can you show an example of this? Show an input String and what you want to get from that input.
Did you try any of the String class's methods?
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
Quote:
//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
Instead of returning the ASCII value for the char it returns its position in the string.
Re: Need help with Cryptosystem
Quote:
What I need to do is convert each char in the string to its ASCII value.
Your code uses the charAt() method to get each character.
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?
Re: Need help with Cryptosystem
I need to see the ASCII binary value of each character.
Re: Need help with Cryptosystem
Quote:
see the ASCII binary value of each character.
To print out the value of a char in binary use the Integer class's toBinaryString() method.
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
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));
Re: Need help with Cryptosystem
Quote:
how to get XOR to work
Please explain what you are trying to do. Show what you want to input and the desired output.
For testing use a small array that is in the program instead of data read from a file. That makes it easier to debug.
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.
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.
Quote:
I need an output of 1's and 0's
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.
How you display a byte's contents does NOT change its contents.