-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRC4.java
73 lines (73 loc) · 1.27 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
package cns;
import java.io.*;
import java.util.*;
class RC4
{
public static void main(String args[])throws Exception
{
int temp=0;
String ptext;
String key;
int s[]=new int[256];
int k[]=new int[256];
Scanner in=new Scanner(System.in);
System.out.println("ENTER PLAIN TEXT");
ptext=in.next();
System.out.println("ENTER KEY TEXT");
key=in.next();
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;
}
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];
cipher[l]=z^ptexti[l];
decrypt[l]=z^cipher[l];
}
System.out.println("ENCRYPTED:" );
display(cipher);
System.out.println("\nDECRYPTED:");
display(decrypt);
}
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]);
}
}
}