12,402,666 members (73,226 online)
alternative version

303.1K views
77 bookmarked
Posted

# RC4 Encryption Algorithm: C# Version

, 25 Sep 2003
 Rate this:
C# version of RC4 encryption algorithm.

## Introduction

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:

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.

A list of licenses authors might use can be found here

## Share

 Web Developer 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...

 First PrevNext
 DO NOT USE!! Member 114553571-Feb-16 11:49 Member 11455357 1-Feb-16 11:49
 RC4 Member 1160136712-Apr-15 16:17 Member 11601367 12-Apr-15 16:17
 Difference between rc4 algorithms. PHS24116-Apr-13 9:42 PHS241 16-Apr-13 9:42
 Re: Difference between rc4 algorithms. futebolb20-Aug-13 9:55 futebolb 20-Aug-13 9:55
 Re: Difference between rc4 algorithms. Sept 15157620-Aug-13 20:28 Sept 151576 20-Aug-13 20:28
 RC4 Encryption Algorithm: C# Version Cedric D17-May-12 4:03 Cedric D 17-May-12 4:03
 Doubt from the code gbngrg7-Dec-11 9:29 gbngrg 7-Dec-11 9:29
 Hi. What does this line do? RC4Engine.cs ```82 output[offset] = (byte)((int)a^(int)b); ``` Appreciate the help. Thank you
 A Bug in it...? Harsh Gandhi16-Apr-10 2:09 Harsh Gandhi 16-Apr-10 2:09
 help me . cliffkhoo28-Sep-09 22:14 cliffkhoo 28-Sep-09 22:14
 Another implementation Alexander Müller25-Jul-09 6:11 Alexander Müller 25-Jul-09 6:11
 Re: Another implementation Member 737877118-Dec-10 12:13 Member 7378771 18-Dec-10 12:13
 c# code for RSA algorithm eng_m23-Mar-09 10:47 eng_m 23-Mar-09 10:47
 RC4 for encrypting files Cuchuk Sergey4-Oct-08 0:32 Cuchuk Sergey 4-Oct-08 0:32
 Re: RC4 for encrypting files Cuchuk Sergey4-Oct-08 1:18 Cuchuk Sergey 4-Oct-08 1:18
 A comment about the algorithm Roaa Mohammed28-Apr-08 23:37 Roaa Mohammed 28-Apr-08 23:37
 Re: A comment about the algorithm Veener15-May-09 3:21 Veener 15-May-09 3:21
 RC4 va PIC microcontroller 16F877X devoice24-Sep-07 9:19 devoice 24-Sep-07 9:19
 Unicode characters ? user_205-Mar-07 16:18 user_20 5-Mar-07 16:18
 Doesn't work blahblah_mskp22-Mar-06 11:45 blahblah_mskp 22-Mar-06 11:45
 Re: Doesn't work Simone Spagna21-Feb-07 0:39 Simone Spagna 21-Feb-07 0:39
 Re: Doesn't work Veener15-May-09 3:22 Veener 15-May-09 3:22
 Thanks pizzy7224-Dec-05 5:08 pizzy72 24-Dec-05 5:08
 A bug? AntoninoLuis23-Oct-05 23:23 AntoninoLuis 23-Oct-05 23:23
 Re: A bug? Simone Spagna21-Feb-07 0:47 Simone Spagna 21-Feb-07 0:47
 Many Thanks Dick Walker4-Oct-05 14:01 Dick Walker 4-Oct-05 14:01
 RC4? (encryption) RC4 craxy22-Jun-05 13:36 RC4 craxy 22-Jun-05 13:36
 Re: RC4? (encryption) Behind The Scene7-Aug-06 4:13 Behind The Scene 7-Aug-06 4:13
 I want code encript password lethanhdaobk14-Jun-05 0:47 lethanhdaobk 14-Jun-05 0:47
 Re: I want code encript password Behind The Scene18-Jan-07 7:14 Behind The Scene 18-Jan-07 7:14
 have a proble to encrypt files like bmp,gif,doc shekharamle28-Mar-05 20:49 shekharamle 28-Mar-05 20:49
 How I can save and load the encrypted text ? Anonymous20-Feb-05 10:21 Anonymous 20-Feb-05 10:21
 Re: How I can save and load the encrypted text ? Seemu21-Jul-05 3:49 Seemu 21-Jul-05 3:49
 Re: How I can save and load the encrypted text ? Zedoc29-Nov-05 0:10 Zedoc 29-Nov-05 0:10
 Re: How I can save and load the encrypted text ? parkerbl1-May-07 12:05 parkerbl 1-May-07 12:05
 Re: How I can save and load the encrypted text ? Lucenty25-Nov-09 0:50 Lucenty 25-Nov-09 0:50
 Design error? dr.joe22-Jan-05 8:55 dr.joe 22-Jan-05 8:55
 RC4 algorithm & explanation needed princekamalesh12-Dec-04 23:21 princekamalesh 12-Dec-04 23:21
 Question about cipher javababe21-Oct-04 10:54 javababe 21-Oct-04 10:54
 about the program surgeproof27-Feb-04 22:54 surgeproof 27-Feb-04 22:54
 Re: about the program Anonymous27-Feb-04 23:06 Anonymous 27-Feb-04 23:06
 Re: about the program Simone Spagna27-Feb-04 23:08 Simone Spagna 27-Feb-04 23:08
 looks like someone beat you to it :) smallguy7829-Oct-03 5:23 smallguy78 29-Oct-03 5:23
 Re: looks like someone beat you to it :) Simone Spagna29-Oct-03 8:26 Simone Spagna 29-Oct-03 8:26
 Nice for a start, but still a lot todo peter_s11126-Sep-03 2:26 peter_s111 26-Sep-03 2:26
 Re: Nice for a start, but still a lot todo Simone Spagna26-Sep-03 2:46 Simone Spagna 26-Sep-03 2:46
 Re: Nice for a start, but still a lot todo poupou1-Oct-03 2:42 poupou 1-Oct-03 2:42
 Re: Nice for a start, but still a lot todo Simone Spagna1-Oct-03 2:44 Simone Spagna 1-Oct-03 2:44
 Re: Nice for a start, but still a lot todo peter_s1111-Oct-03 23:23 peter_s111 1-Oct-03 23:23
 Last Visit: 31-Dec-99 18:00     Last Update: 27-Jul-16 20:09 Refresh 12 Next »