All pastes #729702 Raw Edit

Stuff

public text v1 · immutable
·published 2007-10-08 07:07 UTC
rendered paste body
arcfour.h
---------
// arcfour.h
// RC4 compatible stream cipher
// Copyright 2003 Sandvine Incorporated.  All rights reserved.

// Arcfour: RC4 compatible algorithm - Applied Cryptography pg 397

#ifndef __ARCFOUR_H
#define __ARCFOUR_H

class Arcfour
{
protected:
    int m_x;
    int m_y;
    unsigned char m_s[256];
public:
    virtual inline void SetKey(const unsigned char* key, int len);
    inline unsigned char Random();
};

void Arcfour::SetKey(const unsigned char* key, int len)
{
    int i;
    int j = 0;
    int k = 0;
    for (i=0; i<256; ++i)
    {
        m_s[i] = i;
    }
    for (i=0; i<256; ++i)
    {
        j = (j + m_s[i] + key[k]) & 0xFF;
        if (++k == len)
        {
            k = 0;
        }
        unsigned int t = m_s[i];
        m_s[i] = m_s[j];
        m_s[j] = t;
    }
    m_x = 0;
    m_y = 0;
}

unsigned char Arcfour::Random()
{
    m_x = (m_x + 1) & 0xFF;
    m_y = (m_y + m_s[m_x]) & 0xFF;
    unsigned int t = m_s[m_x];
    m_s[m_x] = m_s[m_y];
    m_s[m_y] = t;

    t = (m_s[m_x] + m_s[m_y]) & 0xFF;
    return m_s[t];
}

#endif