import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter message: ");
String message = scanner.nextLine();
System.out.print("Enter shift value (N): ");
int shift = scanner.nextInt();
String encrypted = encrypt(message, shift);
System.out.println("Encrypted Message: " + encrypted);
String decrypted = encrypt(encrypted, 26 - (shift % 26));
System.out.println("Decrypted Message: " + decrypted);
scanner.close();
}
public static String encrypt(String text, int shift) {
StringBuilder result = new StringBuilder();
for (char character : text.toCharArray()) {
if (Character.isLetter(character)) {
char base = Character.isUpperCase(character) ? 'A' : 'a';
// Calculate new position: (current - base + shift) % 26 + base
char shifted = (char) ((character - base + shift) % 26 + base);
result.append(shifted);
} else {
result.append(character);
}
}
return result.toString();
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: