Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Random;
- public class Cipher {
- public static final int TOTAL_CHAR = 27;
- public static final char[] ALPHABET = {' ','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'};
- public static void main(String[] args) {
- boolean finished = true;
- do {
- System.out.print("Please select the following action('decrpyt'/'encrpyt') or enter 'quit' to exit:");
- Scanner myScanner = new Scanner(System.in);
- String selectedAction = myScanner.next();
- char[] cipher = createCipher(ALPHABET);
- if(selectedAction.equals("encrpyt"))
- {
- System.out.print("Enter the word you need to encrpyt:");
- myScanner = new Scanner(System.in);
- String enteredString = myScanner.nextLine().toLowerCase();
- String encrpytedString = encrpyt(enteredString,cipher);
- System.out.println(encrpytedString);
- }
- else if(selectedAction.equals("decrpyt"))
- {
- System.out.print("Enter the word you need to decrpyt:");
- myScanner = new Scanner(System.in);
- String enteredString = myScanner.nextLine();
- String encrpytedString = decrpyt(enteredString,cipher);
- System.out.println(encrpytedString);
- }
- else if(selectedAction.equals("quit"))
- {
- finished = false;
- }
- else
- {
- System.out.print("Please enter 'decrpyt' or 'encrpyt' to select your action.");
- }
- }while(finished);
- }
- public static char[] createCipher(char[] alphabet)
- {
- Random generator = new Random(TOTAL_CHAR-1);
- int[] indexList = new int[TOTAL_CHAR];
- char[] cipher = new char[TOTAL_CHAR];
- int cipherIndex,count = 0;
- while(count<TOTAL_CHAR)
- {
- cipherIndex = generator.nextInt(TOTAL_CHAR);
- if(indexList[cipherIndex]==0)
- {
- indexList[cipherIndex] = 1;
- cipher[count] = alphabet[cipherIndex];
- count++;
- }
- }
- return cipher;
- }
- public static String encrpyt(String word,char[] cipher)
- {
- char[] characterArray1 = word.toCharArray();
- char[] encrpytedCharacter = new char[characterArray1.length];
- int tempIndex,index = 0;
- while(index<characterArray1.length)
- {
- boolean done = true;
- for(int count = 0; ((count<ALPHABET.length)&&(done));count++)
- {
- if(characterArray1[index] == ALPHABET[count])
- {
- tempIndex = count;
- done = false;
- encrpytedCharacter[index] = cipher[count];
- }
- }
- index++;
- }
- String encrpytedString = new String(encrpytedCharacter);
- return encrpytedString;
- }
- public static String decrpyt(String encrpytWord,char[] cipher)
- {
- char[] characterArray2 = encrpytWord.toCharArray();
- char[] decrpytedCharacter = new char[characterArray2.length];
- int tempIndex,index = 0;
- while(index<characterArray2.length)
- {
- boolean done = true;
- for(int count = 0; ((count<cipher.length)&&(done));count++)
- {
- if(characterArray2[index] == cipher[count])
- {
- tempIndex = count;
- done = false;
- decrpytedCharacter[index] = ALPHABET[count];
- }
- }
- index++;
- }
- String decrpytedString = new String(decrpytedCharacter);
- return decrpytedString;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement