This repository was archived by the owner on Mar 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRailFenceCipher.java
More file actions
97 lines (80 loc) · 2.59 KB
/
RailFenceCipher.java
File metadata and controls
97 lines (80 loc) · 2.59 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
class RailFenceBasic{
String Encryption(String plainText,int depth)throws Exception
{
int r=depth,len=plainText.length();
int c=len/depth;
char mat[][]=new char[r][c];
int k=0;
String cipherText="";
for(int i=0;i< c;i++)
{
for(int j=0;j< r;j++)
{
if(k!=len)
mat[j][i]=plainText.charAt(k++);
else
mat[j][i]='X';
}
}
for(int i=0;i< r;i++)
{
for(int j=0;j< c;j++)
{
cipherText+=mat[i][j];
}
}
return cipherText;
}
String Decryption(String cipherText,int depth)throws Exception
{
int r=depth,len=cipherText.length();
int c=len/depth;
char mat[][]=new char[r][c];
int k=0;
String plainText="";
for(int i=0;i< r;i++)
{
for(int j=0;j< c;j++)
{
mat[i][j]=cipherText.charAt(k++);
}
}
for(int i=0;i< c;i++)
{
for(int j=0;j< r;j++)
{
plainText+=mat[j][i];
}
}
return plainText;
}
}
class RailFence {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Ceaser Cipher\n To Encrypt given plain text enter 1\n To Decrypt given cipher text enter 2");
int n = Integer.parseInt(br.readLine());
RailFenceBasic rf = new RailFenceBasic();
Scanner scn = new Scanner(System.in);
int depth;
String plainText, cipherText, decryptedText;
if (n == 1) {
System.out.println("Enter plain text:");
plainText = scn.nextLine();
System.out.println("Enter depth for Encryption:");
depth = scn.nextInt();
cipherText = rf.Encryption(plainText, depth);
System.out.println("Encrypted text is:\n" + cipherText);
} else {
System.out.println("Enter Cipher text:");
cipherText = scn.nextLine();
System.out.println("Enter depth for Encryption:");
depth = scn.nextInt();
decryptedText = rf.Decryption(cipherText, depth);
System.out.println("Decrypted text is:\n" + decryptedText);
}
}
}