-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeistelCipher.java
More file actions
30 lines (30 loc) · 1.13 KB
/
FeistelCipher.java
File metadata and controls
30 lines (30 loc) · 1.13 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
import java.util.Scanner;
public class FeistelCipher {
public static int feistelFunction(int halfBlock, int key) {
return halfBlock ^ key;
}
public static String encrypt(String plainText, int key) {
int L = plainText.charAt(0);
int R = plainText.charAt(1);
int newL = R;
int newR = L ^ feistelFunction(R, key);
return "" + (char)newL + (char)newR;
}
public static String decrypt(String cipherText, int key) {
int L = cipherText.charAt(0);
int R = cipherText.charAt(1);
int newR = L;
int newL = R ^ feistelFunction(L, key);
return "" + (char)newL + (char)newR;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a two-character plain text: ");
String plainText = scanner.nextLine();
int key = 5;
String cipherText = encrypt(plainText, key);
System.out.println("Encrypted Text: " + cipherText);
String decryptedText = decrypt(cipherText, key);
System.out.println("Decrypted Text: " + decryptedText);
}
}