/* 
	 rc4_single_brute.c -- "optimized" single processor 40 bit rc4 brute force.  

	 COMPILE on x86 with: 
	 	gcc -DLITTLE_ENDIAN -O3 -m486 rc4_single_brute.c -o rc4_single_brute 
	 
	 USAGE - specify a plaintext file and a ciphertext file and this walks
	 through all the 40 bit keys, looking for the match.  the following perl
	 script can be used to generate ciphertexts:  
	 
	 on a Pentium 3 500mhz the entire 24 bit keyspace takes 74 seconds.
	 on a Pentium 3 500mhz the entire 32 bit keyspace takes 315 minutes.
	 on a Pentium 3 500mhz the entire 40 bit keyspace takes 56 days.

----------- rc4.pl 
[ author: adam back (?) ]

#!/usr/local/bin/perl 
@k=unpack('C*',pack('H*',shift));
sub S{@s[$x,$y]=@s[$y,$x];}
for(@t=@s=0..255)
{
	$y=($k[$i++]+$s[$_]+$y)%256;
	$x=$_;&S;$i%=@k;
}
$/=$x=$y=0;
for(unpack('C*',<>))
{
	$x++;
	$y=($s[$x%=256]+$y)%256;
	&S;
	print pack(C,$_^=$s[($s[$x]+$s[$y])%256]);
}

----------- end rc4.pl

	to generate a plaintext / ciphertext pair do something like this:

		echo "hello" > pt
		cat pt | ./rc4.pl 0000ffffff > ct
		
	that will encrypt "hello" with the hex key 0x0000ffffff (largest 24 bit key)	
	and store the contents in a file called "ct".
	
	then do
		./rc4_single_brute pt ct
		
	and wait.  always use 40 bit keylengths (pad with 0, as above example);
	that is what this program brute forces.
	
	on a Pentium 3 500mhz, finding the 24 bit key 0x0000ffffff takes 74
	seconds; thus finding the largest 32 bit key takes 256 * 74 seconds, and the
	entire 40 bit keyspace takes approx 256 * 256 * 74 seconds (56 days).

----------- DESCRIPTION OF ALG 
[ from www.cs.uwf.edu/~wilde/CEN6990/papers/boone/RC4.htm ]

	RC4 (which ostensibly stands for Rivest's Cipher 4) is a symmetric
	algorithm, but instead of operating on a block of bits at a time, it
	operates on a bitstream. It operates with a variable-length key up to 256
	bits.

	This cipher has a 256-entry substitution-box, the entries are permutations
	of the numbers 0 through 255, and the particular permutation is a function
	of the key. Initializing the substitution-box is easy. First, fill it
	linearly so that S0=0, S1=1, ..., S255=255. Then fill another 256-byte array
	with the key, repeating the key as often as necessary to fill the whole
	array (K0, K1, ..., K255). Set the index j = 0, then:

	For i = 0 to 255

	j = (j + Si + Ki) mod 256

	swap Si and Sj

	To generate a random byte for encryption, first take two counters (i and j)
	initialized to zero, then:

	i = ( i + 1) mod 256

	j = (j + Si) mod 256

	swap Si and Sj

	t = (Si + Sj) mod 256

	K = St

	The byte K is then XORed with the plaintext to produce ciphertext, or XORed
	with the ciphertext to produce the plaintext. Encryption is about 10 times
	faster than DES in software.


-----------

           Cyberspace: Pandora's Mailbox, RC4 a secret no longer

                                 Mark Chen



          On September 9 there appeared on the Cypherpunks'
          Internet mailing list a short piece of computer source
          code purporting to be RSA Data Security's secret RC4
          cipher algorithm. RC4 is one of the most widely used
          commercial ciphers, but its internals have for years
          been a guarded trade secret--a status that changed
          within hours, as the program fragment, which simple
          tests revealed to be the genuine article, traversed the
          farthest corners of the Net via e-mail, bulletin boards,
          and file transfer. State Department export regulations,
          as well as intellectual property laws, were smoothly
          rendered moot. As Cypherpunk founder Tim May says,
          "National borders are just speed bumps on the
          information superhighway."

          Soon after RC4 hit the Net, RSA Data Security (RSADSI)
          issued a statement that read in part: "It has come to
          RSA Data Security's attention that certain RSA trade
          secrets, in the form of confidential and proprietary
          source code, have been misappropriated and disclosed..."

          ... Not only is this act a violation of law, but its
          publication is a gross abuse of the Internet. RSA has
          begun an investigation and will proceed with legal
          action against anyone found to have violated its
          intellectual property rights."

          Setting aside the questionable legal basis of these
          threats, why doesn't RSADSI bring charges against the
          person who originally released the code? The answer is
          they don't know who did it. The code was distributed
          through a mechanism called a "mailing list," a system
          that distributes e-mail to people who have signed up to
          receive messages on a given topic. For example, if you
          were interested in fish, you might sign up for an
          aquarium hobbyists' list. People could then send e-mail
          to the list server, and the e-mail would be forwarded to
          everyone on the list. RC4 was posted to the Cypherpunks'
          "anonymous remailers," list. When an anonymous remailer
          receives a message, it strips off all of the sender
          information and remails the message under an anonymous
          pseudonym. This effectively breaks the link between
          sender and recipients, and makes tracing impossible.

          The intended purpose of these remailers is to allow free
          distribution of various intellectual "commodities,"
          whose distribution is ordinarily discouraged by law or
          custom. A Cypherpunk faction called the Information
          Liberation Front has long used anonymous remailing to
          distribute inaccessible, expensive, copyrighted
          literature. Moreover, the remailers serve as
          technological guarantee of the right to free speech.
          They allow unpopular opinions to be voiced, while
          protecting the authors of those opinions from
          retribution.

          Far from being a "gross abuse of the Internet," as
          RSADSI maintains, the distribution of RC4 was a case of
          the Internet doing what it does best--propagating ideas.
          While this act may or may not have been a violation of
          human-made laws, it was a faithful exercise of the
          natural laws of information flow, as expressed by Thomas
          Jefferson, parent of the American patent system, in an
          August 13, 1813 letter to Isaac McPherson: "If nature
          has made any one thing less susceptible than all others
          of exclusive property, it is the action of the thinking
          power called an idea, which an individual may
          exclusively possess as long as he keeps it to himself;
          but the moment it is divulged, it forces itself into the
          possession of everyone, and the receiver cannot
          dispossess himself of it. Its peculiar character, too,
          is that no one possesses the less, because every other
          possesses the whole of it. He who receives an idea from
          me, receives instruction himself without lessening mine;
          as he who lights his taper at mine, receives light
          without darkening me. That ideas should freely spread
          from one to another over the globe, for the moral and
          mutual instruction of man, and improvement of his
          condition, seems to have been peculiarly and
          benevolently designed by nature, when she made them,
          like fire, expandable over all space, without lessening
          their density at any point, and like the air in which we
          breathe, move, and have our physical being, incapable of
          confinement or exclusive appropriation. Inventions then
          cannot, in nature, be a subject of property."

          So to the dismay of RSADSI and many others, 21st century
          technology is catching up to Thomas Jefferson's 18th
          century natural philosophy, and turning "intellectual
          property" into an oxymoron. The question of "What can be
          done?" about truly unrestricted free speech--that is,
          the freedom to distribute any intellectual products at
          all, including those that supposedly "belong" to someone
          else--is a matter of controversy.

          The fear among the commissars is that the force of
          Jefferson's laws of infodynamics may well prove to be
          absolute. This fear is so profound that the
          Administration's techno-evangelists are still in a state
          of denial--a fact well-illustrated by the Information
          Infrastructure Task Force's extraordinary 150-page draft
          report on "Intellectual Property and the National
          Information Infrastructure." Beginning with the
          Panglossian assertion that "with no more than minor
          clarification and amendment, the Copyright Act, like the
          Patent Act, will provide the necessary protection of
          rights" on the Infobahn, the report goes on to make
          various pronouncements on how intellectual "property" is
          to be defended technologically--none of which can
          withstand cursory inspection by any engineer with more
          intelligence than a bivalve. Sensing, perhaps, the
          dearth of substance in their work, the authors quickly
          move on to discuss more practical matters. First, some
          moral training: "There seems to be an attitude by some
          on the Internet...that you check your copyright at the
          door when you enter that domain. There is a general lack
          of awareness...about intellectual property."

          And such a deplorable circumstance obviously calls for a
          massive program of indoctrination: "Effective education
          of the public about intellectual property rights is
          crucial....Therefore, the principles of intellectual
          property law must be taught in our schools and
          libraries."

          And it goes downhill from there.

          While this official pabulum may frighten laypersons
          (lacking, as we do, the moral fiber afforded by a proper
          education in intellectual property law), it must be
          conceded that desperate times call for desperate
          measures. What is a government to do? Suppose, for
          example, that to put an end to the kind of hanky-panky
          that allowed the release of RC4, Congress decided to
          outlaw anonymous remailers. Well, then, what about the
          ones overseas? Any computer attached to the Internet is
          just as accessible as any other; neither national
          borders nor geographical distances make a whit of
          difference. It might further propose that the NSA
          monitor all overseas traffic and have the FBI visit
          anyone who transmits items from a stipulated verboten
          list. This, too, fails because unbreakable cryptography
          is now widely available, and would render such
          transmissions opaque even to the NSA. Okay, then, let's
          suppose that we prohibit transmission of encrypted data
          altogether. Unfortunately, encrypted data can be made
          virtually impossible to detect. Through a technique
          called "steganography," encrypted messages can be
          imbedded in other, normal-looking data so that they
          appear to be nothing more than, say, graphics or audio
          files. And as a further complication, a technology
          called "DC-nets" (invented by David Chaum) now enables
          people to create anonymous messages that are
          mathematically impossible to trace--with or without
          remailers. Practical implementations of this technology
          have not yet been devised, but they are not far off.

          Some analysts are now advocating a scheme called
          "superdistribution," based on the premise that while it
          is difficult to control software's duplication, it is
          easy to control its behavior. It implicitly recognizes
          that past attempts at control have been dismal failures,
          due largely to the fact that they tried to treat ideas
          like physical goods. So in stark contrast to such silly
          measures as copy protection, superdistribution
          encourages aggressive, free dissemination of software.
          Control happens not at the point of duplication, but at
          the point of use. The user may freely obtain a program
          from a bulletin board, a public file server, or e-mail;
          but when they run it, the program requests payment in
          order to function.

          Various mechanisms have been proposed for enabling this
          "revenue collection," ranging from specialized hardware
          that accumulates usage fees for monthly billing (like a
          gas meter) to on-the-spot digital cash payment via
          e-mail. Whether or not computer users are ultimately
          willing to submit to such a regimen of capitalist
          genuflection is an open question; but the fact that
          these solutions are being proposed underscores the
          magnitude of the problem.

          It is impossible to judge what shape an eventual
          solution will take. Schemes like superdistribution apply
          only to binary programs, not source code, and as yet, no
          one has made even a plausible conjecture about how to
          regulate static data like text or artwork. What is clear
          is that some of the fundamental assumptions of
          capitalism, like the relationship of supply to demand,
          cannot survive in cyberspace without drastic
          modification. In the words of John Perry Barlow:
          "Notions of property, value, ownership, and the nature
          of wealth itself are changing more fundamentally than at
          any time since the Sumerians first poked cuneiform into
          wet clay and called it stored grain. Only a very few
          people are aware of the enormity of this shift, and
          fewer of them are lawyers or public officials."

          In today's virtual world, property cannot be bolted down
          and speech cannot be curtailed. The Espionage Act cannot
          enforce obedience; employers cannot "discipline"
          employees for complaining about management ineptitude or
          paternalism. There is no identifiable author to throw in
          jail or to fire. There is no press to shut down.

          These facts have direct implications for our existing
          system of ideological hegemony. On September 1, for
          instance, NBC aired a "Dateline" segment (demurely
          entitled "Dial in for Mayhem") about peripatetic youths
          who had lost fingers while trying to follow pyrotechnic
          recipes from terrorist manuals found on computer
          bulletin boards. Viewers were treated to the spectacle
          of Deborah Roberts expressing shock and dismay at the
          easy accessibility of such titles as Anarchy for Fun and
          Profit and Uncle Fester's Home Workshop Explosives. The
          segment closed with Congressman Ed Markey fuming, "This
          is an open network that sends sewage...into the homes
          and minds of children without any safeguards
          whatsoever....We are now going to have to construct a
          new set of laws to give parents the power they need to
          protect their children," thus deftly adapting the NSA's
          "pederast in the computer" argument (featured recently
          in the Clipper Chip debate) to our newest national
          security threat.

          The message? Knowledge can kill, folks, so be sure to
          get yours from a licensed retailer



-----------
	code by hamblin@cs.wisc.edu
	optimization ideas by rob (boy wonder) & pete & me

*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>

inline void init_s(unsigned char *box);
inline void init_k(unsigned char *box, unsigned char *key);
inline void make_s(unsigned char *k, unsigned char *s);
inline void init_k_brute(unsigned char *box, int index, unsigned char block);
inline void get_byte(unsigned char *out, unsigned char *s, unsigned char
		*i, unsigned char *j);

inline void init_s(unsigned char *box)
{
	long i;
	
	/* set box[i] = i */

#ifdef LITTLE_ENDIAN

	*(int *)&box[0] = 0x03020100;
	*(int *)&box[4] = 0x07060504;
	*(int *)&box[8] = 0x0b0a0908;
	*(int *)&box[12] = 0x0f0e0d0c;
	*(int *)&box[16] = 0x13121110;
	*(int *)&box[20] = 0x17161514;
	*(int *)&box[24] = 0x1b1a1918;
	*(int *)&box[28] = 0x1f1e1d1c;
	*(int *)&box[32] = 0x23222120;
	*(int *)&box[36] = 0x27262524;
	*(int *)&box[40] = 0x2b2a2928;
	*(int *)&box[44] = 0x2f2e2d2c;
	*(int *)&box[48] = 0x33323130;
	*(int *)&box[52] = 0x37363534;
	*(int *)&box[56] = 0x3b3a3938;
	*(int *)&box[60] = 0x3f3e3d3c;
	*(int *)&box[64] = 0x43424140;
	*(int *)&box[68] = 0x47464544;
	*(int *)&box[72] = 0x4b4a4948;
	*(int *)&box[76] = 0x4f4e4d4c;
	*(int *)&box[80] = 0x53525150;
	*(int *)&box[84] = 0x57565554;
	*(int *)&box[88] = 0x5b5a5958;
	*(int *)&box[92] = 0x5f5e5d5c;
	*(int *)&box[96] = 0x63626160;
	*(int *)&box[100] = 0x67666564;
	*(int *)&box[104] = 0x6b6a6968;
	*(int *)&box[108] = 0x6f6e6d6c;
	*(int *)&box[112] = 0x73727170;
	*(int *)&box[116] = 0x77767574;
	*(int *)&box[120] = 0x7b7a7978;
	*(int *)&box[124] = 0x7f7e7d7c;
	*(int *)&box[128] = 0x83828180;
	*(int *)&box[132] = 0x87868584;
	*(int *)&box[136] = 0x8b8a8988;
	*(int *)&box[140] = 0x8f8e8d8c;
	*(int *)&box[144] = 0x93929190;
	*(int *)&box[148] = 0x97969594;
	*(int *)&box[152] = 0x9b9a9998;
	*(int *)&box[156] = 0x9f9e9d9c;
	*(int *)&box[160] = 0xa3a2a1a0;
	*(int *)&box[164] = 0xa7a6a5a4;
	*(int *)&box[168] = 0xabaaa9a8;
	*(int *)&box[172] = 0xafaeadac;
	*(int *)&box[176] = 0xb3b2b1b0;
	*(int *)&box[180] = 0xb7b6b5b4;
	*(int *)&box[184] = 0xbbbab9b8;
	*(int *)&box[188] = 0xbfbebdbc;
	*(int *)&box[192] = 0xc3c2c1c0;
	*(int *)&box[196] = 0xc7c6c5c4;
	*(int *)&box[200] = 0xcbcac9c8;
	*(int *)&box[204] = 0xcfcecdcc;
	*(int *)&box[208] = 0xd3d2d1d0;
	*(int *)&box[212] = 0xd7d6d5d4;
	*(int *)&box[216] = 0xdbdad9d8;
	*(int *)&box[220] = 0xdfdedddc;
	*(int *)&box[224] = 0xe3e2e1e0;
	*(int *)&box[228] = 0xe7e6e5e4;
	*(int *)&box[232] = 0xebeae9e8;
	*(int *)&box[236] = 0xefeeedec;
	*(int *)&box[240] = 0xf3f2f1f0;
	*(int *)&box[244] = 0xf7f6f5f4;
	*(int *)&box[248] = 0xfbfaf9f8;
	*(int *)&box[252] = 0xfffefdfc;

#else
	
	*(int *)&box[0] = 0x00010203;
	*(int *)&box[4] = 0x04050607;
	*(int *)&box[8] = 0x08090a0b;
	*(int *)&box[12] = 0x0c0d0e0f;
	*(int *)&box[16] = 0x10111213;
	*(int *)&box[20] = 0x14151617;
	*(int *)&box[24] = 0x18191a1b;
	*(int *)&box[28] = 0x1c1d1e1f;
	*(int *)&box[32] = 0x20212223;
	*(int *)&box[36] = 0x24252627;
	*(int *)&box[40] = 0x28292a2b;
	*(int *)&box[44] = 0x2c2d2e2f;
	*(int *)&box[48] = 0x30313233;
	*(int *)&box[52] = 0x34353637;
	*(int *)&box[56] = 0x38393a3b;
	*(int *)&box[60] = 0x3c3d3e3f;
	*(int *)&box[64] = 0x40414243;
	*(int *)&box[68] = 0x44454647;
	*(int *)&box[72] = 0x48494a4b;
	*(int *)&box[76] = 0x4c4d4e4f;
	*(int *)&box[80] = 0x50515253;
	*(int *)&box[84] = 0x54555657;
	*(int *)&box[88] = 0x58595a5b;
	*(int *)&box[92] = 0x5c5d5e5f;
	*(int *)&box[96] = 0x60616263;
	*(int *)&box[100] = 0x64656667;
	*(int *)&box[104] = 0x68696a6b;
	*(int *)&box[108] = 0x6c6d6e6f;
	*(int *)&box[112] = 0x70717273;
	*(int *)&box[116] = 0x74757677;
	*(int *)&box[120] = 0x78797a7b;
	*(int *)&box[124] = 0x7c7d7e7f;
	*(int *)&box[128] = 0x80818283;
	*(int *)&box[132] = 0x84858687;
	*(int *)&box[136] = 0x88898a8b;
	*(int *)&box[140] = 0x8c8d8e8f;
	*(int *)&box[144] = 0x90919293;
	*(int *)&box[148] = 0x94959697;
	*(int *)&box[152] = 0x98999a9b;
	*(int *)&box[156] = 0x9c9d9e9f;
	*(int *)&box[160] = 0xa0a1a2a3;
	*(int *)&box[164] = 0xa4a5a6a7;
	*(int *)&box[168] = 0xa8a9aaab;
	*(int *)&box[172] = 0xacadaeaf;
	*(int *)&box[176] = 0xb0b1b2b3;
	*(int *)&box[180] = 0xb4b5b6b7;
	*(int *)&box[184] = 0xb8b9babb;
	*(int *)&box[188] = 0xbcbdbebf;
	*(int *)&box[192] = 0xc0c1c2c3;
	*(int *)&box[196] = 0xc4c5c6c7;
	*(int *)&box[200] = 0xc8c9cacb;
	*(int *)&box[204] = 0xcccdcecf;
	*(int *)&box[208] = 0xd0d1d2d3;
	*(int *)&box[212] = 0xd4d5d6d7;
	*(int *)&box[216] = 0xd8d9dadb;
	*(int *)&box[220] = 0xdcdddedf;
	*(int *)&box[224] = 0xe0e1e2e3;
	*(int *)&box[228] = 0xe4e5e6e7;
	*(int *)&box[232] = 0xe8e9eaeb;
	*(int *)&box[236] = 0xecedeeef;
	*(int *)&box[240] = 0xf0f1f2f3;
	*(int *)&box[244] = 0xf4f5f6f7;
	*(int *)&box[248] = 0xf8f9fafb;
	*(int *)&box[252] = 0xfcfdfeff;
	
#endif

}

inline void init_k_brute(unsigned char *box, int index, unsigned char block)
{
	long i;
	
	/* copy the key into box repeatedly */

	box[index] = block;
	box[index + 5] = block;
	box[index + 10] = block;
	box[index + 15] = block;
	box[index + 20] = block;
	box[index + 25] = block;
	box[index + 30] = block;
	box[index + 35] = block;
	box[index + 40] = block;
	box[index + 45] = block;
	box[index + 50] = block;
	box[index + 55] = block;
	box[index + 60] = block;
	box[index + 65] = block;
	box[index + 70] = block;
	box[index + 75] = block;
	box[index + 80] = block;
	box[index + 85] = block;
	box[index + 90] = block;
	box[index + 95] = block;
	box[index + 100] = block;
	box[index + 105] = block;
	box[index + 110] = block;
	box[index + 115] = block;
	box[index + 120] = block;
	box[index + 125] = block;
	box[index + 130] = block;
	box[index + 135] = block;
	box[index + 140] = block;
	box[index + 145] = block;
	box[index + 150] = block;
	box[index + 155] = block;
	box[index + 160] = block;
	box[index + 165] = block;
	box[index + 170] = block;
	box[index + 175] = block;
	box[index + 180] = block;
	box[index + 185] = block;
	box[index + 190] = block;
	box[index + 195] = block;
	box[index + 200] = block;
	box[index + 205] = block;
	box[index + 210] = block;
	box[index + 215] = block;
	box[index + 220] = block;
	box[index + 225] = block;
	box[index + 230] = block;
	box[index + 235] = block;
	box[index + 240] = block;
	box[index + 245] = block;
	box[index + 250] = block;
	if (index == 0)
		box[255] = block;
	
}

inline void init_k(unsigned char *box, unsigned char *key)
{
	int i;

	char k0[4], k1[4], k2[4], k3[4], k4[4];
	
	k0[0] = key[0];
	k0[1] = key[1];
	k0[2] = key[2];
	k0[3] = key[3];

	k1[0] = key[1];
	k1[1] = key[2];
	k1[2] = key[3];
	k1[3] = key[4];

	k2[0] = key[2];
	k2[1] = key[3];
	k2[2] = key[4];
	k2[3] = key[0];

	k3[0] = key[3];
	k3[1] = key[4];
	k3[2] = key[0];
	k3[3] = key[1];

	k4[0] = key[4];
	k4[1] = key[0];
	k4[2] = key[1];
	k4[3] = key[2];
	
	/* copy the key into box repeatedly */

	*(int*)&box[0] = *(int*)&k0[0];
	*(int*)&box[20] = *(int*)&k0[0];
	*(int*)&box[40] = *(int*)&k0[0];
	*(int*)&box[60] = *(int*)&k0[0];
	*(int*)&box[80] = *(int*)&k0[0];
	*(int*)&box[100] = *(int*)&k0[0];
	*(int*)&box[120] = *(int*)&k0[0];
	*(int*)&box[140] = *(int*)&k0[0];
	*(int*)&box[160] = *(int*)&k0[0];
	*(int*)&box[180] = *(int*)&k0[0];
	*(int*)&box[200] = *(int*)&k0[0];
	*(int*)&box[220] = *(int*)&k0[0];
	*(int*)&box[240] = *(int*)&k0[0];

	*(int*)&box[4] = *(int*)&k4[0];
	*(int*)&box[24] = *(int*)&k4[0];
	*(int*)&box[44] = *(int*)&k4[0];
	*(int*)&box[64] = *(int*)&k4[0];
	*(int*)&box[84] = *(int*)&k4[0];
	*(int*)&box[104] = *(int*)&k4[0];
	*(int*)&box[124] = *(int*)&k4[0];
	*(int*)&box[144] = *(int*)&k4[0];
	*(int*)&box[164] = *(int*)&k4[0];
	*(int*)&box[184] = *(int*)&k4[0];
	*(int*)&box[204] = *(int*)&k4[0];
	*(int*)&box[224] = *(int*)&k4[0];
	*(int*)&box[244] = *(int*)&k4[0];

	*(int*)&box[8] = *(int*)&k3[0];
	*(int*)&box[28] = *(int*)&k3[0];
	*(int*)&box[48] = *(int*)&k3[0];
	*(int*)&box[68] = *(int*)&k3[0];
	*(int*)&box[88] = *(int*)&k3[0];
	*(int*)&box[108] = *(int*)&k3[0];
	*(int*)&box[128] = *(int*)&k3[0];
	*(int*)&box[148] = *(int*)&k3[0];
	*(int*)&box[168] = *(int*)&k3[0];
	*(int*)&box[188] = *(int*)&k3[0];
	*(int*)&box[208] = *(int*)&k3[0];
	*(int*)&box[228] = *(int*)&k3[0];
	*(int*)&box[248] = *(int*)&k3[0];

	*(int*)&box[12] = *(int*)&k2[0];
	*(int*)&box[32] = *(int*)&k2[0];
	*(int*)&box[52] = *(int*)&k2[0];
	*(int*)&box[72] = *(int*)&k2[0];
	*(int*)&box[92] = *(int*)&k2[0];
	*(int*)&box[112] = *(int*)&k2[0];
	*(int*)&box[132] = *(int*)&k2[0];
	*(int*)&box[152] = *(int*)&k2[0];
	*(int*)&box[172] = *(int*)&k2[0];
	*(int*)&box[192] = *(int*)&k2[0];
	*(int*)&box[212] = *(int*)&k2[0];
	*(int*)&box[232] = *(int*)&k2[0];
	*(int*)&box[252] = *(int*)&k2[0];

	*(int*)&box[16] = *(int*)&k1[0];
	*(int*)&box[36] = *(int*)&k1[0];
	*(int*)&box[56] = *(int*)&k1[0];
	*(int*)&box[76] = *(int*)&k1[0];
	*(int*)&box[96] = *(int*)&k1[0];
	*(int*)&box[116] = *(int*)&k1[0];
	*(int*)&box[136] = *(int*)&k1[0];
	*(int*)&box[156] = *(int*)&k1[0];
	*(int*)&box[176] = *(int*)&k1[0];
	*(int*)&box[196] = *(int*)&k1[0];
	*(int*)&box[216] = *(int*)&k1[0];
	*(int*)&box[236] = *(int*)&k1[0];

}

inline void make_s(unsigned char *k, unsigned char *s)
{
	register unsigned char i; 
	register unsigned char j;

		j = k[0];
		s[0] = s[j];
		s[j] = 0;

		i = s[1];
		j += (i + k[1]);	
		s[1] = s[j];
		s[j] = i;
	
		i = s[2];
		j += (i + k[2]);	
		s[2] = s[j];
		s[j] = i;
	
		i = s[3];
		j += (i + k[3]);	
		s[3] = s[j];
		s[j] = i;
	
		i = s[4];
		j += (i + k[4]);	
		s[4] = s[j];
		s[j] = i;
	
		i = s[5];
		j += (i + k[5]);	
		s[5] = s[j];
		s[j] = i;
	
		i = s[6];
		j += (i + k[6]);	
		s[6] = s[j];
		s[j] = i;
	
		i = s[7];
		j += (i + k[7]);	
		s[7] = s[j];
		s[j] = i;
	
		i = s[8];
		j += (i + k[8]);	
		s[8] = s[j];
		s[j] = i;
	
		i = s[9];
		j += (i + k[9]);	
		s[9] = s[j];
		s[j] = i;
	
		i = s[10];
		j += (i + k[10]);	
		s[10] = s[j];
		s[j] = i;
	
		i = s[11];
		j += (i + k[11]);	
		s[11] = s[j];
		s[j] = i;
	
		i = s[12];
		j += (i + k[12]);	
		s[12] = s[j];
		s[j] = i;
	
		i = s[13];
		j += (i + k[13]);	
		s[13] = s[j];
		s[j] = i;
	
		i = s[14];
		j += (i + k[14]);	
		s[14] = s[j];
		s[j] = i;
	
		i = s[15];
		j += (i + k[15]);	
		s[15] = s[j];
		s[j] = i;
	
		i = s[16];
		j += (i + k[16]);	
		s[16] = s[j];
		s[j] = i;
	
		i = s[17];
		j += (i + k[17]);	
		s[17] = s[j];
		s[j] = i;
	
		i = s[18];
		j += (i + k[18]);	
		s[18] = s[j];
		s[j] = i;
	
		i = s[19];
		j += (i + k[19]);	
		s[19] = s[j];
		s[j] = i;
	
		i = s[20];
		j += (i + k[20]);	
		s[20] = s[j];
		s[j] = i;
	
		i = s[21];
		j += (i + k[21]);	
		s[21] = s[j];
		s[j] = i;
	
		i = s[22];
		j += (i + k[22]);	
		s[22] = s[j];
		s[j] = i;
	
		i = s[23];
		j += (i + k[23]);	
		s[23] = s[j];
		s[j] = i;
	
		i = s[24];
		j += (i + k[24]);	
		s[24] = s[j];
		s[j] = i;
	
		i = s[25];
		j += (i + k[25]);	
		s[25] = s[j];
		s[j] = i;
	
		i = s[26];
		j += (i + k[26]);	
		s[26] = s[j];
		s[j] = i;
	
		i = s[27];
		j += (i + k[27]);	
		s[27] = s[j];
		s[j] = i;
	
		i = s[28];
		j += (i + k[28]);	
		s[28] = s[j];
		s[j] = i;
	
		i = s[29];
		j += (i + k[29]);	
		s[29] = s[j];
		s[j] = i;
	
		i = s[30];
		j += (i + k[30]);	
		s[30] = s[j];
		s[j] = i;
	
		i = s[31];
		j += (i + k[31]);	
		s[31] = s[j];
		s[j] = i;
	
		i = s[32];
		j += (i + k[32]);	
		s[32] = s[j];
		s[j] = i;
	
		i = s[33];
		j += (i + k[33]);	
		s[33] = s[j];
		s[j] = i;
	
		i = s[34];
		j += (i + k[34]);	
		s[34] = s[j];
		s[j] = i;
	
		i = s[35];
		j += (i + k[35]);	
		s[35] = s[j];
		s[j] = i;
	
		i = s[36];
		j += (i + k[36]);	
		s[36] = s[j];
		s[j] = i;
	
		i = s[37];
		j += (i + k[37]);	
		s[37] = s[j];
		s[j] = i;
	
		i = s[38];
		j += (i + k[38]);	
		s[38] = s[j];
		s[j] = i;
	
		i = s[39];
		j += (i + k[39]);	
		s[39] = s[j];
		s[j] = i;
	
		i = s[40];
		j += (i + k[40]);	
		s[40] = s[j];
		s[j] = i;
	
		i = s[41];
		j += (i + k[41]);	
		s[41] = s[j];
		s[j] = i;
	
		i = s[42];
		j += (i + k[42]);	
		s[42] = s[j];
		s[j] = i;
	
		i = s[43];
		j += (i + k[43]);	
		s[43] = s[j];
		s[j] = i;
	
		i = s[44];
		j += (i + k[44]);	
		s[44] = s[j];
		s[j] = i;
	
		i = s[45];
		j += (i + k[45]);	
		s[45] = s[j];
		s[j] = i;
	
		i = s[46];
		j += (i + k[46]);	
		s[46] = s[j];
		s[j] = i;
	
		i = s[47];
		j += (i + k[47]);	
		s[47] = s[j];
		s[j] = i;
	
		i = s[48];
		j += (i + k[48]);	
		s[48] = s[j];
		s[j] = i;
	
		i = s[49];
		j += (i + k[49]);	
		s[49] = s[j];
		s[j] = i;
	
		i = s[50];
		j += (i + k[50]);	
		s[50] = s[j];
		s[j] = i;
	
		i = s[51];
		j += (i + k[51]);	
		s[51] = s[j];
		s[j] = i;
	
		i = s[52];
		j += (i + k[52]);	
		s[52] = s[j];
		s[j] = i;
	
		i = s[53];
		j += (i + k[53]);	
		s[53] = s[j];
		s[j] = i;
	
		i = s[54];
		j += (i + k[54]);	
		s[54] = s[j];
		s[j] = i;
	
		i = s[55];
		j += (i + k[55]);	
		s[55] = s[j];
		s[j] = i;
	
		i = s[56];
		j += (i + k[56]);	
		s[56] = s[j];
		s[j] = i;
	
		i = s[57];
		j += (i + k[57]);	
		s[57] = s[j];
		s[j] = i;
	
		i = s[58];
		j += (i + k[58]);	
		s[58] = s[j];
		s[j] = i;
	
		i = s[59];
		j += (i + k[59]);	
		s[59] = s[j];
		s[j] = i;
	
		i = s[60];
		j += (i + k[60]);	
		s[60] = s[j];
		s[j] = i;
	
		i = s[61];
		j += (i + k[61]);	
		s[61] = s[j];
		s[j] = i;
	
		i = s[62];
		j += (i + k[62]);	
		s[62] = s[j];
		s[j] = i;
	
		i = s[63];
		j += (i + k[63]);	
		s[63] = s[j];
		s[j] = i;
	
		i = s[64];
		j += (i + k[64]);	
		s[64] = s[j];
		s[j] = i;
	
		i = s[65];
		j += (i + k[65]);	
		s[65] = s[j];
		s[j] = i;
	
		i = s[66];
		j += (i + k[66]);	
		s[66] = s[j];
		s[j] = i;
	
		i = s[67];
		j += (i + k[67]);	
		s[67] = s[j];
		s[j] = i;
	
		i = s[68];
		j += (i + k[68]);	
		s[68] = s[j];
		s[j] = i;
	
		i = s[69];
		j += (i + k[69]);	
		s[69] = s[j];
		s[j] = i;
	
		i = s[70];
		j += (i + k[70]);	
		s[70] = s[j];
		s[j] = i;
	
		i = s[71];
		j += (i + k[71]);	
		s[71] = s[j];
		s[j] = i;
	
		i = s[72];
		j += (i + k[72]);	
		s[72] = s[j];
		s[j] = i;
	
		i = s[73];
		j += (i + k[73]);	
		s[73] = s[j];
		s[j] = i;
	
		i = s[74];
		j += (i + k[74]);	
		s[74] = s[j];
		s[j] = i;
	
		i = s[75];
		j += (i + k[75]);	
		s[75] = s[j];
		s[j] = i;
	
		i = s[76];
		j += (i + k[76]);	
		s[76] = s[j];
		s[j] = i;
	
		i = s[77];
		j += (i + k[77]);	
		s[77] = s[j];
		s[j] = i;
	
		i = s[78];
		j += (i + k[78]);	
		s[78] = s[j];
		s[j] = i;
	
		i = s[79];
		j += (i + k[79]);	
		s[79] = s[j];
		s[j] = i;
	
		i = s[80];
		j += (i + k[80]);	
		s[80] = s[j];
		s[j] = i;
	
		i = s[81];
		j += (i + k[81]);	
		s[81] = s[j];
		s[j] = i;
	
		i = s[82];
		j += (i + k[82]);	
		s[82] = s[j];
		s[j] = i;
	
		i = s[83];
		j += (i + k[83]);	
		s[83] = s[j];
		s[j] = i;
	
		i = s[84];
		j += (i + k[84]);	
		s[84] = s[j];
		s[j] = i;
	
		i = s[85];
		j += (i + k[85]);	
		s[85] = s[j];
		s[j] = i;
	
		i = s[86];
		j += (i + k[86]);	
		s[86] = s[j];
		s[j] = i;
	
		i = s[87];
		j += (i + k[87]);	
		s[87] = s[j];
		s[j] = i;
	
		i = s[88];
		j += (i + k[88]);	
		s[88] = s[j];
		s[j] = i;
	
		i = s[89];
		j += (i + k[89]);	
		s[89] = s[j];
		s[j] = i;
	
		i = s[90];
		j += (i + k[90]);	
		s[90] = s[j];
		s[j] = i;
	
		i = s[91];
		j += (i + k[91]);	
		s[91] = s[j];
		s[j] = i;
	
		i = s[92];
		j += (i + k[92]);	
		s[92] = s[j];
		s[j] = i;
	
		i = s[93];
		j += (i + k[93]);	
		s[93] = s[j];
		s[j] = i;
	
		i = s[94];
		j += (i + k[94]);	
		s[94] = s[j];
		s[j] = i;
	
		i = s[95];
		j += (i + k[95]);	
		s[95] = s[j];
		s[j] = i;
	
		i = s[96];
		j += (i + k[96]);	
		s[96] = s[j];
		s[j] = i;
	
		i = s[97];
		j += (i + k[97]);	
		s[97] = s[j];
		s[j] = i;
	
		i = s[98];
		j += (i + k[98]);	
		s[98] = s[j];
		s[j] = i;
	
		i = s[99];
		j += (i + k[99]);	
		s[99] = s[j];
		s[j] = i;
	
		i = s[100];
		j += (i + k[100]);	
		s[100] = s[j];
		s[j] = i;
	
		i = s[101];
		j += (i + k[101]);	
		s[101] = s[j];
		s[j] = i;
	
		i = s[102];
		j += (i + k[102]);	
		s[102] = s[j];
		s[j] = i;
	
		i = s[103];
		j += (i + k[103]);	
		s[103] = s[j];
		s[j] = i;
	
		i = s[104];
		j += (i + k[104]);	
		s[104] = s[j];
		s[j] = i;
	
		i = s[105];
		j += (i + k[105]);	
		s[105] = s[j];
		s[j] = i;
	
		i = s[106];
		j += (i + k[106]);	
		s[106] = s[j];
		s[j] = i;
	
		i = s[107];
		j += (i + k[107]);	
		s[107] = s[j];
		s[j] = i;
	
		i = s[108];
		j += (i + k[108]);	
		s[108] = s[j];
		s[j] = i;
	
		i = s[109];
		j += (i + k[109]);	
		s[109] = s[j];
		s[j] = i;
	
		i = s[110];
		j += (i + k[110]);	
		s[110] = s[j];
		s[j] = i;
	
		i = s[111];
		j += (i + k[111]);	
		s[111] = s[j];
		s[j] = i;
	
		i = s[112];
		j += (i + k[112]);	
		s[112] = s[j];
		s[j] = i;
	
		i = s[113];
		j += (i + k[113]);	
		s[113] = s[j];
		s[j] = i;
	
		i = s[114];
		j += (i + k[114]);	
		s[114] = s[j];
		s[j] = i;
	
		i = s[115];
		j += (i + k[115]);	
		s[115] = s[j];
		s[j] = i;
	
		i = s[116];
		j += (i + k[116]);	
		s[116] = s[j];
		s[j] = i;
	
		i = s[117];
		j += (i + k[117]);	
		s[117] = s[j];
		s[j] = i;
	
		i = s[118];
		j += (i + k[118]);	
		s[118] = s[j];
		s[j] = i;
	
		i = s[119];
		j += (i + k[119]);	
		s[119] = s[j];
		s[j] = i;
	
		i = s[120];
		j += (i + k[120]);	
		s[120] = s[j];
		s[j] = i;
	
		i = s[121];
		j += (i + k[121]);	
		s[121] = s[j];
		s[j] = i;
	
		i = s[122];
		j += (i + k[122]);	
		s[122] = s[j];
		s[j] = i;
	
		i = s[123];
		j += (i + k[123]);	
		s[123] = s[j];
		s[j] = i;
	
		i = s[124];
		j += (i + k[124]);	
		s[124] = s[j];
		s[j] = i;
	
		i = s[125];
		j += (i + k[125]);	
		s[125] = s[j];
		s[j] = i;
	
		i = s[126];
		j += (i + k[126]);	
		s[126] = s[j];
		s[j] = i;
	
		i = s[127];
		j += (i + k[127]);	
		s[127] = s[j];
		s[j] = i;
	
		i = s[128];
		j += (i + k[128]);	
		s[128] = s[j];
		s[j] = i;
	
		i = s[129];
		j += (i + k[129]);	
		s[129] = s[j];
		s[j] = i;
	
		i = s[130];
		j += (i + k[130]);	
		s[130] = s[j];
		s[j] = i;
	
		i = s[131];
		j += (i + k[131]);	
		s[131] = s[j];
		s[j] = i;
	
		i = s[132];
		j += (i + k[132]);	
		s[132] = s[j];
		s[j] = i;
	
		i = s[133];
		j += (i + k[133]);	
		s[133] = s[j];
		s[j] = i;
	
		i = s[134];
		j += (i + k[134]);	
		s[134] = s[j];
		s[j] = i;
	
		i = s[135];
		j += (i + k[135]);	
		s[135] = s[j];
		s[j] = i;
	
		i = s[136];
		j += (i + k[136]);	
		s[136] = s[j];
		s[j] = i;
	
		i = s[137];
		j += (i + k[137]);	
		s[137] = s[j];
		s[j] = i;
	
		i = s[138];
		j += (i + k[138]);	
		s[138] = s[j];
		s[j] = i;
	
		i = s[139];
		j += (i + k[139]);	
		s[139] = s[j];
		s[j] = i;
	
		i = s[140];
		j += (i + k[140]);	
		s[140] = s[j];
		s[j] = i;
	
		i = s[141];
		j += (i + k[141]);	
		s[141] = s[j];
		s[j] = i;
	
		i = s[142];
		j += (i + k[142]);	
		s[142] = s[j];
		s[j] = i;
	
		i = s[143];
		j += (i + k[143]);	
		s[143] = s[j];
		s[j] = i;
	
		i = s[144];
		j += (i + k[144]);	
		s[144] = s[j];
		s[j] = i;
	
		i = s[145];
		j += (i + k[145]);	
		s[145] = s[j];
		s[j] = i;
	
		i = s[146];
		j += (i + k[146]);	
		s[146] = s[j];
		s[j] = i;
	
		i = s[147];
		j += (i + k[147]);	
		s[147] = s[j];
		s[j] = i;
	
		i = s[148];
		j += (i + k[148]);	
		s[148] = s[j];
		s[j] = i;
	
		i = s[149];
		j += (i + k[149]);	
		s[149] = s[j];
		s[j] = i;
	
		i = s[150];
		j += (i + k[150]);	
		s[150] = s[j];
		s[j] = i;
	
		i = s[151];
		j += (i + k[151]);	
		s[151] = s[j];
		s[j] = i;
	
		i = s[152];
		j += (i + k[152]);	
		s[152] = s[j];
		s[j] = i;
	
		i = s[153];
		j += (i + k[153]);	
		s[153] = s[j];
		s[j] = i;
	
		i = s[154];
		j += (i + k[154]);	
		s[154] = s[j];
		s[j] = i;
	
		i = s[155];
		j += (i + k[155]);	
		s[155] = s[j];
		s[j] = i;
	
		i = s[156];
		j += (i + k[156]);	
		s[156] = s[j];
		s[j] = i;
	
		i = s[157];
		j += (i + k[157]);	
		s[157] = s[j];
		s[j] = i;
	
		i = s[158];
		j += (i + k[158]);	
		s[158] = s[j];
		s[j] = i;
	
		i = s[159];
		j += (i + k[159]);	
		s[159] = s[j];
		s[j] = i;
	
		i = s[160];
		j += (i + k[160]);	
		s[160] = s[j];
		s[j] = i;
	
		i = s[161];
		j += (i + k[161]);	
		s[161] = s[j];
		s[j] = i;
	
		i = s[162];
		j += (i + k[162]);	
		s[162] = s[j];
		s[j] = i;
	
		i = s[163];
		j += (i + k[163]);	
		s[163] = s[j];
		s[j] = i;
	
		i = s[164];
		j += (i + k[164]);	
		s[164] = s[j];
		s[j] = i;
	
		i = s[165];
		j += (i + k[165]);	
		s[165] = s[j];
		s[j] = i;
	
		i = s[166];
		j += (i + k[166]);	
		s[166] = s[j];
		s[j] = i;
	
		i = s[167];
		j += (i + k[167]);	
		s[167] = s[j];
		s[j] = i;
	
		i = s[168];
		j += (i + k[168]);	
		s[168] = s[j];
		s[j] = i;
	
		i = s[169];
		j += (i + k[169]);	
		s[169] = s[j];
		s[j] = i;
	
		i = s[170];
		j += (i + k[170]);	
		s[170] = s[j];
		s[j] = i;
	
		i = s[171];
		j += (i + k[171]);	
		s[171] = s[j];
		s[j] = i;
	
		i = s[172];
		j += (i + k[172]);	
		s[172] = s[j];
		s[j] = i;
	
		i = s[173];
		j += (i + k[173]);	
		s[173] = s[j];
		s[j] = i;
	
		i = s[174];
		j += (i + k[174]);	
		s[174] = s[j];
		s[j] = i;
	
		i = s[175];
		j += (i + k[175]);	
		s[175] = s[j];
		s[j] = i;
	
		i = s[176];
		j += (i + k[176]);	
		s[176] = s[j];
		s[j] = i;
	
		i = s[177];
		j += (i + k[177]);	
		s[177] = s[j];
		s[j] = i;
	
		i = s[178];
		j += (i + k[178]);	
		s[178] = s[j];
		s[j] = i;
	
		i = s[179];
		j += (i + k[179]);	
		s[179] = s[j];
		s[j] = i;
	
		i = s[180];
		j += (i + k[180]);	
		s[180] = s[j];
		s[j] = i;
	
		i = s[181];
		j += (i + k[181]);	
		s[181] = s[j];
		s[j] = i;
	
		i = s[182];
		j += (i + k[182]);	
		s[182] = s[j];
		s[j] = i;
	
		i = s[183];
		j += (i + k[183]);	
		s[183] = s[j];
		s[j] = i;
	
		i = s[184];
		j += (i + k[184]);	
		s[184] = s[j];
		s[j] = i;
	
		i = s[185];
		j += (i + k[185]);	
		s[185] = s[j];
		s[j] = i;
	
		i = s[186];
		j += (i + k[186]);	
		s[186] = s[j];
		s[j] = i;
	
		i = s[187];
		j += (i + k[187]);	
		s[187] = s[j];
		s[j] = i;
	
		i = s[188];
		j += (i + k[188]);	
		s[188] = s[j];
		s[j] = i;
	
		i = s[189];
		j += (i + k[189]);	
		s[189] = s[j];
		s[j] = i;
	
		i = s[190];
		j += (i + k[190]);	
		s[190] = s[j];
		s[j] = i;
	
		i = s[191];
		j += (i + k[191]);	
		s[191] = s[j];
		s[j] = i;
	
		i = s[192];
		j += (i + k[192]);	
		s[192] = s[j];
		s[j] = i;
	
		i = s[193];
		j += (i + k[193]);	
		s[193] = s[j];
		s[j] = i;
	
		i = s[194];
		j += (i + k[194]);	
		s[194] = s[j];
		s[j] = i;
	
		i = s[195];
		j += (i + k[195]);	
		s[195] = s[j];
		s[j] = i;
	
		i = s[196];
		j += (i + k[196]);	
		s[196] = s[j];
		s[j] = i;
	
		i = s[197];
		j += (i + k[197]);	
		s[197] = s[j];
		s[j] = i;
	
		i = s[198];
		j += (i + k[198]);	
		s[198] = s[j];
		s[j] = i;
	
		i = s[199];
		j += (i + k[199]);	
		s[199] = s[j];
		s[j] = i;
	
		i = s[200];
		j += (i + k[200]);	
		s[200] = s[j];
		s[j] = i;
	
		i = s[201];
		j += (i + k[201]);	
		s[201] = s[j];
		s[j] = i;
	
		i = s[202];
		j += (i + k[202]);	
		s[202] = s[j];
		s[j] = i;
	
		i = s[203];
		j += (i + k[203]);	
		s[203] = s[j];
		s[j] = i;
	
		i = s[204];
		j += (i + k[204]);	
		s[204] = s[j];
		s[j] = i;
	
		i = s[205];
		j += (i + k[205]);	
		s[205] = s[j];
		s[j] = i;
	
		i = s[206];
		j += (i + k[206]);	
		s[206] = s[j];
		s[j] = i;
	
		i = s[207];
		j += (i + k[207]);	
		s[207] = s[j];
		s[j] = i;
	
		i = s[208];
		j += (i + k[208]);	
		s[208] = s[j];
		s[j] = i;
	
		i = s[209];
		j += (i + k[209]);	
		s[209] = s[j];
		s[j] = i;
	
		i = s[210];
		j += (i + k[210]);	
		s[210] = s[j];
		s[j] = i;
	
		i = s[211];
		j += (i + k[211]);	
		s[211] = s[j];
		s[j] = i;
	
		i = s[212];
		j += (i + k[212]);	
		s[212] = s[j];
		s[j] = i;
	
		i = s[213];
		j += (i + k[213]);	
		s[213] = s[j];
		s[j] = i;
	
		i = s[214];
		j += (i + k[214]);	
		s[214] = s[j];
		s[j] = i;
	
		i = s[215];
		j += (i + k[215]);	
		s[215] = s[j];
		s[j] = i;
	
		i = s[216];
		j += (i + k[216]);	
		s[216] = s[j];
		s[j] = i;
	
		i = s[217];
		j += (i + k[217]);	
		s[217] = s[j];
		s[j] = i;
	
		i = s[218];
		j += (i + k[218]);	
		s[218] = s[j];
		s[j] = i;
	
		i = s[219];
		j += (i + k[219]);	
		s[219] = s[j];
		s[j] = i;
	
		i = s[220];
		j += (i + k[220]);	
		s[220] = s[j];
		s[j] = i;
	
		i = s[221];
		j += (i + k[221]);	
		s[221] = s[j];
		s[j] = i;
	
		i = s[222];
		j += (i + k[222]);	
		s[222] = s[j];
		s[j] = i;
	
		i = s[223];
		j += (i + k[223]);	
		s[223] = s[j];
		s[j] = i;
	
		i = s[224];
		j += (i + k[224]);	
		s[224] = s[j];
		s[j] = i;
	
		i = s[225];
		j += (i + k[225]);	
		s[225] = s[j];
		s[j] = i;
	
		i = s[226];
		j += (i + k[226]);	
		s[226] = s[j];
		s[j] = i;
	
		i = s[227];
		j += (i + k[227]);	
		s[227] = s[j];
		s[j] = i;
	
		i = s[228];
		j += (i + k[228]);	
		s[228] = s[j];
		s[j] = i;
	
		i = s[229];
		j += (i + k[229]);	
		s[229] = s[j];
		s[j] = i;
	
		i = s[230];
		j += (i + k[230]);	
		s[230] = s[j];
		s[j] = i;
	
		i = s[231];
		j += (i + k[231]);	
		s[231] = s[j];
		s[j] = i;
	
		i = s[232];
		j += (i + k[232]);	
		s[232] = s[j];
		s[j] = i;
	
		i = s[233];
		j += (i + k[233]);	
		s[233] = s[j];
		s[j] = i;
	
		i = s[234];
		j += (i + k[234]);	
		s[234] = s[j];
		s[j] = i;
	
		i = s[235];
		j += (i + k[235]);	
		s[235] = s[j];
		s[j] = i;
	
		i = s[236];
		j += (i + k[236]);	
		s[236] = s[j];
		s[j] = i;
	
		i = s[237];
		j += (i + k[237]);	
		s[237] = s[j];
		s[j] = i;
	
		i = s[238];
		j += (i + k[238]);	
		s[238] = s[j];
		s[j] = i;
	
		i = s[239];
		j += (i + k[239]);	
		s[239] = s[j];
		s[j] = i;
	
		i = s[240];
		j += (i + k[240]);	
		s[240] = s[j];
		s[j] = i;
	
		i = s[241];
		j += (i + k[241]);	
		s[241] = s[j];
		s[j] = i;
	
		i = s[242];
		j += (i + k[242]);	
		s[242] = s[j];
		s[j] = i;
	
		i = s[243];
		j += (i + k[243]);	
		s[243] = s[j];
		s[j] = i;
	
		i = s[244];
		j += (i + k[244]);	
		s[244] = s[j];
		s[j] = i;
	
		i = s[245];
		j += (i + k[245]);	
		s[245] = s[j];
		s[j] = i;
	
		i = s[246];
		j += (i + k[246]);	
		s[246] = s[j];
		s[j] = i;
	
		i = s[247];
		j += (i + k[247]);	
		s[247] = s[j];
		s[j] = i;
	
		i = s[248];
		j += (i + k[248]);	
		s[248] = s[j];
		s[j] = i;
	
		i = s[249];
		j += (i + k[249]);	
		s[249] = s[j];
		s[j] = i;
	
		i = s[250];
		j += (i + k[250]);	
		s[250] = s[j];
		s[j] = i;
	
		i = s[251];
		j += (i + k[251]);	
		s[251] = s[j];
		s[j] = i;
	
		i = s[252];
		j += (i + k[252]);	
		s[252] = s[j];
		s[j] = i;
	
		i = s[253];
		j += (i + k[253]);	
		s[253] = s[j];
		s[j] = i;
	
		i = s[254];
		j += (i + k[254]);	
		s[254] = s[j];
		s[j] = i;
	
		i = s[255];
		j += (i + k[255]);	
		s[255] = s[j];
		s[j] = i;

}

inline void get_byte(unsigned char *out, unsigned char *s, unsigned char *i, 
		unsigned char *j)
{
	unsigned char k;

	(*i) ++;
	(*j) += s[*i];
	k = s[*i];
	s[*i] = s[*j];
	s[*j] = k;
	k = (s[*i] + s[*j]);
	*out = s[k];
}

int main(int argc, char **argv)
{
	/* for encrypting and guessing */
	unsigned char key[5];
	
	/* boxes in rc4 */
	unsigned char s_box[256];
	unsigned char k_box[256];

	/* for holding the various texts */
	unsigned char cipher[80];
	unsigned char decrypt[80];
	unsigned char plain[80];
	
	FILE *pt_fd, *ct_fd;
	char *pt_file, *ct_file;

	/* timing */
	time_t c_start, c_stop;
	

	unsigned char first_byte, byte;
	int i, pt_len;
	unsigned char enc_i = 0, enc_j = 0;
	
	if (argc != 3)
	{
		fprintf(stderr, "usage: %s pt_file ct_file\n", argv[0]);
		exit(0);
	}
	
	bzero(plain, 80);
	bzero(cipher, 80);
	pt_file = argv[1];
	ct_file = argv[2];
	
	pt_fd = fopen(pt_file, "r");
	fgets(plain, 80, pt_fd);
	ct_fd = fopen(ct_file, "r");
	fgets(cipher, 80, ct_fd);
	fclose(pt_fd);
	fclose(ct_fd);

	pt_len = strlen(plain);

	printf("pt (length %d): ", strlen(plain));
	for (i = 0; i < strlen(plain); i ++)
		printf("%3d ", plain[i]);
	printf("\nct (length %d): ", strlen(cipher));
	for (i = 0; i < strlen(cipher); i ++)
		printf("%3d ", cipher[i]);
	printf("\n");
	
	first_byte = plain[0] ^ cipher[0];


	printf("\n----\n");
	fflush(stdout);
	
	/* begin brute force */

	bzero(key, 5 * sizeof(char));
	bzero(k_box, 256 * sizeof(char));
	bzero(s_box, 256 * sizeof(char));
	time(&c_start);
	printf("START: %s----\n", ctime(&c_start));
	do
	{
		init_k_brute(k_box, 0, (unsigned char)key[0]);
		do
		{
			init_k_brute(k_box, 1, (unsigned char)key[1]);
			do
			{
				init_k_brute(k_box, 2, (unsigned char)key[2]);
				do
				{
					init_k_brute(k_box, 3, (unsigned char)key[3]);
					do
					{

						init_k_brute(k_box, 4, (unsigned char)key[4]);
						init_s(s_box);
						make_s(k_box, s_box);

						if ((s_box[(s_box[1] + s_box[s_box[1]]) & 0xff]) == first_byte)
						{
							decrypt[0] = plain[0];
							enc_i = enc_j = 0;
							get_byte(&byte, s_box, &enc_i, &enc_j);
							
							i = 1;
							do
							{
								get_byte(&byte, s_box, &enc_i, &enc_j);
								decrypt[i] = cipher[i] ^ byte;
								i ++;
								if (i == pt_len)
								{
									time(&c_stop);
									printf("----\n");
									printf("KEY    %4d %4d %4d %4d %4d\n", 
											key[0], key[1], key[2], key[3], key[4]);
									printf("CIPHER %4d %4d %4d %4d %4d -> ", 
											cipher[0], cipher[1], cipher[2], cipher[3], cipher[4]);
									printf("PLAIN  %4d %4d %4d %4d %4d", 
											decrypt[0], decrypt[1], decrypt[2], decrypt[3], decrypt[4]);
									printf("\n----\n");
									printf("STOP: %s----\n", ctime(&c_stop));
									c_stop -= c_start;
									printf("ELAPSED: %d seconds\n", c_stop);
									exit(0);
								}
							}
							while ( (i < pt_len) && (decrypt[i - 1] == plain[i - 1]) );
						}
						key[4] ++;
					} while (key[4] != 0);
					key[3] ++;
				} while (key[3] != 0);
				key[2] ++;
			} while (key[2] != 0);
			key[1] ++;
		} while (key[1] != 0);
		key[0] ++;
	} while (key[0] != 0);
}

