Task:
Encrypt any plaintext over the Latin alphabet using a single letter replacement cipher
Code:
String str = "BE_HAPPY"; String[] bytes = new String[] { "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", " " }; int[] key = new int[] { 18, 22, 11, 7, 15, 19, 1, 23, 20, 26, 3, 14, 25, 4, 9, 5, 2, 24, 21, 6, 0, 8, 13, 12, 16, 10, 17 }; String a = ""; String m = ""; for (String ma : str.split("")) { m = m + bytes[key[Arrays.asList(bytes).indexOf(ma)]]; } System.out.print(m); Mistake:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at L5.lab5.main (lab5.java:24)
What to do?