Help with Vigener Cipher Project

Input file -> Encrypt or Decrypt file -> Output file

Page 1 of 1

6 Replies - 10331 Views - Last Post: 25 October 2010 - 01:40 PM Rate Topic: -----

#1 Zarithras   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Help with Vigener Cipher Project

Posted 24 October 2010 - 05:06 PM

Hello all! I'm a first time user at this forum. I'm taking a java class at my university so I can begin my career in computer science! I appreciate any help that can be given to me ahead of time. Anyways, I'll get right to the point. I've been instructed to write a program to encrypt and decrypt a file using a Vigenere Cipher. For those of you who do not know how this Cipher works, refer to http://en.wikipedia....%C3%A8re_cipher for information. I have all of the code working except for the actual cipher part, as in the encrypting and decrypting of a file. The goal for writing this code is to let the user decide whether they want to encrypt or decrypt a file. The user is to enter in the file that they want to be encrypted / decrypted, enter in the key to be used in the Cipher, and then enter in the name of the file to which they want the newly encrypted / decrypted file to be stored in. The program will prompt the user to encrypt / decrypt for as many times as they like until they type in "q" upon which the program will exit. I'll post the code for you to see.

 import java.util.*;
import java.io.*;

/**
 * Cipher is a program that can encrypt and decrypt any given text file. It encrypts and decrypts
 * using a Vigenere Cipher.
 * @author Jordan Curd
 */
 
 public class Cipher
 {
 	/**
 	 * Starts the program
 	 * @param args
 	 */
 	public static void main (String [] args)
 	{
 		//header that explains the application and instructs the user on how to use it
 		System.out.println();
 		System.out.println("Cipher is an application that can encrypt or decrypt a file using a Vigenere");
 		System.out.println("Cipher. In order to use this application, first declare whether you would like");
 		System.out.println("to have a file encrypted or decrypted by following the prompt on the screen.");
 		System.out.println("Next, you'll be asked for the name of the file to be encrypted or decrypted.");
 		System.out.println("After doing so, enter in the key to which the file will be encrypted or decrypted");
 		System.out.println("with. Finally, enter in the name of the file that you would like your output file");
 		System.out.println("to be named. You may encrypt or decrypt as many filesas you like. In order to exit");
 		System.out.println("the program, follow the prompt on the screen. Enjoy!");
 		
 		userInterface(); //interface with the user
 	}
 	
 	public static void userInterface()
 	{
 		boolean run = true;
 		while(run)
 		{
 			Scanner console = new Scanner(System.in);
 			System.out.println();
 			System.out.print("Would you like to (E)ncrypt or (D)ecrypt a message? Or (Q)uit? ");
 			String userin = console.next();
 			
 			if (userin.equalsIgnoreCase("e"))
 			{
 				String key = getKey(console);
 				Scanner inputFile = getInputScanner(console);
 				PrintStream output = getOutputPrintStream(console);
 				encrypt(key, inputFile, output);
 			}
 			else if (userin.equalsIgnoreCase("d"))
 			{
 				String key = getKey(console);
 				Scanner inputFile = getInputScanner(console);
 				PrintStream output = getOutputPrintStream(console);
 				decrypt(key, inputFile, output);
 			}
 			else if (userin.equalsIgnoreCase("q"))
 			{
 				run = false;
 			}
 			else
 			{
 				System.out.println("Incorrect Value. Please try again.");
 			}
 		}
 	}
 	
 	/** Encrypts the file that the user desires and creates a new file containing the
 	  * encrypted text.
 	  * @param key the key to be used in encryption
 	  * @param input the file to be encrypted
 	  * @param output the file to which the encrypted file will be saved
 	  */
 	public static void encrypt(String key, Scanner input, PrintStream output)
 	{
 		while (input.hasNextLine())
 		{
 			String s = encryptLine(key, input.nextLine());
 			output.println(s);
 		}
 	}
 	
 	/** Decrypts the file that the user desires and creates a new file containing the
 	  * decrypted text.
 	  * @param key the key to be used in decryption
 	  * @param input the file to be decrypted
 	  * @param output the file to which the decrypted file will be saved
 	  */
 	public static void decrypt(String key, Scanner input, PrintStream output)
 	{
 		while (input.hasNextLine())
 		{
 			String s = decryptLine(key, input.nextLine());
 			output.println(s);
 		}
 	}
 	
 	/** Encrypts a line of the given file
 	  * @param key the key to be used in encryption
 	  * @param line the line to be encrypted
 	  * @return the encrypted line
 	  */
 	public static String encryptLine(String key, String line)
 	{
 		Scanner readline = new Scanner(line);
 		String newline = null;
 		while (readline.hasNext())
 		{
 			for (int i = 0 ; i < key.length() ; i++)
 			{
 				if (readline.hasNext())
 				{
 					String s = readline.next();						
 					char ch = s.charAt(0);
 					if ((ch + (key.charAt(i) - 'A')) <= 'Z')
 					{
 						newline += ch + (key.charAt(i) - 'A');
 					}
 					else if ((ch + (key.charAt(i) - 'A')) > 'Z')
 					{
 						newline += 'A' + ('Z' % ch);
 					}
 				}
 			}
 		}

 		return newline;
 	}
 	
 	/** Decrypts a line of the given file
 	  * @param key the key to be used in decryption
 	  * @param line the line to be decrypted
 	  * @return the decrypted line
 	  */
 	public static String decryptLine(String key, String line)
 	{
 		Scanner readline = new Scanner(line);
 		String newline = null;
 		while (readline.hasNext())
 		{
 			for (int i = 0 ; i < key.length() ; i++)
 			{
 				if (readline.hasNext())
 				{
 					String s = readline.next();				
 					char ch = s.charAt(0);
 					if ((ch - (key.charAt(i) - 'A')) >= 'A')
 					{
 					newline += ch - (key.charAt(i) - 'A');
 					}
 					else if ((ch + (key.charAt(i) - 'A')) < 'A')
 					{
 						newline += 'Z' - (ch % 'A');
 					}
 				}
 			}
 		}
 		return newline;
 	}
 	
 	/**
 	 * Prompts the user for the key to be used in the encryption / decryption
 	 * @param console scanner to read the user's input
 	 * @return the key to be used
 	 */
 	public static String getKey(Scanner console)
 	{
 		System.out.print("Key? ");
 		String key = console.next();
 		while ((!key.equals(key.toUpperCase())) || (hasNumber(key) == true))
 		{
 			System.out.println("Invalid key (must consist of uppercase letters). Please try again.");
 			System.out.print("Key? ");
 			key = console.next();
 		}
 		return key;
 	}
 	
 	/**
 	 * Checks the user's key for numbers
 	 * @param s the string entered in by the user as the key
 	 * @return true if string has numbers, false if string doesn't have numbers
 	 */
 	public static boolean hasNumber(String s) 
 	{
 		for (int i = 0 ; i < s.length() ; i++)
 		{
 			if (Character.isDigit(s.charAt(i))) 
 			{
 				return true; 
 			}
 		}
 		return false;
 	}
 	
 	/**
	  * Reads filename from user, then return a PrintStream
	  * @param scanner for input from the console
	  * @return a scanner to read input from the file
	  */
 	public static Scanner getInputScanner(Scanner console)
	{
		String filename;
		Scanner in = null;
		System.out.print("Input filename? ");
		while (in == null) 
		{
			filename = console.next();
			try 
			{
				in = new Scanner(new File(filename));		
			} 
			catch (FileNotFoundException e) 
			{
				System.out.println(e.getMessage());
				System.out.print("Input filename? ");
			}
		}
		return in;
	}
	
	/**
	  * Reads filename from user, then return a PrintStream
	  * @param console for input from the console
	  * @return a PrintStream to write to a file
	  */
	public static PrintStream getOutputPrintStream(Scanner console) {
		
		String filename = null;
		PrintStream out = null;
		String overwrite = null;
		System.out.print("Output filename? ");
		while (out == null) 
		{
			filename = console.next();
			
			try 
			{			
				File file = new File(filename);
			
				if (file.exists()) 
				{
					System.out.print("File already exists, OK to overwrite (y,n)? ");
					
					overwrite = console.next();
					if (overwrite.startsWith("y") || overwrite.startsWith("Y")) 
					{
						out = new PrintStream(file);
					} 
					else if (overwrite.startsWith("n") || overwrite.startsWith("N"))
					{
						System.out.print("Output filename? ");
						out = null;
					}
					else
					{
						System.out.print("Incorrect input. Try again. ");
						System.out.print("Output filename? ");
						out = null;
					}
				}
				else 
				{
					out = new PrintStream(file);
				}
			
			}
			catch (FileNotFoundException e) 
			{
				System.out.println(e.getMessage());
				System.exit(1);
			}
		}
		return out;
	}
 } 
 


I've spent a great deal of time on this code. It seems to work perfectly from what I've tested it with, aside from the actual Cipher'ing, of course. The program handles errors correctly, handles the user's input correctly, and everything else. I just need some help figuring out what I'm doing wrong with the actual encryption / decryption. To narrow it down a little bit, refer to the methods titled encryptLine() and decryptLine(). The encrypt() and decrypt() methods utilize a while loop to check if the file has a next line, and if it does, it will send the line as an entire string to the methods encryptLine() or decryptLine() to be encrypted / decrypted. I believe everything else is working as intended. I was very lost when I began typing the code for those methods so the code is potentially entirely wrong for what I'm trying to do.

Again, I appreciate any help that you all can give me!

Is This A Good Question/Topic? 0
  • +

Replies To: Help with Vigener Cipher Project

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Help with Vigener Cipher Project

Posted 24 October 2010 - 06:31 PM

I will have a look at what is wrong in your code
Meanwhile you can have a look at that tutorial:

http://www.dreaminco...igenere-square/

Vigenere square is the second one of my multiple tutorials about Secret Code

Howver this is what I've got trying to encript your Cipher.java whith key: ABCDEF

null155155
null155155
null
null47
null426815515515515515515515515515515515515575155155155
null421551558971
null42657670
null42
null
null15515569
null155
null47
null4284155155
null4265155
null42
null15515515515544155155
null155
null47155155155155155155155155155155155155155
null83
null8315515515515515515515515515515515515587
null8374155155155155155155155155155155155
null83155155155155155155155155155155155155155
null83155155155155155155155155155155155155155155
null83155155155155155155155155155155155155155155155
null8371155155155155155155155155155155155155155155
null831551556615515515515515515515515515574155155155
null8315515515515515515515571
null
null15548155155155
null155
null
null155155155155
null155
null15515563155
null155
null155
null831556315587
null83
null831551551554415540155155824439
null8315563155
null
null15541
null155
null8315563155
null8315563155
null8015563155
null155155155
null155
null15515542
null155
null8315563155
null8315563155
null8015563155
null155155155
null155
null15515542
null155
null15562155
null155
null155
null155
null838782155155
null155
null155
null155
null
null4770155155155155155155155155155155155155155
null42155155
null4265155155155155155155155155
null4265155155155155155155
null4265155155155155155155155155155155155
null42
null1551551551551558815581155
null155
null15541
null155
null8315563155155
null155
null155
null155
null
null4769155155155155155155155155155155155155155
null42155155
null4265155155155155155155155155
null4265155155155155155155
null4265155155155155155155155155155155155
null42
null1551551551551558815581155
null155
null15541
null155
null8315563155155
null155
null155
null155
null
null4770155155155155155155
null4265155155155155155155155155
null4265155155155155155155
null4265155155155
null42
null1551558515515588155
null155
null831556315587
null8315563155
null15541
null155
null155411556452641556115562155
null155
null15541
null155
null8315563155
null15515563155
null15541454349446040
null155
null1554415546445039
null155
null15515542464450396341
null155
null1554441464442155
null155
null155
null155
null155
null
null155155
null155
null
null4769155155155155155155
null4265155155155155155155155155
null4265155155155155155155
null4265155155155
null42
null1551558515515588155
null155
null831556315587
null8315563155
null15541
null155
null155411556452641556115562155
null155
null15541
null155
null8315563155
null15515563155
null15541474349446240
null155
null1554415548445039
null155
null15515542464450396141
null155
null155444148444239
null155
null155
null155
null155
null155155
null155
null
null47
null428115515515515515515515515515515515548155
null4265155155155155155155155
null4265155155155155155
null42
null15515585155155
null155
null8335
null8315563155
null155411554365155
null155
null831554215515515515581155155
null8335
null15562155
null155
null155155
null155
null
null47
null4268155155155155155
null4265155155155155155155155155155155155
null4265155155155155155155155155155155155
null42
null155155155155155
null155
null155411556452641556115562155
null155
null15541
null155
null155155
null155
null155
null155155
null155
null
null47
null428315515515515515515582
null4265155155155155155155
null4265155155155155155155155155
null42
null15515585155155
null155
null83155
null8315563155
null8315536
null1554163155
null155
null15562155
null155
null155
null155621558674
null155
null15541155
null155
null83
null8315536
null155
null155
null155155
null155
null
null47
null428315515515515515515582
null4265155155155155155155
null426515583155155155155155
null42
null15515582155155155
null
null8315563155
null8015563155
null8315563155
null8315536
null1554163155
null155
null15562155
null
null155
null155
null701556315574
null
null15541
null155
null83155155821551554035
null
null15562155
null15541155155
null155
null1556215583
null155
null15515542155155
null155
null8315536
null15562155
null155
null155
null155
null831558615538
null8315536
null15562155
null155
null155
null155
null155
null1556215583
null155
null
null155
null15541155
null155
null83
null83
null155
null155
null155155
null155
null155
null

This post has been edited by pbl: 24 October 2010 - 06:40 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Help with Vigener Cipher Project

Posted 24 October 2010 - 06:49 PM

In your encryptLine() method why do you create a Scanner object ?
Your receives the line to encrypt as parameter
just apply to it the String class method toCharArray() to make an array of char[] of the line to encrypt and then rotate the letters in that array using the key

Calling charAt() multiple times, especially to read the same char, multiple times is really not efficient

public static String encryptLine(String key, String line) {

   char[] digit = line.toCharArray();

   for(int i = 0; i < digit.length; i++) {
      char keyToUse = key.charAt(i % key.length);
      ...

   }

   return new String(digit);
}


This post has been edited by pbl: 24 October 2010 - 06:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 Zarithras   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Re: Help with Vigener Cipher Project

Posted 24 October 2010 - 08:02 PM

View Postpbl, on 24 October 2010 - 05:49 PM, said:

In your encryptLine() method why do you create a Scanner object ?
Your receives the line to encrypt as parameter
just apply to it the String class method toCharArray() to make an array of char[] of the line to encrypt and then rotate the letters in that array using the key

Calling charAt() multiple times, especially to read the same char, multiple times is really not efficient

public static String encryptLine(String key, String line) {

   char[] digit = line.toCharArray();

   for(int i = 0; i < digit.length; i++) {
      char keyToUse = key.charAt(i % key.length);
      ...

   }

   return new String(digit);
}




The reason I was using the scanner was that I was thinking that I should use the scanner look ahead methods such as hasNext() in order to traverse through the whole string. With the way I was thinking, I could just use a while loop to apply the cipher to the entire line (as long as there was another character to be looked at) and then use a for loop with the length of the key to just alter each next character. The code became messy and didn't work, as you saw. Thanks so much for the code you posted, I'm going to try and implement your suggestions right now. If anymore problems come up I'll be back to ask again. :)
Was This Post Helpful? 0
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Help with Vigener Cipher Project

Posted 24 October 2010 - 08:06 PM

View PostZarithras, on 24 October 2010 - 09:02 PM, said:

The reason I was using the scanner was that I was thinking that I should use the scanner look ahead methods such as hasNext() in order to traverse through the whole string.


Scanner.next() return a String not a character
you have a String an you know you have to parse it ignoring or not (your choice) the non alpabetic characters. As the computer does the whole job you might let it do the job for all characters
Was This Post Helpful? 0
  • +
  • -

#6 Zarithras   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Re: Help with Vigener Cipher Project

Posted 24 October 2010 - 09:32 PM

I'm running into some trouble with what you recommended. The cipher is to be applied to each next character, not including white space. In other words, if there is a space between words, the next part of the key is to be applied to the next character, not the white space in between the words. I created an empty string and just added the resulting characters onto the string and returned the string. I included an if statement after the for loop such that if the next char in the array is a space, it would just include a space in the string and not do any char math on it. However, when I did that, the cipher is thrown off. I'll repost my entire code so that you can compile and test it if you wish rather than just posting the two revised methods.

import java.util.*;
import java.io.*;

/**
 * Cipher is a program that can encrypt and decrypt any given text file. It encrypts and decrypts
 * using a Vigenere Cipher.
 * @author Jordan Curd
 */
 
 public class Cipher
 {
 	/**
 	 * Starts the program
 	 * @param args
 	 */
 	public static void main (String [] args)
 	{
 		//header that explains the application and instructs the user on how to use it
 		System.out.println();
 		System.out.println("Cipher is an application that can encrypt or decrypt a file using a Vigenere");
 		System.out.println("Cipher. In order to use this application, first declare whether you would like");
 		System.out.println("to have a file encrypted or decrypted by following the prompt on the screen.");
 		System.out.println("Next, you'll be asked for the name of the file to be encrypted or decrypted.");
 		System.out.println("After doing so, enter in the key to which the file will be encrypted or decrypted");
 		System.out.println("with. Finally, enter in the name of the file that you would like your output file");
 		System.out.println("to be named. You may encrypt or decrypt as many filesas you like. In order to exit");
 		System.out.println("the program, follow the prompt on the screen. Enjoy!");
 		
 		userInterface(); //interface with the user
 	}
 	
 	public static void userInterface()
 	{
 		boolean run = true;
 		while(run)
 		{
 			Scanner console = new Scanner(System.in);
 			System.out.println();
 			System.out.print("Would you like to (E)ncrypt or (D)ecrypt a message? Or (Q)uit? ");
 			String userin = console.next();
 			
 			if (userin.equalsIgnoreCase("e"))
 			{
 				String key = getKey(console);
 				Scanner inputFile = getInputScanner(console);
 				PrintStream output = getOutputPrintStream(console);
 				encrypt(key, inputFile, output);
 			}
 			else if (userin.equalsIgnoreCase("d"))
 			{
 				String key = getKey(console);
 				Scanner inputFile = getInputScanner(console);
 				PrintStream output = getOutputPrintStream(console);
 				decrypt(key, inputFile, output);
 			}
 			else if (userin.equalsIgnoreCase("q"))
 			{
 				run = false;
 			}
 			else
 			{
 				System.out.println("Incorrect Value. Please try again.");
 			}
 		}
 	}
 	
 	/** Encrypts the file that the user desires and creates a new file containing the
 	  * encrypted text.
 	  * @param key the key to be used in encryption
 	  * @param input the file to be encrypted
 	  * @param output the file to which the encrypted file will be saved
 	  */
 	public static void encrypt(String key, Scanner input, PrintStream output)
 	{
 		while (input.hasNextLine())
 		{
 			String s = encryptLine(key, input.nextLine());
 			output.println(s);
 		}
 	}
 	
 	/** Decrypts the file that the user desires and creates a new file containing the
 	  * decrypted text.
 	  * @param key the key to be used in decryption
 	  * @param input the file to be decrypted
 	  * @param output the file to which the decrypted file will be saved
 	  */
 	public static void decrypt(String key, Scanner input, PrintStream output)
 	{
 		while (input.hasNextLine())
 		{
 			String s = decryptLine(key, input.nextLine());
 			output.println(s);
 		}
 	}
 	
 	/** Encrypts a line of the given file
 	  * @param key the key to be used in encryption
 	  * @param line the line to be encrypted
 	  * @return the encrypted line
 	  */
 	public static String encryptLine(String key, String line)
 	{
 		char[] digit = line.toCharArray();
 		String newline = "";
 		
 		for (int i = 0 ; i < digit.length; i++)
 		{
 			if (digit[i] > ' ')
 			{
 				char keyToUse = key.charAt(i % key.length());
 				if ((digit[i] + (keyToUse - 'A')) <= 'Z')
 				{
 					newline += (char) (digit[i] + (keyToUse - 'A'));
 				}
 				else
 				{
 					newline += (char) ('A' + ((digit[i] + (keyToUse - 'A')) - 'Z'));
 				}
 			}
 			else
 			{
 				newline += " ";
 			}
 		}
 		
 		System.out.println(newline); //here just to quickly see newline
 		return newline;
 	}
 	
 	/** Decrypts a line of the given file
 	  * @param key the key to be used in decryption
 	  * @param line the line to be decrypted
 	  * @return the decrypted line
 	  */
 	public static String decryptLine(String key, String line)
 	{
 		char[] digit = line.toCharArray();
 		String newline = "";
 		
 		for (int i = 0 ; i < digit.length; i++)
 		{
 			if (digit[i] > ' ')
 			{
 				char keyToUse = key.charAt(i % key.length());
 				if ((digit[i] - (keyToUse - 'A')) >= 'A')
 				{
 					newline += (char) (digit[i] - (keyToUse - 'A'));
 				}
 				else
 				{
 					newline += (char) ('Z' - (digit[i] - (keyToUse - 'A')));
 				}
 			}
 			else 
 			{
 				newline += " ";
 			}
 		}
 		
 		return newline;
 	}
 	
 	/**
 	 * Prompts the user for the key to be used in the encryption / decryption
 	 * @param console scanner to read the user's input
 	 * @return the key to be used
 	 */
 	public static String getKey(Scanner console)
 	{
 		System.out.print("Key? ");
 		String key = console.next();
 		while ((!key.equals(key.toUpperCase())) || (hasNumber(key) == true))
 		{
 			System.out.println("Invalid key (must consist of uppercase letters). Please try again.");
 			System.out.print("Key? ");
 			key = console.next();
 		}
 		return key;
 	}
 	
 	/**
 	 * Checks the user's key for numbers
 	 * @param s the string entered in by the user as the key
 	 * @return true if string has numbers, false if string doesn't have numbers
 	 */
 	public static boolean hasNumber(String s) 
 	{
 		for (int i = 0 ; i < s.length() ; i++)
 		{
 			if (Character.isDigit(s.charAt(i))) 
 			{
 				return true; 
 			}
 		}
 		return false;
 	}
 	
 	/**
	  * Reads filename from user, then return a PrintStream
	  * @param scanner for input from the console
	  * @return a scanner to read input from the file
	  */
 	public static Scanner getInputScanner(Scanner console)
	{
		String filename;
		Scanner in = null;
		System.out.print("Input filename? ");
		while (in == null) 
		{
			filename = console.next();
			try 
			{
				in = new Scanner(new File(filename));		
			} 
			catch (FileNotFoundException e) 
			{
				System.out.println(e.getMessage());
				System.out.print("Input filename? ");
			}
		}
		return in;
	}
	
	/**
	  * Reads filename from user, then return a PrintStream
	  * @param console for input from the console
	  * @return a PrintStream to write to a file
	  */
	public static PrintStream getOutputPrintStream(Scanner console) {
		
		String filename = null;
		PrintStream out = null;
		String overwrite = null;
		System.out.print("Output filename? ");
		while (out == null) 
		{
			filename = console.next();
			
			try 
			{			
				File file = new File(filename);
			
				if (file.exists()) 
				{
					System.out.print("File already exists, OK to overwrite (y,n)? ");
					
					overwrite = console.next();
					if (overwrite.startsWith("y") || overwrite.startsWith("Y")) 
					{
						out = new PrintStream(file);
					} 
					else if (overwrite.startsWith("n") || overwrite.startsWith("N"))
					{
						System.out.print("Output filename? ");
						out = null;
					}
					else
					{
						System.out.print("Incorrect input. Try again. ");
						System.out.print("Output filename? ");
						out = null;
					}
				}
				else 
				{
					out = new PrintStream(file);
				}
			
			}
			catch (FileNotFoundException e) 
			{
				System.out.println(e.getMessage());
				System.exit(1);
			}
		}
		return out;
	}
 }



I'll give an example to make my problem more clear. If we were given a file that contained only the words "Pop Quiz Today!" the resulting encrypted file should read "Pbi Qhbz Ghdnr!". Spaces, numbers, and punctuation are to not be affected by the key, and as such, the character that was to be next used in the key should be used on the following character. I think that's why I was initially trying to use a scanner so I could just examine the next characters while preserving the white space. Any suggestions or thoughts? Thanks again.

This post has been edited by Zarithras: 24 October 2010 - 09:35 PM

Was This Post Helpful? 0
  • +
  • -

#7 Zarithras   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 8
  • Joined: 24-October 10

Re: Help with Vigener Cipher Project

Posted 25 October 2010 - 01:40 PM

i finally figured out the code to make this program work as intended. I'll post the revised encryptLine() and decryptLine() methods.

/** Encrypts a line of the given file
 	  * @param key the key to be used in encryption
 	  * @param line the line to be encrypted
 	  * @return the encrypted line
 	  */
 	public static String encryptLine(String key, String line)
 	{
		String lineupper = line.toUpperCase();
		String linelower = line.toLowerCase();
 		char[] digit = line.toCharArray();
		char[] digitupper = lineupper.toCharArray();
		char[] digitlower = linelower.toCharArray();
 		String newline = "";
		int counter = 0;
 		
 		for (int i = 0 ; i < digit.length; i++)
 		{
			char keyToUse = ' ';
			if ((digit[i] >= 'A' || digit[i] >= 'a') && (digit[i] <= 'Z' || digit[i] <= 'z') || (digit[i] == ' '))
			{
 				if (digit[i] > ' ')
 				{
					if (digit[i] == digitupper[i])
					{
						key = key.toUpperCase();
						keyToUse = key.charAt((i - counter) % key.length());
 						if ((digit[i] + (keyToUse - 'A')) <= 'Z')
 						{
 							newline += (char) (digit[i] + (keyToUse - 'A'));
 						}
 						else
 						{
 							newline += (char) ('A' + ((digit[i] + (keyToUse - 'A')) - ('Z' + 1)));
 						}
					}
					else if (digit[i] == digitlower[i])
					{
						key = key.toLowerCase();
						keyToUse = key.charAt((i - counter) % key.length());
						if ((digit[i] + (keyToUse - 'a')) <= 'z')
 						{
 							newline += (char) (digit[i] + (keyToUse - 'a'));
 						}
 						else
 						{
 							newline += (char) ('a' + ((digit[i] + (keyToUse - 'a')) - ('z' + 1)));
 						}
					}
 				}
 				else
 				{
 					newline += " ";
					counter++;
 				}
			}
			else if ((digit[i] < 'A' || digit[i] < 'a') || (digit[i] > 'Z' || digit[i] > 'z'))
			{
				newline += digit[i];
			}
			//System.out.println(i + " " + digit[i] + " " + keyToUse + " " + counter + " " + newline);
 		}
 		
 		return newline;
 	}
 	
 	/** Decrypts a line of the given file
 	  * @param key the key to be used in decryption
 	  * @param line the line to be decrypted
 	  * @return the decrypted line
 	  */
 	public static String decryptLine(String key, String line)
 	{
 		String lineupper = line.toUpperCase();
		String linelower = line.toLowerCase();
 		char[] digit = line.toCharArray();
		char[] digitupper = lineupper.toCharArray();
		char[] digitlower = linelower.toCharArray();
 		String newline = "";
		int counter = 0;
 		
 		for (int i = 0 ; i < digit.length; i++)
 		{
			char keyToUse = ' ';
			if ((digit[i] >= 'A' || digit[i] >= 'a') && (digit[i] <= 'Z' || digit[i] <= 'z') || (digit[i] == ' '))
			{
 				if (digit[i] > ' ')
 				{
					if (digit[i] == digitupper[i])
					{
						key = key.toUpperCase();
						keyToUse = key.charAt((i - counter) % key.length());
 						if ((digit[i] - (keyToUse - 'A')) >= 'A')
 						{
 							newline += (char) (digit[i] - (keyToUse - 'A'));
 						}
 						else
 						{
 							newline += (char) ('Z' - (('A' - 1) - (digit[i] - (keyToUse - 'A'))));
 						}
					}
					else if (digit[i] == digitlower[i])
					{
						key = key.toLowerCase();
						keyToUse = key.charAt((i - counter) % key.length());
						if ((digit[i] - (keyToUse - 'a')) >= 'a')
 						{
 							newline += (char) (digit[i] - (keyToUse - 'a'));
 						}
 						else
 						{
 							newline += (char) ('z' - (('a' - 1) - (digit[i] - (keyToUse - 'a'))));
 						}
					}
 				}
 				else
 				{
 					newline += " ";
					counter++;
 				}
			}
			else if ((digit[i] < 'A' || digit[i] < 'a') || (digit[i] > 'Z' || digit[i] > 'z'))
			{
				newline += digit[i];
			}
			//System.out.println(i + " " + digit[i] + " " + keyToUse + " " + counter + " " + newline);
 		}
 		
 		return newline;
 	}



Thanks for your help! You definitely got me on the right track.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1