forked from client69/Open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrc4.java
88 lines (76 loc) · 2.37 KB
/
rc4.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
package rc4;
import java.util.Scanner;
import java.io.*;
class RC4 {
public static void main(String args[]) throws IOException {
int temp = 0;
String ptext;
String key;
int s[] = new int[256];
int k[] = new int[256];
Scanner sc=new Scanner(System.in);
DataInputStream in = new DataInputStream(System.in);
System.out.println("ENTER PLAIN TEXT");
ptext = sc.nextLine();
System.out.println("\n");
System.out.println("ENTER KEY");
key = sc.nextLine();
System.out.println("\n");
char ptextc[] = ptext.toCharArray();
char keyc[] = key.toCharArray();
int cipher[] = new int[ptext.length()];
int decrypt[] = new int[ptext.length()];
int ptexti[] = new int[ptext.length()];
int keyi[] = new int[key.length()];
for (int i = 0; i < ptext.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;
}
for(int i=0;i<255;i++)
{
System.out.print(","+s[i]);
}
System.out.print("\n");
int i = 0;
j = 0;
int z = 0;
for (int l = 0; l < ptext.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];
System.out.print("\t"+z);
cipher[l] = z ^ ptexti[l];
decrypt[l] = z ^ cipher[l];
}
System.out.print("\n");
System.out.println("ENCRYPTED");
display(cipher);
System.out.println("\n");
System.out.println("DECRYPRED");
display(decrypt);
System.out.println("\n");
}
static void display(int disp[]) {
char convert[] = new char[disp.length];
for (int l = 0; l < disp.length; l++) {
convert[l] = (char) disp[l];
System.out.print(convert[l]);
}
}
}