substitution
public class SubstitutionCipher {
public static String encrypt(String text, String key) {
text = text.toUpperCase();
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isLetter(c)) {
result.append(key.charAt(c - 'A'));
} else {
result.append(c);
}
}
return result.toString();
}
public static String decrypt(String text, String key) {
text = text.toUpperCase();
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isLetter(c)) {
int index = key.indexOf(c);
result.append((char)('A' + index));
} else {
result.append(c);
}
}
return result.toString();
}
public static void main(String[] args) {
String message = "HELLO WORLD";
String key = "QWERTYUIOPASDFGHJKLZXCVBNM"; // substitution key
String encrypted = encrypt(message, key);
String decrypted = decrypt(encrypted, key);
System.out.println("Substitution Cipher");
System.out.println("Original : " + message);
System.out.println("Encrypted: " + encrypted);
System.out.println("Decrypted: " + decrypted);
}
}