-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayfair.java
More file actions
149 lines (132 loc) · 3.07 KB
/
Playfair.java
File metadata and controls
149 lines (132 loc) · 3.07 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
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
public class Playfair
{
static int SIZE = 30;
static void toLowerCase(char plain[], int ps)
{
int i;
for (i = 0; i < ps; i++) {
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32;
}
}
static int removeSpaces(char[] plain, int ps)
{
int i, count = 0;
for (i = 0; i < ps; i++)
if (plain[i] != '\u0000')
plain[count++] = plain[i];
return count;
}
static void generateKeyTable(char key[], int ks, char keyT[][])
{
int i, j, k;
int dicty[] = new int[26];
for (i = 0; i < ks; i++) {
if (key[i] != 'j')
dicty[key[i] - 97] = 2;
}
dicty['j' - 97] = 1;
i = 0;
j = 0;
for (k = 0; k < ks; k++) {
if (dicty[key[k] - 97] == 2) {
dicty[key[k] - 97] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5) {
i++;
j = 0;
}
}
}
for (k = 0; k < 26; k++) {
if (dicty[k] == 0) {
keyT[i][j] = (char)(k + 97);
j++;
if (j == 5) {
i++;
j = 0;
}
}
}
}
static void search(char keyT[][], char a, char b, int arr[])
{
int i, j;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
}
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
}
}
}
}
static int mod5(int a) { return (a % 5); }
static int prepare(char str[], int ptrs)
{
if (ptrs % 2 != 0) {
str[ptrs++] = 'z';
str[ptrs] = '\0';
}
return ptrs;
}
static void encrypt(char str[], char keyT[][], int ps)
{
int i;
int[] a =new int[4];
for (i = 0; i < ps; i += 2) {
search(keyT, str[i], str[i + 1], a);
if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod5(a[1] + 1)];
str[i + 1] = keyT[a[0]][mod5(a[3] + 1)];
}
else if (a[1] == a[3]) {
str[i] = keyT[mod5(a[0] + 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] + 1)][a[1]];
}
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
}
static void encryptByPlayfairCipher(char str[], char key[])
{
int ps;
int ks;
char[][] keyT = new char[5][5];
ks = key.length;
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
ps = str.length;
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
ps = prepare(str, ps);
generateKeyTable(key, ks, keyT);
encrypt(str, keyT, ps);
}
static void strcpy(char[] arr, String s) {
for(int i = 0;i < s.length();i++){
arr[i] = s.charAt(i);
}
}
public static void main(String[] args) {
char str[] = new char[SIZE];
char key[] = new char[SIZE];
strcpy(key, "projectx");
System.out.println("Key text: " + String.valueOf(key));
strcpy(str, "attack pearl harbor");
System.out.println("Plain text: " + String.valueOf(str));
encryptByPlayfairCipher(str, key);
System.out.println("Cipher text: " + String.valueOf(str));
}
}