****************************************************************************
* Rivest's Cipher 4 toolkit for mIRC32                                     *
*                                                                          *
* Hurriedly made by Quension.  (C) 2000 by Quension too.                   *
* For free, noncommercial use only, at your own risk.                      *
*                                                                          *
* You can modify the source and distribute both source and any DLLs built  *
* by you (subject to export rules governing strong encryption), as long as *
* you don't charge any money for it.  You must also make clear the fact    *
* that it has been modified.  Commercial use is strictly prohibited.       *
****************************************************************************

rc4toolkit.dll is a 256-bit RC4 encryption/decryption utility for mIRC.  It
contains everything you should need to create RC4-based cipher scripts.

There are 3 main crypt functions in the RC4 toolkit.
- The first is simply RC4, and uses the plain Rivest Cipher 4 algorithm.
- The second is CS-1 (CipherSabre 1), and attaches a 10-byte initialization
  vector to the front of each encrypted packet.  The IV is randomly generated
  and serves to make the key unique for each packet.
- The third is a modified CS-2 (CipherSabre 2) algorithm.  In addition to
  using the initialization vector, the state mixer is looped x number of
  times to ensure a unique state array.  The CS-2 specification calls for
  the loop number to be exchanged with each side, in addition to the key.
  Instead, this DLL uses the IV in combination with the key array to select
  the number of times to loop.

There are also two converters (from raw to hex and back again), as well as
a random byte generator.

There are two forms of data the DLL will accept and return: ASCII-armored
hex; and raw.  ASCII-armored hex is a string returned that represents raw
data.  For example, "68656c6c6f" represents "hello".  This is used because
not all characters can be transferred over IRC (or even between the DLL and
mIRC).  Raw data is simply the plain stuff, for example just "hello" -- no
fancy junk.

Now, on to the DLL procedures.  Type "/help $dll" in mIRC for info on how to
use these.

The crypt functions all expect the password, followed by the byte value 1,
followed by the data you wish to crypt.  An example DLL call:
$dll(rc4toolkit.dll, cs2_enc_raw, password [ $+ [ $chr(01) ] $+ ] data)

Procedure    Description
-----------  ---------------------------------------------------------------
rc4_enc_raw  encrypts raw using a raw key, outputs hex data (RC4 algorithm)
rc4_dec_raw  decrypts hex using a raw key, outputs raw data (RC4 algorithm)
cs1_enc_raw  encrypts raw using a raw key, outputs hex data (CS-1 algorithm)
cs1_dec_raw  decrypts hex using a raw key, outputs raw data (CS-1 algorithm)
cs2_enc_raw  encrypts raw using a raw key, outputs hex data (modified CS-2)
cs2_dec_raw  decrypts hex using a raw key, outputs raw data (modified CS-2)

rc4_enc_hex  encrypts raw using a hex key, outputs hex data (RC4)
rc4_dec_hex  decrypts hex using a hex key, outputs raw data (RC4)
cs1_enc_hex  encrypts raw using a hex key, outputs hex data (CS-1)
cs1_dec_hex  decrypts hex using a hex key, outputs raw data (CS-1)
cs2_enc_hex  encrypts raw using a hex key, outputs hex data (modified CS-2)
cs2_dec_hex  decrypts hex using a hex key, outputs raw data (modified CS-2)

rc4_symmetric  does a symmetric (two-way) crypt on hex data with a hex key
-----------  ---------------------------------------------------------------

The following are miscellaneous procedures that only take one parameter.

Procedure    Description
-----------  ---------------------------------------------------------------
cnv_raw2hex  converts the supplied raw data into hex
cnv_hex2raw  converts the supplied hex data into raw
get_rnd_hex  gets N number of random bytes (hex encoded)
-----------  ---------------------------------------------------------------

Some random examples:
...
alias enc {
  say $dll(rc4toolkit.dll, cs2_enc_raw, mykey [ $+ [ $chr(01) ] $+ ] [ $1- ] )
}

alias dec {
  echo -a $dll(rc4toolkit.dll, cs2_dec_raw, mykey [ $+ [ $chr(01) ] $+ ] [ $1- ] )
}
...
alias setpass {
  set %password $dll(rc4toolkit.dll, cnv_raw2hex, [ $1- ] )
}
alias enc {
  say $dll(rc4toolkit.dll, rc4_enc_hex, [ %password ] [ $+ [ $chr(01) ] $+ ] [ $1- ] )
}
...
alias enc {
  set %key $dll(rc4toolkit.dll, get_rnd_hex, 20 )
  set %data $dll(rc4toolkit.dll, cnv_raw2hex, [ $1- ] )
  say %key $+ $dll(rc4toolkit.dll, rc4_symmetric, [ %key ] [ $+ [ $chr(01) ] $+ ] [ %data ] )
}
...

Those should give you some ideas.

Enjoy!
- Quension

