File: arc4.5c

package info (click to toggle)
nickle 2.107
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 3,756 kB
  • sloc: ansic: 27,954; yacc: 1,874; lex: 954; sh: 204; makefile: 13; lisp: 1
file content (67 lines) | stat: -rw-r--r-- 1,315 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * Copyright © 1999 Bart Massey.
 * All Rights Reserved.  See the file COPYING in this directory
 * for licensing information.
 */

/*
 * Implementation of Alleged RC4
 *
 * Bart 1999/2
 * 
 * Used primarily for PRNG, so interface is not too fancy
 */

namespace ARC4 {

  global int[256] state = {};
  global int p1, p2;

  public void nsetkey(int nn, int k) 
    /*
     * First argument is number of times to iterate the
     * initial scramble.  10 seems good for most apps.
     * Second argument is integer key: largest useful key is
     * 8 * 62 bits.
     */
  {
    int i, j, n, ii;
    int[64] key = {};

    key[0] = 13;
    key[1] = 17;
    for (n = 2; n < 64 && k > 0; n++) {
      key[n] = k & 0xff;
      k = k >> 8;
    }

    for (i = 0; i < 256; i++)
      state[i] = i;

    for (ii = 0; ii < nn; ii++) {
      j = 0;
      for (i = 0; i < 256; i++) {
        j = (j + state[i] + key[i % n]) & 0xff;
	int tmp = state[i];
	state[i] = state[j];
	state[j] = tmp;
      }
    }
    p1 = p2 = 0;
  }

  public int streambyte() 
    /*
     * Get the next byte from the stream 
     */
  {
    p1 = (p1 + 1) & 0xff;
    p2 = (p2 + state[p1]) & 0xff;
    int tmp = state[p1];
    state[p1] = state[p2];
    state[p2] = tmp;
    int n = (state[p1] + state[p2]) & 0xff;
    return state[n];
  }
  
}