Question: In this assignment, take the provided Java template file and finish the implementation of RC4 algorithm. You will find 1 java file with several methods

In this assignment, take the provided Java template file and finish the implementation of RC4 algorithm.

You will find 1 java file with several methods declared.

  • The main() method is already done. It will use "Secret" as the key and attempt to encrypt the message "Attack at dawn" using RC4 and then decrypt the message back.
  • The encrypt() method is already done. It will check that the key is between 5 to 16 bytes, call the genKSA method to generate the key schedule, call the genKeyStream method to generate the stream of key bytes, and then perform the encryption based on the produced key stream.

Your task is to provide the definitions for three methods:

  • genKSA - generates the key scheduling algorithm according to the algorithm provided on Slide 17 from the notes.
  • genKeyStream - generates the key stream using the created schedule, according to the algorithm provided on Slide 19 from the notes.
  • decrypt - decrypts the ciphertext according to the key. Think carefully about an easy way to do this!!!

Once you're done, run and check the output. Read the Wikipedia entry on RC4 to see if your generated ciphertext is correct.

import java.util.ArrayList; import java.util.Collections;

public class RC4 {

/** * Encrypt plaintext with key. Place output in ciphertext. */ public void encrypt(ArrayList plaintext, ArrayList key, ArrayList ciphertext) {

//Ensure key length is between 5 to 16 bytes assert key.size() >= 5 : "Key too short"; assert key.size() <= 16 : "Key too long";

//Create our state s and set key schedule ArrayList s = new ArrayList(); genKSA(key, s); //create a key stream and call genKeyStream() to create the key bytes // according to the schedule s ArrayList keyStream = new ArrayList(); for (int i = 0; i < 256; i++) keyStream.add(i); genKeyStream(s, keyStream, plaintext.size()); //For debugging System.out.print("keyStream: "); for (int c=0; c < plaintext.size(); c++) System.out.print(Integer.toHexString(keyStream.get(c))+" "); System.out.println(); //encrypt the plaintext using the key stream for (int i = 0; i < plaintext.size(); i++) { ciphertext.add(plaintext.get(i) ^ keyStream.get(i)); }

}

/** * Decrypt cyphertext with key. Place output in plaintext. */ public void decrypt(ArrayList ciphertext, ArrayList key, ArrayList plaintext) {

// FILL IN CODE // THINK BEFORE YOU CODE THIS... This should be very short }

/** * Generate the schedule s corresponding to the key. */ private void genKSA(ArrayList key, ArrayList s) {

// FILL IN CODE (Refer to the algorithm from notes or from the RC4 Wikipedia entry) }

/** * Generate a key stream keyStream of numBytes bytes, using the schedule s. */ private void genKeyStream(ArrayList s, ArrayList keyStream, int numBytes) {

// FILL IN CODE (Refer to the algorithm from notes or from the RC4 Wikipedia entry) }

/** * Encrypt the phrase "Attack at dawn" using key "Secret". Use the * ciphertext and the key to recover the plaintext in order to verify your * answer. * * DO NOT MODIFY */ public static void main(String[] args) { RC4 rc4 = new RC4();

//Use "Secret" as the key ArrayList key = new ArrayList(); String k = "Secret"; for (int i=0; i < k.length(); i++) { key.add((int)k.charAt(i)); }

//Set up our plaintext "Attack at dawn" ArrayList pt = new ArrayList(); String p = "Attack at dawn"; for (int i=0; i < p.length(); i++) { pt.add((int)p.charAt(i)); }

//Call the encrypt() method and print out the hex representation ArrayList ct = new ArrayList(pt.size()); rc4.encrypt(pt, key, ct); System.out.print("Ciphertext: "); for (int c=0; c < ct.size(); c++) System.out.print(Integer.toHexString(ct.get(c))+" "); System.out.println();

//Call the decrypt() method and reproduce the original plaintext pt.clear(); rc4.decrypt(ct, key, pt); System.out.print("Recovered plaintext: "); for (int c=0; c < pt.size(); c++) System.out.print((char) pt.get(c).intValue()); System.out.println(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!