import java.awt.*;
import java.lang.Process;

class CipherKey
{

	public static void main(String str[]) throws java.io.IOException
	{
		char option;
	
		char datam[] = new char[25];
		CipherFunc cFunc = new CipherFunc();	

		String key = new String();

		String text, temp;
		//StringBuffer str = new StringBuffer();
		StringBuffer str1; // = new StringBuffer();
		
		char c;
		
		cFunc.str = key = "ABCD EFG";
		
		//do
		//{
		while(true)
		{
			//Runtime r = Runtime.getRuntime();
			//Process p = r.exec("cmd /c cls");
			
			str1 = new StringBuffer();

			System.out.println("Enter the Options");
			System.out.println("******************");
			System.out.println("1 - Change the Key");
			System.out.println("2 - Encrypt a Line");
			System.out.println("3 - Decrypt a Line");
			System.out.println("4 - Quit");
			System.out.println("******************");
		
			System.out.print("Enter the choice: ");

			try
			{
				while ((c = (char)System.in.read())!= '\n')
				{
					str1.append(c);
				}
			}
			catch (Exception e)
			{
				System.out.println("Error: " + e.toString());
			}

			option = str1.charAt(0);
		
			str1 = new StringBuffer();
			
/*
			char ch;
			
			for(int i=0; i < text.length(); i++)
			{
				ch = text.charAt(i);
				datam[i] = (char)ch;
				System.out.println(datam[i]);
			}

			temp = new String(datam);
			
			System.out.println(temp);
			
			text= temp;
			
			for(int i=0; i <= str1.length(); i++)
			{
				if(str1.charAt(i)!='\n')	
					datam[i] = str1.charAt(i);
				else
				{
					datam[i] = '\n';
					break;	
				}			
				System.out.print(datam[i]+" ");
			}
			
			System.out.println("After Loop");
			
			text = new String(datam);
			
			System.out.println(">>>>>> "+text);
*/
						
			switch (option)
			{
				case '1':

					// Input New Key
					System.out.print("Enter the NewKey: ");
					try
					{
						while ((c = (char)System.in.read())!= '\n')
						{
							str1.append(c);
						}
					}
					catch (Exception e)
					{
						System.out.println("Error: " + e.toString());
					}
		
					text = new String();
					
					text = str1.toString();

					if(cFunc.changeKey(text))
					{	
						cFunc.str = text;
						System.out.println("Success");
					}
					else
					{
						cFunc.str = key;
						System.out.println("Invalid Key");
					}
					//changeKey();
					break;
					
				case '2':
										
					// Input Message for Encryption
					System.out.print("Enter the Message: ");
					try
					{
						while ((c = (char)System.in.read())!= '\n')
						{
							str1.append(c);
						}
					}
					catch (Exception e)
					{
						System.out.println("Error: " + e.toString());
					}
		
					text = new String();
					
					text = str1.toString();			

					System.out.println(cFunc.encryptLine(text));
					break;
					
				case '3':
					// Input Message for Decryption
					System.out.print("Enter the Message: ");
					try
					{
						while ((c = (char)System.in.read())!= '\n')
						{
							str1.append(c);
						}
					}
					catch (Exception e)
					{
						System.out.println("Error: " + e.toString());
					}
		
					text = new String();
					
					text = str1.toString();			

					System.out.println(cFunc.decryptLine(text));
					break;
					
				case '4':
					System.exit(0);		
			}
		}
		//while (option < '1' || option > '4');
	}
	
}