zoukankan      html  css  js  c++  java
  • 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();
            }
        }
  • 相关阅读:
    jQuery jsonp(转载)
    vue jsonp (转载)
    Win10系统 开机总是黑屏一分钟 然后才进入解锁界面?
    Win10怎么取消开机密码
    Qt启动报错“0xc0000005”错误?
    公司转让债务问题怎么处理
    从车载激光点云数据轨迹数据中提取坐高斯标数据
    macbook air和pro的ppi
    任务管理器是灰色的
    python列表删除某个元素
  • 原文地址:https://www.cnblogs.com/rufi/p/1639122.html
Copyright © 2011-2022 走看看