-
Notifications
You must be signed in to change notification settings - Fork 2
/
_2_PlayfairCipher.java
154 lines (123 loc) · 3.82 KB
/
_2_PlayfairCipher.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.company.ISC;
import java.util.Arrays;
import java.util.Scanner;
public class PlayfairCipher {
static String removeDuplicate(String s) {
int j, index = 0, len = s.length();
char[] c = s.toCharArray();
for (int i = 0; i < len; i++) {
for (j = 0; j < i; j++) {
if (c[i] == c[j]) break;
}
if (i == j) c[index++] = c[i];
}
s = new String((Arrays.copyOf(c, index)));
return s;
}
static String removeWhiteSpace(char[] ch, String key) {
char[] c = key.toCharArray();
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < ch.length; j++) {
if (c[i] == ch[j]) {
c[i] = ' ';
break;
}
}
}
key = new String(c);
key = key.replaceAll(" ", "");
return key;
}
static String makePair(String pt) {
String s = "";
for (int i = 0; i < pt.length(); i++) {
if (pt.charAt(i) == ' ') continue;
else {
s += pt.charAt(i);
}
if (i < pt.length() - 1)
if (pt.charAt(i) == pt.charAt(i + 1))
s += "x";
}
if (s.length() % 2 != 0)
s += "x";
System.out.println(s);
return s;
}
static int[] findIJ(char a, char b, char[][] x) {
int[] y = new int[4];
if (a == 'j') a = 'i';
else if (b == 'j') b = 'i';
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (x[i][j] == a) {
y[0] = i;
y[1] = j;
} else if (x[i][j] == b) {
y[2] = i;
y[3] = j;
}
}
}
if (y[0] == y[2]) {
y[1] += 1;
y[3] += 1;
} else if (y[1] == y[3]) {
y[0] += 1;
y[2] += 1;
}
for (int i = 0; i < 4; i++) y[i] %= 5;
return y;
}
static String encrypt(String pt, char[][] x) {
char[] ch = pt.toCharArray();
int[] a;
for (int i = 0; i < pt.length(); i += 2) {
if (i < pt.length() - 1) {
a = findIJ(pt.charAt(i), pt.charAt(i + 1), x);
if (a[0] == a[2]) {
ch[i] = x[a[0]][a[1]];
ch[i + 1] = x[a[0]][a[3]];
} else if (a[1] == a[3]) {
ch[i] = x[a[0]][a[1]];
ch[i + 1] = x[a[2]][a[1]];
} else {
ch[i] = x[a[0]][a[3]];
ch[i + 1] = x[a[2]][a[1]];
}
}
}
pt = new String(ch);
return pt;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Plain text : ");
String pt = sc.nextLine();
System.out.print("Enter key : ");
String key = sc.next();
key = removeDuplicate(key);
char[] ch = key.toCharArray();
String st = "abcdefghiklmnopqrstuvwxyz";
st = removeWhiteSpace(ch, st);
char[] c = st.toCharArray();
char[][] x = new char[5][5];
int indexOfSt = 0, indexOfKey = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (indexOfKey < key.length())
x[i][j] = ch[indexOfKey++];
else
x[i][j] = c[indexOfSt++];
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++)
System.out.print(x[i][j] + " ");
System.out.println();
}
pt = makePair(pt);
pt = encrypt(pt, x);
System.out.println(pt);
}
}