Click here to Skip to main content
15,435,939 members
Articles / Programming Languages / C#
Posted 25 Sep 2003


77 bookmarked

RC4 Encryption Algorithm: C# Version

Rate me:
Please Sign up or sign in to vote.
4.75/5 (49 votes)
25 Sep 20033 min read
C# version of RC4 encryption algorithm.


The security of data has become a recurrent topic in computer science. I think all software developers in their careers have to study that topic. I always keep informed about that, and I apply various kind of algorithms into the several applications customers ask me to develop.

One of the algorithms I frequently use is the RC4.

RC4 is a stream cipher symmetric key algorithm. It was developed in 1987 by Ronald Rivest and kept as a trade secret by RSA Data Security. On September 9, 1994, the RC4 algorithm was anonymously posted on the Internet on the Cyperpunks’ “anonymous remailers” list.

RC4 uses a variable length key from 1 to 256 bytes to initialize a 256-byte state table. The state table is used for subsequent generation of pseudo-random bytes and then to generate a pseudo-random stream which is XOR-ed with the plaintext to give the cipher text. Each element in the state table is swapped at least once.

The RC4 key is often limited to 40 bits, because of export restrictions but it is sometimes used as a 128 bit key. It has the capability of using keys between 1 and 2048 bits. RC4 is used in many commercial software packages such as Lotus Notes and Oracle Secure SQL. It is also part of the Cellular Specification.

I’ve noticed that nobody provided a C# version of RC4 algorithm, so I’ve done it.

Algorithm description

The RC4 algorithm works in two phases:

  1. key setup
  2. ciphering.

Key setup

Key setup is the first and most difficult phase of this algorithm. During a N-bit key setup (N being your key length), the encryption key is used to generate an encrypting variable using two arrays, state and key, and N-number of mixing operations. These mixing operations consist of swapping bytes, modulo operations, and other formulae.

In the attached project you can see how I do it in the EncryptionKey set property of RC4Engine class.

Chiphering phase

Once the encrypting variable is produced from the key setup, it enters the ciphering phase, where it is XOR-ed with the plain text message to create an encrypted message. XOR is the logical operation of comparing two binary bits. If the bits are different, the result is 1. If the bits are the same, the result is 0. Once the receiver gets the encrypted message, he decrypts it by XOR-ing the encrypted message with the same encrypting variable.

In the attached project you can see how I do it in the RC4Engine class:

  • Encrypt: encript method
  • Decrypt: decript method

I want to remark that the cripted message comes decrypted using the algorithm used in the encryption phase.

How does the application work?

The front-end layout is the following:

Sample screenshot

You can:

  1. Enter in In Clear Text Box, a text that has maximum 32767 characters
  2. Click the Encrypt button that appears after you have filled the In Clear Text Box.
  3. To verify the Crypted Text Box you can click the Decrypt button that appears at the end of the encryption process.


You can also modify the encryption key but make sure to use it for both encrypting and decrypting the text.

Written By
Web Developer
Italy Italy
I'm a senior software engineer specialized in data access ( odbc, oledb and .net ), resource dispensers and security software development.
My favourite programming languages are C#, C++.NET, C++ and Java. I'm used also to work with MFC, ATL, STL, lex & yacc and regular expressions.

Comments and Discussions

SuggestionDO NOT USE!! Pin
Member 114553571-Feb-16 11:49
MemberMember 114553571-Feb-16 11:49 
QuestionRC4 Pin
Member 1160136712-Apr-15 16:17
MemberMember 1160136712-Apr-15 16:17 
QuestionDifference between rc4 algorithms. Pin
Septimus Hedgehog16-Apr-13 9:42
MemberSeptimus Hedgehog16-Apr-13 9:42 
AnswerRe: Difference between rc4 algorithms. Pin
futebolb20-Aug-13 9:55
Memberfutebolb20-Aug-13 9:55 
GeneralRe: Difference between rc4 algorithms. Pin
Septimus Hedgehog20-Aug-13 20:28
MemberSeptimus Hedgehog20-Aug-13 20:28 
QuestionRC4 Encryption Algorithm: C# Version Pin
Cedric D17-May-12 4:03
MemberCedric D17-May-12 4:03 
QuestionDoubt from the code Pin
gbngrg7-Dec-11 9:29
Membergbngrg7-Dec-11 9:29 
QuestionA Bug in it...? Pin
Harsh Gandhi16-Apr-10 2:09
MemberHarsh Gandhi16-Apr-10 2:09 
Generalhelp me . Pin
cliffkhoo28-Sep-09 22:14
Membercliffkhoo28-Sep-09 22:14 
GeneralLicense, copyright and attribution terms Pin
Joanne WIlson12-Aug-09 4:44
MemberJoanne WIlson12-Aug-09 4:44 
GeneralRe: License, copyright and attribution terms Pin
Simone Spagna12-Aug-09 5:51
MemberSimone Spagna12-Aug-09 5:51 
GeneralAnother implementation Pin
Alexander Müller25-Jul-09 6:11
MemberAlexander Müller25-Jul-09 6:11 
GeneralRe: Another implementation Pin
Member 737877118-Dec-10 12:13
MemberMember 737877118-Dec-10 12:13 
Generalc# code for RSA algorithm Pin
eng_m23-Mar-09 10:47
Membereng_m23-Mar-09 10:47 
GeneralRC4 for encrypting files Pin
drweb864-Oct-08 0:32
Memberdrweb864-Oct-08 0:32 
GeneralRe: RC4 for encrypting files Pin
drweb864-Oct-08 1:18
Memberdrweb864-Oct-08 1:18 
GeneralA comment about the algorithm Pin
Roaa Mohammed28-Apr-08 23:37
MemberRoaa Mohammed28-Apr-08 23:37 
GeneralRe: A comment about the algorithm Pin
Veener15-May-09 3:21
MemberVeener15-May-09 3:21 
QuestionRC4 va PIC microcontroller 16F877X Pin
devoice24-Sep-07 9:19
Memberdevoice24-Sep-07 9:19 
QuestionUnicode characters ? Pin
user_205-Mar-07 16:18
Memberuser_205-Mar-07 16:18 
GeneralDoesn't work Pin
Delete_Me_Mukul22-Mar-06 11:45
MemberDelete_Me_Mukul22-Mar-06 11:45 
GeneralRe: Doesn't work Pin
Simone Spagna21-Feb-07 0:39
MemberSimone Spagna21-Feb-07 0:39 
GeneralRe: Doesn't work Pin
Veener15-May-09 3:22
MemberVeener15-May-09 3:22 
GeneralThanks Pin
User 117202124-Dec-05 5:08
MemberUser 117202124-Dec-05 5:08 
QuestionA bug? Pin
AntoninoLuis23-Oct-05 23:23
MemberAntoninoLuis23-Oct-05 23:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.