RC4 Encrypt Decrypt Algoritm

RC4 is a simple yet strong encryption algorithm. It is a symmetric-key algorithm i.e. the same key is used for both decryption and encryption. RC4 is widely used, such as Secure Sockets Layer, BitTorrent protocol encryption, Remote Desktop Protocol, PDF, WEP. I implemented this algorithm in my PDF processor project.

 

Code
    /// <summary>
    
/// RC4 Encrypt/Decrypt Algoritm
    
/// </summary>
    
/// <remarks>
    
/// The RC4 encryption algorithm is stream cipher, which can use variable length keys. 
    
/// The algorithm was developed in 1987 by Ron Rivest, for RSA Data Security, 
    
/// and was a propriety algorithm until 1994.
    
/// </remarks>
    public class RC4
    {
        
/// <summary>
        
/// Number of states
        
/// </summary>
        const int snum = 256;

        
/// <summary>
        
/// Swap the value of two array cells.
        
/// </summary>
        
/// <param name="array"></param>
        
/// <param name="i"></param>
        
/// <param name="j"></param>
        static void Swap(byte[] array, int i, int j)
        {
            
byte tmp = array[i];
            array[i] 
= array[j];
            array[j] 
= tmp;
        }

        
static byte[] CreateStates(byte[] key)
        {
            
byte[] states = new byte[snum];
            
for (int i = 0; i < snum; i++)
            {
                states[i] 
= (byte)i;
            }

            
int j = 0;
            
for (int i = 0; i < snum; i++)
            {
                j 
= (j + states[i] + key[i % key.Length]) % snum;
                Swap(states, i, j);
            }
            
return states;
        }

        
/// <summary>
        
/// The same function is used to Encrypt and Decrypt.
        
/// At most 256 bytes in key are used.
        
/// </summary>
        
/// <param name="input">input stream</param>
        
/// <param name="output">output stream</param>
        
/// <param name="key">secret key</param>
        public static void EncryptDecrypt(Stream input, Stream output, byte[] key)
        {
            
byte[] states = CreateStates(key); // thread safe

            
int i = 0;
            
int j = 0;

            
int b = input.ReadByte();
            
while (b != -1)
            {
                i 
= (i + 1% snum;
                j 
= (j + states[i]) % snum;
                Swap(states, i, j);
                
int t = (states[i] + states[j]) % snum;
                
byte z = states[t];
                
byte mc = (byte)b;
                
byte cm = (byte)(mc ^ z);
                output.WriteByte(cm);

                b 
= input.ReadByte();
            }
        }

        
/// <summary>
        
/// The same function is used to Encrypt and Decrypt.
        
/// At most 256 bytes in key are used.
        
/// </summary>
        
/// <param name="data">input data</param>
        
/// <param name="key">secret key</param>
        
/// <returns>output data</returns>
        public static byte[] EncryptDecrypt(byte[] data, byte[] key)
        {
            MemoryStream input 
= new MemoryStream(data);
            MemoryStream output 
= new MemoryStream(data.Length);
            EncryptDecrypt(input, output, key);
            
return output.ToArray();
        }
    }

 

 

 

posted @ 2010-01-04 18:21  刘俊峰  阅读(1484)  评论(0编辑  收藏  举报