4 Replies - 1194 Views - Last Post: 20 April 2009 - 09:24 AM Rate Topic: -----

#1 atribone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-April 09

Encryption Program

Posted 17 April 2009 - 11:24 AM

I've been writing an encryption program and I'm stuck on the last part. Here is an example of the encryption I would like to do:

Suppose we want to convert two ASCII characters (say “TV”) from the plain text.

’T’ → ASCII 84 → subtract 32 gives 52 → 71*52+2*54 = 3800 → (mod 97) gives 17 → add 32 gives 49
’V’ → ASCII 86 → subtract 32 gives 54 → 2*52+26*54 = 1508 → (mod 97) gives 53 → add 32 gives 85

Now 49 is the ASCII code of char ‘1’ and 85 is the ASCII code of char ‘U’. So the pair “TV” gets converted to the pair “1U”.

Let’s apply the same transformation to the string of two characters “1U”.

’1’ → ASCII 49 → subtract 32 gives 17 → 71*17+2*53 = 1313 → (mod 97) gives 52 → add 32 gives 84
’U’ → ASCII 85 → subtract 32 gives 53 → 2*17+26*53 = 1412 → (mod 97) gives 54 → add 32 gives 86

My program so far reads:

public class MountainCipher {

	private int numberOfChars;
	private int[] plainText;
	private int[] cipherText;
	private String plainFile;
	private String cipherFile;

	public MountainCipher(String fileName1, String fileName2) throws FileNotFoundException {
		plainFile = fileName1; 
		cipherFile = fileName2;
		numberOfChars = 0;
		int numOfLines = 0;

		File myFile = new File(plainFile);
		Scanner inputFile = new Scanner(myFile);

		String next = "";
		while (inputFile.hasNextLine()) {	   //calulates length of text and accounts for the two spots necessary to determine the end of that line
			next += inputFile.nextLine();
			numOfLines++;
		}
		inputFile.close();

		numberOfChars = next.length() + (numOfLines * 2);

		//System.out.println(numberOfChars);
		//System.out.println(numOfLines);

		plainText = new int[numberOfChars];	 //allocates memory for the two arrays
		cipherText = new int[numberOfChars];
	}

	public void readPlainText() throws FileNotFoundException {
		File myFile = new File(plainFile);
		Scanner inputFile = new Scanner(myFile);
		char c;
		int i;
		String next = "";

		do {										//take in the characters from the file and adds qz where line breaks should be
			next += inputFile.nextLine();		   
			//System.out.println(next);
			next += "qz";
			//System.out.println(next);
		} while (inputFile.hasNextLine());

		for (int k = 0; k < numberOfChars; k++) {		   //replaces qz with ASCII 13 and ASC 10 and puts next into plainText array
			if (next.charAt(k) == 'q' && next.charAt(k + 1) == 'z') {
				plainText[k] = 13;
				k++;
				plainText[k] = 10;
			} else {
				c = next.charAt(k);
				i = (int) c;
				plainText[k] = i;
			}
		}
	}

	public void transform() throws FileNotFoundException {
		int oldX;
		int oldY;
		int newX;
		int newY;
		
		for (int i = 0; i < plainText.length; i = i + 2) {
			oldX = plainText[i];		//gets pair of characters to encrypt
			oldY = plainText[i + 1];

			if (oldX == 169 || oldX == 174) {   //character code of 127 and 128 are non-printable characters
				if (oldX == 169) {			  //change 127 to 169 and 128 to 174
					oldX = 127;
				} else {
					oldX = 128;
				}
			}
			if (oldY == 169 || oldY == 174) {
				if (oldY == 169) {
					oldY = 127;
				} else {
					oldY = 128;
				}
			}

			if (oldX == 13) {	   //if at end of line no need to change ASCII 13 and ASCII 10
				cipherText[i] = 13;
				cipherText[i + 1] = 10;
			} else if (oldY == 13) {		//if line has uneven number of characters applies a perdetermined transformation
				oldX = oldX - 32;
				oldX = oldX * 96;
				oldX = oldX % 97;
				oldX = oldX + 32;

				if (oldX == 127 || oldX == 128) {   
					if (oldX == 127) {
						oldX = 169;
					} else {
						oldX = 174;
					}
				}

				cipherText[i] = oldX;
				cipherText[i + 1] = 13;
				cipherText[i + 2] = 10;
				i++;
			} else {			//applies normal transformation within a line of text

				oldX = oldX - 32;
				oldY = oldY - 32;

				newX = 71 * oldX + 2 * oldY;
				newY = 2 * oldX + 26 * oldY;

				newX = newX % 97;
				newY = newY % 97;

				newX = newX + 32;
				newY = newY + 32;

				if (newX == 127 || newX == 128) {
					if (newX == 127) {
						newX = 169;
					} else {
						newX = 174;
					}
				}
				if (newY == 127 || newY == 128) {
					if (newY == 127) {
						newY = 169;
					} else {
						newY = 174;
					}
				}
				
				cipherText[i] = newX;
				cipherText[i + 1] = newY;
			}
			//System.out.print(cipherText[i] + " " + cipherText[i+1] + " ");
		}
	}
	
	public void writeCipherText() throws IOException{
		FileWriter outFile = new FileWriter(cipherFile);
		PrintWriter out = new PrintWriter(outFile);
		int i = 0;
		char c;
		
		for(int k = 0; k < cipherFile.length(); k++){   //takes ASCII code and converts it back to characters and writes it to file
			i = cipherText[k];
			c = (char)i;
			System.out.print(c);
			out.print(c);
		}
		
		out.close();
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException {
		MountainCipher myCipher = new MountainCipher("file1.txt", "file2.txt");		 //driver
		myCipher.readPlainText();
		myCipher.transform();
		myCipher.writeCipherText();
	}
}



When I run the program on the file:

Mary had a little lamb whose fleece was white as snow.
Georgie Porgie puddin pie kiss the girls and made them cry.
Old King Cole was a merry old soul.
1, 2, 3 slap my knee. 4, 5, 6 pick up sticks.

My output is:

;BsUO=©WA

That is correct for the first 9 characters, but I have two questions, why would it do only 9 characters when the encryption step is dependent on a pair of characters? And why is it stopping after 9?

Thanks for the help.

This post has been edited by atribone: 17 April 2009 - 11:33 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Encryption Program

#2 atribone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-April 09

Re: Encryption Program

Posted 17 April 2009 - 11:36 AM

Also another question I have is about the part where I put qz to mark the end of a line. This is not correct, I believe, because you should be able to put an encrypted file through this program and decrypt it. qz may be a part of that encrypted file. Any suggestions about what place holder I should use there?
Was This Post Helpful? 0
  • +
  • -

#3 brwilliams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-April 09

Re: Encryption Program

Posted 17 April 2009 - 12:34 PM

The problem is in the function readPlainText() at the end of the for loop plainText is only 9 characters in length.


Why do you need a place holder at all?
Was This Post Helpful? 0
  • +
  • -

#4 brwilliams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-April 09

Re: Encryption Program

Posted 17 April 2009 - 01:14 PM

Actually readPlainText is ok. The problem is in writeCipherText().

You have the following line of code:

for(int k = 0; k < cipherFile.length(); k++){ //takes ASCII code and converts it back to characters and writes it to file


It should be cipherText.length instead of cipherFile.length()

I changed this and it worked fine.
Was This Post Helpful? 0
  • +
  • -

#5 atribone  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-April 09

Re: Encryption Program

Posted 20 April 2009 - 09:24 AM

View Postbrwilliams, on 17 Apr, 2009 - 11:34 AM, said:

The problem is in the function readPlainText() at the end of the for loop plainText is only 9 characters in length.


Why do you need a place holder at all?

At the end of a line you need to add ASCII code 13 and ASCII code 10. I need to add a place holder when reading the text so the program knows where the end of the line is. I just chose qz because that wouldn't occur in the English language, but than I remembered that you can also put encrypted files through this program and they may contain qz.

Thanks for you help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1