-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRC.java
91 lines (78 loc) · 2.45 KB
/
RC.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
package AIM;
class RC {
static String display(int disp[]) {
char convert[] = new char[disp.length];
for (int l = 0; l < disp.length; l++) {
convert[l] = (char) disp[l];
}
String str = String.copyValueOf(convert);
return str;
}
public static String encryption(String TEXT) {
int temp = 0;
String key;
int s[] = new int[256];
int k[] = new int[256];
key = "AIMS";
char ptextc[] = TEXT.toCharArray();
char keyc[] = key.toCharArray();
int cipher[] = new int[TEXT.length()];
int decrypt[] = new int[TEXT.length()];
int ptexti[] = new int[TEXT.length()];
int keyi[] = new int[key.length()];
for (int i = 0; i < TEXT.length(); i++) {
ptexti[i] = (int) ptextc[i];
}
for (int i = 0; i < key.length(); i++) {
keyi[i] = (int) keyc[i];
}
for (int i = 0; i < 255; i++) {
s[i] = i;
k[i] = keyi[i % key.length()];
}
int j = 0;
for (int i = 0; i < 255; i++) {
j = (j + s[i] + k[i]) % 256;
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
int i = 0;
j = 0;
int z = 0;
for (int l = 0; l < TEXT.length(); l++) {
i = (l + 1) % 256;
j = (j + s[i]) % 256;
temp = s[i];
s[i] = s[j];
s[j] = temp;
z = s[(s[i] + s[j]) % 256];
cipher[l] = z ^ ptexti[l];
decrypt[l] = z ^ cipher[l];
}
// System.out.print("\n\nENCRYPTED:\t\t");
String encrypted = display(cipher);
return encrypted;
}
// public static void main(String args[]) throws IOException {
//
// BufferedReader d = new BufferedReader(new InputStreamReader(System.in));
// System.out.println("Enter text1: ");
// String ptext1 = d.readLine();
// String output1 = encryption(ptext1);
// System.out.println(output1);
//
// System.out.println("Enter text2: ");
// String ptext2 = d.readLine();
// String output2 = encryption(ptext2);
// System.out.println(output2);
//
// if(output1.compareTo(output2) == 0) {
// System.out.println("Sign in successful");
// }
// else {
// System.out.println("Incorrect text");
// }
//
}
// }