-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesarCipher.java
More file actions
48 lines (38 loc) · 1.71 KB
/
CaesarCipher.java
File metadata and controls
48 lines (38 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.Scanner;
public class CaesarCipher {
// Encrypts the input text using the shift key
public static String encrypt(String text, int shift) {
StringBuilder result = new StringBuilder();
shift = shift % 26; // Normalize shift to stay within the range of the alphabet
for (char character : text.toCharArray()) {
if (Character.isLetter(character)) {
char base = Character.isUpperCase(character) ? 'A' : 'a';
char encryptedChar = (char) ((character - base + shift + 26) % 26 + base);
result.append(encryptedChar);
} else {
result.append(character); // Non-alphabetic characters remain unchanged
}
}
return result.toString();
}
// Decrypts the input text using the shift key
public static String decrypt(String text, int shift) {
return encrypt(text, 26 - (shift % 26)); // Decrypt by reversing the shift
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Caesar Cipher Implementation");
System.out.print("Enter the text to encrypt: ");
String plaintext = scanner.nextLine();
System.out.print("Enter the shift key (integer): ");
int shift = scanner.nextInt();
scanner.nextLine(); // Consume newline
// Encrypt the plaintext
String encryptedText = encrypt(plaintext, shift);
System.out.println("Encrypted Text: " + encryptedText);
// Decrypt the text
String decryptedText = decrypt(encryptedText, shift);
System.out.println("Decrypted Text: " + decryptedText);
scanner.close();
}
}