Click here to Skip to main content
15,895,084 members
Articles / Programming Languages / Java
Tip/Trick

RC4 (Rivest Cipher 4)

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
19 Jul 2016CPOL1 min read 9.5K   240   1  
Java implementation for the RC4 stream cipher algorithm, the RC4 is implemented using the IDE 8.1

What is RC4

RC4 is a stream cipher designed at 1978 by Ron Rivest for RSA security, and stayed secret until RC4 posted anonymously at September 1994 .RC4 used in standard SSL\TLS for communication between client and server, and involved in WEP protocol and newer WPA protocol that is part at IEEE 802.11 WLAN standards.

Algorithm

Encrypting n numbers of bytes using RC4 algorithm based on encrypting each 256 bytes together and moving to next block after that until finishing the whole data.The following are the steps of encrypting each block of data:

  1. Choose variable length key from 1 to 256 bytes (8 to 2048 bits)
  2. The chosen key is used for initialization S vector (S[0],S[1], ... ,S[255])
  3. Permutation of S vector using the key for generating RC4 key
  4. The elements of RC4 reordered
  5. The elements of plain text Xored using the RC4 key

Using the Code

The source code contains the class RC4.java that contains the main methods for encrypting the message, and the main class calls the startCiphering method at RC4.java for starting the ciphering of plain text. The code reads the messages and start ciphering the 256 blocks at the plain text one by one.

At the beginning in the initialization method, get block of 256 bytes from plain text and initialize S vector with size 256. After that, the elements of S vector is permutated.

Java
for (; Ptr1 < MessageBlockSize; Ptr1++) {
    intUnSignedByteKey = getUnsigendByte(T_signedByteKey[Ptr1]);
    Ptr2 = (Ptr2 + S[Ptr1] + intUnSignedByteKey) % MessageBlockSize;
    // use the pointers to swap two elements
    temp = S[Ptr1];
    S[Ptr1] = S[Ptr2];
    S[Ptr2] = temp;
}

At GenerateRC4Key(), the elements of S vector is permutated using the key for generating the RC4 key...

Java
// 2nd Permutation of S to generate the secret keys
        for (int index = 0; index < MessageBlockSize; index++) {
            Ptr1 = (Ptr1 + 1) % MessageBlockSize;
            Ptr2 = (Ptr2 + S[index]) % MessageBlockSize;
            // permutation elements
            temp = S[Ptr1];
            S[Ptr1] = S[Ptr2];
            S[Ptr2] = temp;
            ////
            t = (S[Ptr1] + S[Ptr2]) % MessageBlockSize;
            RC4Key[index] = S[t];
        }

...encrypting the plain text at Encryption() method, by Xoring the elements at S vector and elements at plain text.

Java
for (int index = 0; index < MesssageBlock.length; index++) {
       intCipherByte = ((int) MesssageBlock[index] ^ (int) RC4Key[index]);
       CipherBlock[index] = (byte) intCipherByte;
       char x = (char) CipherBlock[index];
       System.out.print(x);
   }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
Jordan Jordan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --