Cipher
Requirements:
Create a Cipher program to:
Create a cipher from a predefined set of alpha or alpha-numerics.
Create String of characters and numbers to use as substitutes (Key) for alphabet
Create input(Scanner) for adding Phrase(The earth is flat) and Save it
Encrypt Phrase with Key
Save Key
Save encrypted phrase
CodeSnipets:
//Randomize character string to use as Key
List<Character> looneytoons = new ArrayList<Character>();
for(int i='a'; i<='z'; i++)
looneytoons.add((char)i);
Collections.shuffle(looneytoons);
//Generate Substitution Table
Map<Character, Character> substitutionTable = new HashMap<Character, Character>();
int listIndex = 0;
for(int i='a'; i<='z'; i++)
substitutionTable.put((char)i,looneytoons.get(listIndex++));
substitutionTable.put(' ' , ' ');
public static String encrypt(Map<Character,Character> swapTable, String plaintext){
StringBuilder sb = new StringBuilder();
for(char character : plaintext.toCharArray())
sb.append(swapTable.get(character));
String encrypted = sb.toString();
return encrypted;
}
public static String decrypt(Map<Character,Character> swapTable, String encrypted){
Map<Character,Character> swapTableInversed = swapTable.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
StringBuilder sb2 = new StringBuilder();
for(char character : encrypted.toCharArray())
sb2.append(swapTableInversed.get(character));
String decrypted = sb2.toString();
return decrypted;
}
Steps
//Generate swap Table
//Print swap table
//Request String to Encrypt
//Encrypt
//Decrypt
//Save EncryptKey to File
//Save initial phrase to File
//Save Encrypted String to File
//Method for
//Create encrypted string
// Process to decrypt selected string