ceaser cipher
public class CaesarCipher {
public static String encrypt(String text, int shift) {
StringBuilder result = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isLetter(c)) {
char base = Character.isUpperCase(c) ? 'A' : 'a';
result.append((char)((c - base + shift) % 26 + base));
} else {
result.append(c);
}
}
return result.toString();
}
public static String decrypt(String text, int shift) {
return encrypt(text, 26 - shift);
}
public static void main(String[] args) {
String message = "HELLO WORLD";
int shift = 3;
String encrypted = encrypt(message, shift);
String decrypted = decrypt(encrypted, shift);
System.out.println("Caesar Cipher");
System.out.println("Original : " + message);
System.out.println("Encrypted: " + encrypted);
System.out.println("Decrypted: " + decrypted);
}
}