javaencryptioncharactertreemap

I am tryng to create a simple Cypher program for my assigment but it doesn't worK


I want to create a message, be able to encode it with a key and decode it with 2 functions : encode and decode. I do not understand why it doesn't work.

I tried to shift my character with a number key. I put the characters in a TreeMAp so that it stays in order. I tried with 1 TreeMap and it didn't work so I decided to create 2 Treemaps eventually where I use one character as the key and the encrypted message as the value, for the first and vice versa for the the decrypted message as the treeMAp. The method decryption should give me the value of the decrypted TreeMAp if the key corespond to the character of the encrypted message.

public class Main {
    public static void main(String[] args) {
        String message = "I love Java";
        String encodedMessage = EncryptionDecryption.encrypton(message);
        String decodedMessage = EncryptionDecryption.decryption(encodedMessage);
        System.out.println(message);
        System.out.println(encodedMessage);
        System.out.println(decodedMessage);
    }
}
import java.util.Map;
import java.util.TreeMap;

public class EncryptionDecryption {
    private static int key = 12;
    private static Map <Character, Character> encryptedText = new TreeMap<>();
    private static Map <Character, Character> decryptedText = new TreeMap<>();

    public static String encrypton(String messageToEncode) {
        for(int i = 0; i < messageToEncode.length(); i++) {
            
            encryptedText.put(messageToEncode.charAt(i), (char) (messageToEncode.charAt(i) + key));
            decryptedText.put((char) (messageToEncode.charAt(i) + key), messageToEncode.charAt(i));
        }

        StringBuilder sb =  new StringBuilder();
        for(Map.Entry<Character, Character> x : encryptedText.entrySet()) {
            sb.append(x.getValue());
        }
        String encryptedMessage = sb.toString();
        return encryptedMessage;
    }

    public static String decryption(String messageToDecode) {
        StringBuilder sb =  new StringBuilder();

        for(Map.Entry<Character, Character> x : decryptedText.entrySet()) {
            for (int i = 0; i < messageToDecode.length(); i++) {
                if (messageToDecode.charAt(i) == x.getKey()) {
                    sb.append(x.getValue());
                }
            }
        }

        String decryptedMessage = sb.toString();
        return decryptedMessage;
    }
}

edit : thanks for the answers. I tried with a HashMap. It didn't work. It gives me :

I love Java

,mqUVx{

aevIJlo


Solution

  • The TreeMap sorts the entries into alphabetically order (first upper case letter than lower case letters. Thats why you get an unexpetect result.

    The returned encrypted message is the encryption of IJaelov which you also get back.

    Iterate over the strings will help you:

    public class EncryptionDecryption {
        private static int key = 12;
        private static Map<Character, Character> encryptedText = new TreeMap<>();
        private static Map <Character, Character> decryptedText = new TreeMap<>();
    
        public static String encrypton(String messageToEncode) {
            for(int i = 0; i < messageToEncode.length(); i++) {
    
                encryptedText.put(messageToEncode.charAt(i), (char) (messageToEncode.charAt(i) + key));
                decryptedText.put((char) (messageToEncode.charAt(i) + key), messageToEncode.charAt(i));
            }
    
            StringBuilder sb =  new StringBuilder();
            for(int j = 0; j<messageToEncode.length();j++) {
                sb.append(encryptedText.get(messageToEncode.charAt(j)));
            }
            String encryptedMessage = sb.toString();
            return encryptedMessage;
        }
    
        public static String decryption(String messageToDecode) {
            StringBuilder sb =  new StringBuilder();
    
            for (int i = 0; i < messageToDecode.length(); i++) {
                    sb.append(decryptedText.get(messageToDecode.charAt(i)));
            }
    
            String decryptedMessage = sb.toString();
            return decryptedMessage;
        }
    

    }

    BTW: You should learn how to debug your code, which is very helpful in such cases.