Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
Java
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="";
		//	System.out.println(plainWord);
		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) {
        
        //get all map keys using keySet method
        Set<K> keys = map.keySet();
        
        //iterate all keys
        for(K key : keys){
            
            //if maps value for the current key matches, return the key
            if( map.get(key).equals(value) ){
                return key;
            }            
        }
        
        //if no values matches, return null
        return null;        
    }
}
Posted
Updated 30-Jan-21 22:22pm
Comments
Richard MacCutchan 31-Jan-21 5:05am    
If you have a database of words you can use the random function to generate (or find) alphabetic letters. You can then use those to do lookups in the database.

1 solution

Advice: do not use the same random as a mean to set the key to Cesar encoding and as a mean to choose the word in list.
Java
String[] plainTextWords={"and","now","mean","hey"};
int shift = (int)(Math.random()*10);
System.out.println(shift);
int plainWordIndex=shift%4;

Use a different random for each.
By the your shift can be 0 with this code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900