I'm trying to develop a Java program which presents Users with encrypted word and asks them to decode it. Currently I present random words from an array after encrypting them with a random Caesar shift number. Could you please advise on how this can be achieved with a bigger word source, any web service or online database i can access by index?
Thank you
What I have tried:
Thus far i am using a simple array as word source:
import java.util.Scanner;
import java.lang.Math;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class Main
{
public static void main(String[] args) {
HashMap<Integer,String> map = new HashMap<>();
map.put(0,"a");
map.put(1,"b");
map.put(2,"c");
map.put(3,"d");
map.put(4,"e");
map.put(5,"f");
map.put(6,"g");
map.put(7,"h");
map.put(8,"i");
map.put(9,"j");
map.put(10,"k");
map.put(11,"l");
map.put(12,"m");
map.put(13,"n");
map.put(14,"o");
map.put(15,"p");
map.put(16,"q");
map.put(17,"r");
map.put(18,"s");
map.put(19,"t");
map.put(20,"u");
map.put(21,"v");
map.put(22,"w");
map.put(23,"x");
map.put(24,"y");
map.put(25,"z");
String[] plainTextWords={"and","now","mean","hey"};
int shift = (int)(Math.random()*10);
System.out.println(shift);
int plainWordIndex=shift%4;
String plainWord=plainTextWords[plainWordIndex];
String encryptedWord="";
for(int i=0;i<=plainWord.length()-1;i++)
{
String letter = String.valueOf(plainWord.charAt(i));
int num1=getKeyFromValue(map,letter);
int encryptedCharIndex=(num1+shift)%26;
String encryptedLetter=map.get(encryptedCharIndex);
encryptedWord=encryptedWord+encryptedLetter;
}
System.out.println(encryptedWord);
System.out.println("Hello User, please decrypt above word and enter your answer");
Scanner sc=new Scanner(System.in);
String answer=sc.nextLine();
System.out.println(answer);
if(answer.toLowerCase().equals(plainWord))
{
System.out.println("Cheers, you cracked this Caesar's cipher");
}
else
{
System.out.println("Not yet there, please try again");
}
}
private static <K, V> K getKeyFromValue(Map<K, V> map, Object value) {
Set<K> keys = map.keySet();
for(K key : keys){
if( map.get(key).equals(value) ){
return key;
}
}
return null;
}
}