Click here to Skip to main content
Click here to Skip to main content

RC4 Encryption Algorithm: C# Version

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

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.

License

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

Share

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.

Comments and Discussions

 
QuestionDifference between rc4 algorithms. PinmemberPHS24116-Apr-13 9:42 
Simone,
 
A: I tried yours and used a key and clear text to encrypt and then decrypt. I get the same clear text.
 
B: I downloaded another rc4 algorithm and used the same key and clear text. That works fine.
 
When I put the encrypted text from your (A) into the decryption of (B) the clear text is different. Likewise if I put the encrypted text from (B) into (A) the clear text is different.
 
If both bits of code are supposedly RC4 compliant would you agree that providing I use the same key and clear text (which I did) I should get the same output for the encrypted and decrypted text?
 
I don't know then if there's more than one flavour of RC4 which is unlikely, or perhaps it's related to the key size or some other internal factor or perhaps one of the algorithms isn't telling the truth! Big Grin | :-D
 
How would you verify which is right and which is wrong?
If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.

AnswerRe: Difference between rc4 algorithms. Pinmemberfutebolb20-Aug-13 9:55 
GeneralRe: Difference between rc4 algorithms. PinmemberSept 15157620-Aug-13 20:28 
QuestionRC4 Encryption Algorithm: C# Version PinmemberCedric D17-May-12 4:03 
QuestionDoubt from the code Pinmembergbngrg7-Dec-11 9:29 
QuestionA Bug in it...? PinmemberHarsh Gandhi16-Apr-10 2:09 
Generalhelp me . Pinmembercliffkhoo28-Sep-09 22:14 
GeneralLicense, copyright and attribution terms PinmemberJoanne WIlson12-Aug-09 4:44 
GeneralRe: License, copyright and attribution terms PinmemberSimone Spagna12-Aug-09 5:51 
GeneralAnother implementation PinmemberAlexander Müller25-Jul-09 6:11 
GeneralRe: Another implementation PinmemberMember 737877118-Dec-10 12:13 
Generalc# code for RSA algorithm Pinmembereng_m23-Mar-09 10:47 
GeneralRC4 for encrypting files PinmemberCuchuk Sergey4-Oct-08 0:32 
GeneralRe: RC4 for encrypting files PinmemberCuchuk Sergey4-Oct-08 1:18 
GeneralA comment about the algorithm PinmemberRoaa Mohammed28-Apr-08 23:37 
GeneralRe: A comment about the algorithm PinmemberVeener15-May-09 3:21 
QuestionRC4 va PIC microcontroller 16F877X Pinmemberdevoice24-Sep-07 9:19 
QuestionUnicode characters ? Pinmemberuser_205-Mar-07 16:18 
GeneralDoesn't work Pinmemberblahblah_mskp22-Mar-06 11:45 
GeneralRe: Doesn't work PinmemberSimone Spagna21-Feb-07 0:39 
GeneralRe: Doesn't work PinmemberVeener15-May-09 3:22 
GeneralThanks Pinmemberpizzy7224-Dec-05 5:08 
QuestionA bug? PinmemberAntoninoLuis23-Oct-05 23:23 
AnswerRe: A bug? PinmemberSimone Spagna21-Feb-07 0:47 
GeneralMany Thanks PinmemberDick Walker4-Oct-05 14:01 
GeneralRC4? (encryption) PinsussRC4 craxy22-Jun-05 13:36 
GeneralRe: RC4? (encryption) PinmemberBehind The Scene7-Aug-06 4:13 
GeneralI want code encript password Pinmemberlethanhdaobk14-Jun-05 0:47 
GeneralRe: I want code encript password PinmemberBehind The Scene18-Jan-07 7:14 
Generalhave a proble to encrypt files like bmp,gif,doc Pinmembershekharamle28-Mar-05 20:49 
QuestionHow I can save and load the encrypted text ? PinsussAnonymous20-Feb-05 10:21 
AnswerRe: How I can save and load the encrypted text ? PinmemberSeemu21-Jul-05 3:49 
GeneralRe: How I can save and load the encrypted text ? PinmemberZedoc29-Nov-05 0:10 
QuestionRe: How I can save and load the encrypted text ? Pinmemberparkerbl1-May-07 12:05 
GeneralRe: How I can save and load the encrypted text ? PinmemberLucenty25-Nov-09 0:50 
QuestionDesign error? Pinmemberdr.joe22-Jan-05 8:55 
GeneralRC4 algorithm & explanation needed Pinsussprincekamalesh12-Dec-04 23:21 
GeneralQuestion about cipher Pinmemberjavababe21-Oct-04 10:54 
Generalabout the program Pinsusssurgeproof27-Feb-04 22:54 
GeneralRe: about the program PinsussAnonymous27-Feb-04 23:06 
GeneralRe: about the program PinmemberSimone Spagna27-Feb-04 23:08 
Generallooks like someone beat you to it :) Pinmembersmallguy7829-Oct-03 5:23 
GeneralRe: looks like someone beat you to it :) PinmemberSimone Spagna29-Oct-03 8:26 
GeneralNice for a start, but still a lot todo Pinmemberpeter_s11126-Sep-03 2:26 
GeneralRe: Nice for a start, but still a lot todo PinmemberSimone Spagna26-Sep-03 2:46 
GeneralRe: Nice for a start, but still a lot todo Pinmemberpoupou1-Oct-03 2:42 
GeneralRe: Nice for a start, but still a lot todo PinmemberSimone Spagna1-Oct-03 2:44 
GeneralRe: Nice for a start, but still a lot todo Pinmemberpeter_s1111-Oct-03 23:23 
GeneralRe: Nice for a start, but still a lot todo Pinmemberpoupou2-Oct-03 1:54 
GeneralRe: Nice for a start, but still a lot todo PinmemberIlluvatan18-Jun-04 9:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.140827.1 | Last Updated 26 Sep 2003
Article Copyright 2003 by Simone Spagna
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid