Click here to Skip to main content
12,069,857 members (52,668 online)
Click here to Skip to main content
Add your own
alternative version


77 bookmarked

RC4 Encryption Algorithm: C# Version

, 25 Sep 2003
Rate this:
Please Sign up or sign in to vote.
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.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Simone Spagna
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.

You may also be interested in...

Comments and Discussions

AnswerRe: A bug? Pin
Simone Spagna21-Feb-07 1:47
memberSimone Spagna21-Feb-07 1:47 
GeneralMany Thanks Pin
Dick Walker4-Oct-05 15:01
memberDick Walker4-Oct-05 15:01 
GeneralRC4? (encryption) Pin
RC4 craxy22-Jun-05 14:36
sussRC4 craxy22-Jun-05 14:36 
GeneralRe: RC4? (encryption) Pin
Behind The Scene7-Aug-06 5:13
memberBehind The Scene7-Aug-06 5:13 
GeneralI want code encript password Pin
lethanhdaobk14-Jun-05 1:47
memberlethanhdaobk14-Jun-05 1:47 
GeneralRe: I want code encript password Pin
Behind The Scene18-Jan-07 8:14
memberBehind The Scene18-Jan-07 8:14 
Generalhave a proble to encrypt files like bmp,gif,doc Pin
shekharamle28-Mar-05 21:49
membershekharamle28-Mar-05 21:49 
QuestionHow I can save and load the encrypted text ? Pin
Anonymous20-Feb-05 11:21
sussAnonymous20-Feb-05 11:21 
AnswerRe: How I can save and load the encrypted text ? Pin
Seemu21-Jul-05 4:49
memberSeemu21-Jul-05 4:49 
GeneralRe: How I can save and load the encrypted text ? Pin
Zedoc29-Nov-05 1:10
memberZedoc29-Nov-05 1:10 
QuestionRe: How I can save and load the encrypted text ? Pin
parkerbl1-May-07 13:05
memberparkerbl1-May-07 13:05 
GeneralRe: How I can save and load the encrypted text ? Pin
Lucenty25-Nov-09 1:50
memberLucenty25-Nov-09 1:50 
QuestionDesign error? Pin
dr.joe22-Jan-05 9:55
memberdr.joe22-Jan-05 9:55 
GeneralRC4 algorithm & explanation needed Pin
princekamalesh13-Dec-04 0:21
sussprincekamalesh13-Dec-04 0:21 
GeneralQuestion about cipher Pin
javababe21-Oct-04 11:54
memberjavababe21-Oct-04 11:54 
Generalabout the program Pin
surgeproof27-Feb-04 23:54
susssurgeproof27-Feb-04 23:54 
GeneralRe: about the program Pin
Anonymous28-Feb-04 0:06
sussAnonymous28-Feb-04 0:06 
GeneralRe: about the program Pin
Simone Spagna28-Feb-04 0:08
memberSimone Spagna28-Feb-04 0:08 
Generallooks like someone beat you to it :) Pin
smallguy7829-Oct-03 6:23
membersmallguy7829-Oct-03 6:23 
GeneralRe: looks like someone beat you to it :) Pin
Simone Spagna29-Oct-03 9:26
memberSimone Spagna29-Oct-03 9:26 
GeneralNice for a start, but still a lot todo Pin
peter_s11126-Sep-03 3:26
memberpeter_s11126-Sep-03 3:26 
GeneralRe: Nice for a start, but still a lot todo Pin
Simone Spagna26-Sep-03 3:46
memberSimone Spagna26-Sep-03 3:46 
GeneralRe: Nice for a start, but still a lot todo Pin
poupou1-Oct-03 3:42
memberpoupou1-Oct-03 3:42 
GeneralRe: Nice for a start, but still a lot todo Pin
Simone Spagna1-Oct-03 3:44
memberSimone Spagna1-Oct-03 3:44 
GeneralRe: Nice for a start, but still a lot todo Pin
peter_s1112-Oct-03 0:23
memberpeter_s1112-Oct-03 0:23 
GeneralRe: Nice for a start, but still a lot todo Pin
poupou2-Oct-03 2:54
memberpoupou2-Oct-03 2:54 
GeneralRe: Nice for a start, but still a lot todo Pin
Illuvatan18-Jun-04 10:13
memberIlluvatan18-Jun-04 10:13 
GeneralNice Pin
Giles25-Sep-03 23:16
memberGiles25-Sep-03 23:16 
GeneralRe: Nice Pin
Simone Spagna26-Sep-03 0:46
memberSimone Spagna26-Sep-03 0:46 
GeneralRe: Nice Pin
MasseNilsson8-Sep-05 1:47
memberMasseNilsson8-Sep-05 1:47 
GeneralRe: Nice Pin
zamnut518-Feb-08 7:19
memberzamnut518-Feb-08 7:19 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160208.1 | Last Updated 26 Sep 2003
Article Copyright 2003 by Simone Spagna
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid