Just Code it!

RC4 Cypher Algorithm in C#

February 8th, 2011 | Posted by riccardotramma in .NET | Cryptography | CSharp | Visual Studio
Comment or Leave a Message

Please Comment or Leave a Message if this is what you are looking for or if you have any question/comment/suggestion/request.

RC4 is the most widely used symmetric cypher algorithm, especially given its performances.

The Key is generally between 5 and 16 bytes, equivalent to 40-128 bits.


To initialise the algorithm it is used a permutation of all 256 possible byte values using the Key-Scheduled algorithm.

The procedure is quite simple. Assuming that we have our key stored in an array that we can access (let’s call this array simply “key”), we have to:

  • Initialise an array with the identity permutation of the 256 values (let’s name this array simply as “buffer” for now)
  • Loop through each array element (let’s assume that “i” is the index of the array for the element currently processed) in this way:
    • Use another index “j” (initialised to 0) and add to this index the element in the i-th position (buffer[i])
    • Add to the result the content of the key buffer in the i-th mod key-length position (or “i % keylength” if you prefer). This essentially means the value in: key[i % keylength]
    • Ensure that we never end outside our buffer boundaries. Take the value of j % 256 (or j & 255 if it sounds cooler to you)
    • Swap buffer[i] with buffer[j]

The code sample shows this algorithm in action:


Once initialised the algorithm using our key we are ready to encode the message.

This algorithm could seem a little bit more convoluted, but it is actually quite simple.
Let’s assume that the message to protect is stored in an array called “message”. The steps are those:

  • Reset i and j to 0
  • For each byte of the message that we want to encode do the following:
    • Increment i and keep it in our buffer boundaries: i = (i + 1) % 256
    • Add buffer[i] to j keeping the result in the array as well: j = (j + buffer[i]) % 256
    • Swap buffer[i] with buffer[j]
    • Add the values in buffer[i] and buffer[j], keep the result in the buffer array range: (buffer[i]+buffer[j]) % 256
    • Use it (let’s name it “encryptionByteIndex”) as an index in buffer itself: encryptionByte = buffer[encryptionByteIndex]
    • Use the value of the buffer in this position to xor the current byte of the message: encryptedMessageByte = message[currentIndex] ^ encryptionByte
    • Store this in an encryptedMessage buffer: encryptedMessage[currentIndex] = encryptedMessageByte

At the end of this algorithm the message will be encrypted in the buffer encryptedMessageBuffer. In order to decrypt it we can follow the same procedure using this buffer as our input buffer.

This is one code sample (in this code I will use only one buffer for the message and its encryption):

If you want to see where this could be useful have a look at my other blog entry here: Crypt in C++ and Decrypt in C# (and C++).

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Leave a Reply

Your email address will not be published.