Back to index

scribus-ng  1.3.4.dfsg+svn20071115
Functions
rc4.c File Reference
#include "rc4.h"

Go to the source code of this file.

Functions

void rc4_init (rc4_context_t *text, const unsigned char *key, unsigned keylen)
void rc4_encrypt (rc4_context_t *text, const unsigned char *input, unsigned char *output, unsigned len)

Function Documentation

void rc4_encrypt ( rc4_context_t text,
const unsigned char *  input,
unsigned char *  output,
unsigned  len 
)

Definition at line 84 of file rc4.c.

{
  unsigned char             tmp;                 /* Swap variable */
  int                i, j;                /* Looping vars */
  int                t;                   /* Current S box */


 /*
  * Loop through the entire buffer...
  */

  i = text->i;
  j = text->j;

  while (len > 0)
  {
   /*
    * Get the next S box indices...
    */

    i = (i + 1) & 255;
    j = (j + text->sbox[i]) & 255;

   /*
    * Swap Si and Sj...
    */

    tmp           = text->sbox[i];
    text->sbox[i] = text->sbox[j];
    text->sbox[j] = tmp;

   /*
    * Get the S box index for this byte...
    */

    t = (text->sbox[i] + text->sbox[j]) & 255;

   /*
    * Encrypt using the S box...
    */

    *output++ = *input++ ^ text->sbox[t];
    len --;
  }

 /*
  * Copy current S box indices back to context...
  */

  text->i = i;
  text->j = j;
}
void rc4_init ( rc4_context_t text,
const unsigned char *  key,
unsigned  keylen 
)

Definition at line 38 of file rc4.c.

{
  int         i, j;                /* Looping vars */
  unsigned char      tmp;                 /* Temporary variable */


 /*
  * Fill in linearly s0=0, s1=1, ...
  */

  for (i = 0; i < 256; i ++)
    text->sbox[i] = i;

  for (i = 0, j = 0; i < 256; i ++)
  {
   /*
    * j = (j + Si + Ki) mod 256
    */

    j = (j + text->sbox[i] + key[i % keylen]) & 255;

   /*
    * Swap Si and Sj...
    */

    tmp           = text->sbox[i];
    text->sbox[i] = text->sbox[j];
    text->sbox[j] = tmp;
  }

 /*
  * Initialized counters to 0 and return...
  */

  text->i = 0;
  text->j = 0;
}