Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To encrypt, XOR the value k with the next block of plaintext. To decrypt, XOR th

ID: 3564405 • Letter: T

Question

To encrypt, XOR the value k with the next block of plaintext. To decrypt, XOR the value k with the next block of ciphertext.

Program Requirements: The program should prompt the user to enter the key length (in blocks), the key (key length number of blocks), size of input (in blocks) and the plain text (size number of blocks). The program should then display the cipher text as well as the decrypted cipher text (which is the plain text).

Initialization for i 0 to 255 do S [i] T [i] Kli mod keylen); Initial Permutation of S for i 0 to 255 do j (j S [i] T [i]) mod 256 Swap (S [i], S [j] Stream Generation while (true) i (i 1) mod 256 j E (j S [i]) mod 256 Swap (S [i] s [j] t E (s [i] S [j]) mod 256 S [t]

Explanation / Answer

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];
DataInputStream in=new DataInputStream(System.in);
System.out.print(" ENTER PLAIN TEXT ");
ptext=in.readLine();
System.out.print(" ENTER KEY TEXT ");
key=in.readLine();
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(" ENCRYPTED: ");
display(cipher);
System.out.print(" DECRYPTED: ");
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]);
}
}
} /*
RC4 encryption algorithm Program Output :

ENTER PLAIN TEXT RC4 PROGRAM

ENTER KEY TEXT A

ENCRYPTED: ??-??

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote