Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

RC4 Encryption and Decryption with Java

1707dca5-027a-43ee-b455-dab49b5e4f43Nov 6 2016 — edited Nov 6 2016

Good day Pros,

Please, I have a project to encrypt a given string with RC4 and decrypt it back to it's original text with Java.

I have been able to encrypt but I need your guide in decrypting the code.

I could print out the encrypted code by simply calling PRGA() or setting it to a variable.

My challenge is how to decrypt it. What I'm doing right now is to print back the original text, but I need to decrypt the encrypted code back to the original text.

Thanks.

My code is below:

package PRGA;

import java.util.Scanner;

public class Rc4 {

    //global

    public static int SIZE = 256;

    public static int[] s1 = new int[SIZE + 1]; //filled with random numbers

    public static int[] s2 = new int[SIZE + 1]; //filled with keytext

    public static int i, j;

    public static String key, input;

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        System.out.println("RC4 Encryption/Decryption");

        System.out.print("Enter message to encrypt/decrypt:");

        input = keyboard.nextLine();

        System.out.print("Enter key :");

        key = keyboard.nextLine();

        INIT();

        KSA();

        PRGA();

        print();

    }

    static void INIT() {

        j = 0;

        for (i = 0; i < SIZE; i++) {

            if (j == key.length())

                j = 0;

            s2[i] = (int) (key.charAt(j++));

        }

    }

    static void KSA() {

        for (i = 0; i < SIZE; i++)

            s1[i] = i;

        j = 0;

        for (int i = 0; i < SIZE; i++) {

            j = (j + s1[i] + SIZE) % SIZE;

            swap(i, j);

        }

    }

    static void PRGA() {

        int Rand = 0;

        j = 0;

        i = SIZE;

        String fullCode = "";

        for (int x = 0; x < input.length(); x++) {

            i = (i + 1) % SIZE;

            j = (j + s1[i] + SIZE) % SIZE;

            swap(i, j);

            Rand = (char) s1[((s1[i] + s1[j]) % SIZE)];

            fullCode += (char) (input.charAt(x) ^ Rand);

            System.out.print((char) (input.charAt(x) ^ Rand));

        }

        System.out.print("\n");

        System.out.print(fullCode);

    }

    static void print() {

        System.out.print("\n");

        for (int y = 0; y < input.length(); y++) {

            System.out.print(input.charAt(y));

        }

        System.out.print("\n");

    }

    static void swap(int i, int j) {

        int temp = s1[i];

        s1[i] = s1[j];

        s1[j] = temp;

    }

}

Thank you.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 4 2016
Added on Nov 6 2016
0 comments
3,212 views