Implement the RC4 algorithm. Suppose the key consists of the following seven bytes: (OxlA, 0x2B, 0x3C, 0x4D, 0x5E, 0x6F, 0x77). For each of the following, give S in the form of a 16 x 16 array where each entry is in hex. I am supposed to create a java code to get the output and answer the questions.
a. List the permutation S and indices i and j after the initialization phase has completed.
b. List the permutation S and indices i and j after the first 100 bytes of keystream have been generated.
c. List the permutation S and indices i and j after the first 1000 bytes of keystream have been generated
/*I came up with the below plain to cipher change algorithm .
*I think it can be useful.
*/
import java.io.* ;
import java.util.Scanner;
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 in =new Scanner(System.in );
System.out.print("\nENTER PLAIN TEXT\t");
ptext = in.nextLine();
System.out.print("\n\nENTER KEY TEXT\t\t");
key = in.nextLine();
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.print("\nENCRYPTED:\t");
display(cipher);
System.out.print("\nDECRYPTED:\t");
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]);
}
System.out.println("");
}
}
Get Answers For Free
Most questions answered within 1 hours.