All pastes #974957 Raw Edit

Anonymous

public java v1 · immutable
·published 2008-04-07 05:21 UTC
rendered paste body
/** * CeasarShiftEncryption contains all the algorythms to encrypt and decrypt a user inputted * string. *  *  * @author (your name)  * @version (a version number or a date) */import java.lang.reflect.Array;import java.util.ArrayList;import java.util.Scanner;public class CeasarShiftEncryption{	private int strLength;    //used to hold the string to encrypt/decrypt length.	private String nums[]={"0","1","2","3","4","5","6","7","8","9","10","11","12",							"13","14","15","16","17","18","19","20","21","22","23",							"24","25","26","27"}; //corresponds to the alph[] array as a index	private String alph[]={" ","a","b","c","d","e","f","g","h","i","j","k","l","m",							"n","o","p","q","r","s","t","u","v","w","x","y","z",","};  //corresponds to the nums[] array 	private ArrayList<Integer> numCon = new ArrayList<Integer>();  //stores the string in segments in order to manipulate.	private int max; //max of nums array (just in case user decides to add a few extra characters).      	//************************************************************************	CeasarShiftEncryption(int length)	{		strLength=length;    				int tempn = 0;		int i;		max = 0; //find max of array (used for flexibility later on, if alpha/nums array are modified with extra characters.				for(i = 0; i < nums.length; i++)			tempn = Integer.parseInt(nums[i]);				if(tempn > max)			max=tempn;	}		//************************************************************************	public static void findArrayOfNums(String sent, CeasarShiftEncryption s)	{	s.numCon.clear(); //Clear the numCon  ArrayList from any elements.     String temp;    	for(int counter = 0; counter < s.nums.length; counter++) //Convert sentence string into numbers string.			sent = sent.replaceAll("(?i)" + s.alph[counter], s.nums[counter] + " ");			Scanner scan = new Scanner(sent); //Scan the string number by number and convert to integer		while(scan.hasNext())		{			temp = scan.next();			s.numCon.add(Integer.parseInt(temp));		}	}		//************************************************************************	public static String Encrypt(CeasarShiftEncryption s)	{		String encrypted = "";		for(int i = 0; i < s.numCon.size(); i++) // runs through the numCon which contains integers which shifts the int's +3 (if the number exceeds array capacity it is added 3 as regular to the original int, then subtracted the by the max.		{			s.numCon.set(i, s.numCon.get(i) + 3);			if(s.numCon.get(i) > s.max)				s.numCon.set(i, s.numCon.get(i) - s.max);		}		for(int i = 0; i < s.numCon.size(); i++) //  matches each integer of numCon to the alph[] by indexing.			encrypted += s.alph[s.numCon.get(i)];				return encrypted;	}		//************************************************************************    	public static String Decrypt(CeasarShiftEncryption s)	{		String decrypted = "";				for(int i = 0; i < s.numCon.size(); i++)		{			s.numCon.set(i, s.numCon.get(i) - 3);			if(s.numCon.get(i) < 0)				s.numCon.set(i, s.numCon.get(i) + s.max);		}				for(int i = 0; i < s.numCon.size(); i++)			decrypted += s.alph[s.numCon.get(i)];				return decrypted;	}}/** * CaeserShiftTester The main of Caeser Shift Cipher, prompts a user to pick wether * to encrypt or decrypt, then calls the corresponding methods from CaesarShiftEncryption. * Ceaser Shift Cipher was thought out with flexibility, not so much 'neatness' so sorry if it looks  * a bit messy! *  * @author Twirk3d * @version 4/6/08 **/import java.util.Scanner;public class CaeserShiftTester{	public static void main(String[] args)	{		System.out.println("\t\t\tWelcome to Caesar Shift Cipher");		System.out.println("\t\t\t      By Twirk3d           ");		System.out.println();				Scanner scan = new Scanner(System.in);		boolean loop = true;		boolean loop2 = true;		String answer;		String sent;		while(loop)		{			while(loop2)			{				System.out.println("What would you like to do?");				System.out.println("a) Encrypt a message.");				System.out.println("b) Decrypt a message.");				System.out.print("a or b?: ");				answer = scan.next();				if(answer.equalsIgnoreCase("a"))				{					System.out.println("Please enter a message to encrypt.");					sent = scan.next();					sent += scan.nextLine();										CeasarShiftEncryption shift = new CeasarShiftEncryption(sent.length());					shift.findArrayOfNums(sent,shift);					String encryptedStr = shift.Encrypt(shift);					System.out.println("Encrypted Strin: "+encryptedStr);					System.out.println();					System.out.println();				}				else if(answer.equalsIgnoreCase("b"))				{					System.out.println("Please enter message to decrypt");					sent = scan.next();					sent += scan.nextLine();										CeasarShiftEncryption shift = new CeasarShiftEncryption(sent.length());					shift.findArrayOfNums(sent, shift);					System.out.println("Decrypted Strin: " + shift.Decrypt(shift));					System.out.println();					System.out.println();				}								System.out.print("Would you like to go again?(Y/N): ");				answer=scan.next();								/*				if(answer.equalsIgnoreCase("y"))					loop2 = true;				else					loop2 = false;				*/				// kryptn changed for simplicity; original above				if(answer.equalsIgnoreCase("n"))					loop2 = false;			}						System.out.print("Are you sure you want to quit?(Y/N) ");			answer = scan.next();						// again.. changed for simplicity			if(answer.equalsIgnoreCase("n"))				loop2 = true;			else				loop = false;		}	}}