-
Notifications
You must be signed in to change notification settings - Fork 0
/
RailFenceCipherEncryption.java
51 lines (51 loc) · 2.01 KB
/
RailFenceCipherEncryption.java
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
49
50
51
import java.util.Scanner;
public class RailFenceCipherEncryption {
// Function to encrypt a message using the Rail Fence Cipher
public static String encrypt(String message, int rails) {
// Create a 2D array to represent the rail fence
char[][] railFence = new char[rails][message.length()];
// Initialize the rail fence with dots
for (int i = 0; i < rails; i++) {
for (int j = 0; j < message.length(); j++) {
railFence[i][j] = '.';
}
}
// Fill in the characters in a zigzag pattern
int row = 0;
boolean down = true;
for (int i = 0; i < message.length(); i++) {
railFence[row][i] = message.charAt(i);
if (row == 0) {
down = true;
} else if (row == rails - 1) {
down = false;
}
if (down) {
row++;
} else {
row--;
}
}
// Read the characters row by row to get the encrypted message
StringBuilder encryptedMessage = new StringBuilder();
for (int i = 0; i < rails; i++) {
for (int j = 0; j < message.length(); j++) {
if (railFence[i][j] != '.') {
encryptedMessage.append(railFence[i][j]);
}
}
}
return encryptedMessage.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the message to encrypt: ");
String message = scanner.nextLine();
System.out.print("Enter the number of rails: ");
int rails = scanner.nextInt();
// Encrypt the message
String encryptedMessage = encrypt(message, rails);
// Display the encrypted message
System.out.println("Encrypted message: " + encryptedMessage);
}
}