5,442,164 members and growing! (15,208 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » Encryption     Intermediate

RC4 Encryption Algorithm: C# Version

By Simone Spagna

C# version of RC4 encryption algorithm.
C#, .NET, Win2K, WinXP, Win2003, Windows, Visual Studio, Dev

Posted: 25 Sep 2003
Updated: 25 Sep 2003
Views: 139,982
Bookmarked: 43 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
41 votes for this Article.
Popularity: 6.25 Rating: 3.88 out of 5
6 votes, 15.0%
1
0 votes, 0.0%
2
1 vote, 2.5%
3
10 votes, 25.0%
4
23 votes, 57.5%
5

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

About the Author

Simone Spagna


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.
Occupation: Web Developer
Location: Italy Italy

Other popular Algorithms & Recipes articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 37 (Total in Forum: 37) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralA comment about the algorithmmemberRoaa Mohammed0:37 29 Apr '08  
QuestionRC4 va PIC microcontroller 16F877Xmemberdevoice10:19 24 Sep '07  
QuestionUnicode characters ?memberuser_2017:18 5 Mar '07  
GeneralDoesn't workmemberblahblah_mskp12:45 22 Mar '06  
GeneralRe: Doesn't workmemberSimone Spagna1:39 21 Feb '07  
GeneralThanksmemberpizzy726:08 24 Dec '05  
GeneralA bug?memberAntoninoLuis0:23 24 Oct '05  
GeneralRe: A bug?memberSimone Spagna1:47 21 Feb '07  
GeneralMany ThanksmemberDick Walker15:01 4 Oct '05  
GeneralRC4? (encryption)sussRC4 craxy14:36 22 Jun '05  
GeneralRe: RC4? (encryption)memberBehind The Scene5:13 7 Aug '06  
GeneralI want code encript passwordmemberlethanhdaobk1:47 14 Jun '05  
GeneralRe: I want code encript passwordmemberBehind The Scene8:14 18 Jan '07  
Generalhave a proble to encrypt files like bmp,gif,docmembershekharamle21:49 28 Mar '05  
GeneralHow I can save and load the encrypted text ?sussAnonymous11:21 20 Feb '05  
GeneralRe: How I can save and load the encrypted text ?memberSeemu4:49 21 Jul '05  
GeneralRe: How I can save and load the encrypted text ?memberZedoc1:10 29 Nov '05  
QuestionRe: How I can save and load the encrypted text ?memberparkerbl13:05 1 May '07  
GeneralDesign error?memberdr.joe9:55 22 Jan '05  
GeneralRC4 algorithm & explanation neededsussprincekamalesh0:21 13 Dec '04  
GeneralQuestion about ciphermemberjavababe11:54 21 Oct '04  
Generalabout the programsusssurgeproof23:54 27 Feb '04  
GeneralRe: about the programsussAnonymous0:06 28 Feb '04  
GeneralRe: about the programmemberSimone Spagna0:08 28 Feb '04  
Generallooks like someone beat you to it :)membersmallguy786:23 29 Oct '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 Sep 2003
Editor: Smitha Vijayan
Copyright 2003 by Simone Spagna
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project