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 @   刘俊峰  阅读(1495)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示