mIRC Home    About    Download    Register    News    Help

Print Thread
#76784 26/03/04 05:59 PM
Joined: Oct 2003
Posts: 101
R
root66 Offline OP
Vogon poet
OP Offline
Vogon poet
R
Joined: Oct 2003
Posts: 101
This script is for supporting NAIM's autocrypt XOR encryption. You can find an explaination of the protocol here: http://shell.n.ml.org/n/naim/docs/CTCP.html

I am starting to suspect mIRC has a flaw in $xor but I could easily be wrong. I only suspect the problem because it loops sequentially, and the message becomes garbled at a random spot within the first 10 or so characters. The key however is 20 characters, so its a really odd problem that it should mess up before at least 19-20 characters.

In a nutshell, once a handshake is made (see the query popup menu to establish) any input into that query is encrypted, converted to hex, and sent in a CTCP HEXTEXT. It is encrypted by taking the $xor(m,k) of each character, where m is a sequential character in the message, and k a character in the key. If the message is longer than the key, the key loops. Thats why I find it odd that it garbles before it makes one loop.

I am certain that the only problem is converting outgoing text (on input) because the script works perfectly for receiving encrypted text from a NAIM client.

Code:
on *:input:?:{
  if ($left($1,1) != /) && ($hget(autocrypt $+ $cid,$active,&k) != $null) {
    bset -t &m 1 $1- | var %i = 0 , %h , %kl = $bvar(&k,0) , %n = 0, %s = $bvar(&m,0)
    while (%i < %s) {
      inc %i | inc %n
      var %h = %h $+ $base($xor($bvar(&k,%n),$bvar(&m,%i)),10,16)
      if (%n == %kl) { %n = 0 }
    }
    .ctcp $active HEXTEXT %h
    echo -a %kl $1- %h 
    echo -cimt own $active $+(<,$active,>) $1-
    haltdef
  }
}

ctcp *:autopeer *:?:{
  if ($2- == -AUTOCRYPT) || ($2- == -AUTOPEER) {
    hdel autocrypt $+ $cid $nick
    if ($nick != $me) { .ctcp $nick AUTOPEER -AUTOPEER }
    echo 4 $iif($query($nick),-eit $nick,-eist) * AutoCrypt Lost $+ $iif($query($nick) == $null,: $nick)
  }
  elseif (+AUTOPEER:? iswm $2) {
    if ($3 == +AUTOCRYPT) { ac_keygen $nick | echo 4 -eit $nick * AutoCrypt Established }
    else { ctcp $nick AUTOPEER $2 +AUTOCRYPT }
  }
  elseif ($gettok($2,1,58) == +AUTOCRYPT) {
    bset -t &b 1 $gettok($2,2,58)
    hadd -mb autocrypt $+ $cid $nick &b
    echo 4 -eit $nick * AutoCrypt Established
  }
  haltdef
}

ctcp *:hextext:?:{
  if $hget(autocrypt $+ $cid,$nick) {
    var %i = 1 , %s = $len($2)
    while (%i <= %s) { bset &r $calc($bvar(&r,0) + 1) $base($mid($2,%i,2),16,10) | inc %i 2 }
    hadd -mb autocrypt $+ $cid buffer &r
    ac_decrypt $nick $address
    haltdef
  }
}

alias autocrypt {
  if ($$1 == -r) { ctcp $$2 AUTOPEER -AUTOCRYPT | hdel autocrypt $+ $cid $2 }
  else { ctcp $1 AUTOPEER +AUTOPEER:4 +AUTOCRYPT }
}

alias -l ac_keygen {
  var %k, %i = 0
  while (%i < 20) { inc %i | %k = %k $+ $chr($r(48,126)) }
  hadd -m autocrypt $+ $cid $1 %k
  .ctcp $$1 AUTOPEER +AUTOCRYPT: $+ %k
}

alias -l ac_decrypt {
  var %i = $hget(autocrypt $+ $cid,$$1,&k) , %i = $hget(autocrypt $+ $cid,buffer,&r), %i = 0 , %n , %r , %s = $bvar(&r,0), %sp
  while (%i < %s) {
    inc %i | inc %n
    var %x = $xor($bvar(&r,%i),$bvar(&k,%n)) , %r = %r $+ $iif(%sp == 1,$chr(32)) $+ $chr(%x) , %sp = $iif(%x == 32,1,0)
  if (%n == $bvar(&k,0)) { %n = 0 } }
  if ($query($1) == $null) { query -n $nick }
  hdel autocrypt $+ $cid buffer
  echo -imt $1 $+(<,$1,>) %r
}

on ^*:text:*:?:{
  if $hget(autocrypt $+ $cid,$nick) {
    bset -t &r 1 $1-
    hadd -mb autocrypt $+ $cid buffer &b
    ac_decrypt $nick $address
    haltdef
  }
}

menu Query {
  -
  $iif($hget(autocrypt $+ $cid,$$1),Disable,Establish) AutoCrypt:autocrypt $iif($hget(autocrypt $+ $cid,$$1),-r) $1
  -
}

#76785 26/03/04 06:37 PM
Joined: Dec 2002
Posts: 2,884
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,884
Well to be honest I'm too lazy to read the code, however the encryption is the same as the 'roast' method used by AIM, and you can find $roast()/$unroast() aliases here which (I think) will do what you need.

#76786 26/03/04 11:12 PM
Joined: Oct 2003
Posts: 101
R
root66 Offline OP
Vogon poet
OP Offline
Vogon poet
R
Joined: Oct 2003
Posts: 101
haha turns out it was stripping trailing 0's from hex numbers like 04 and 07. I just added a $iif so it would add the 0 if the length of a new character was only 1.


#76787 26/03/04 11:24 PM
Joined: Dec 2002
Posts: 2,884
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 2,884
Ah, well in that case $base() has a 4th parameter you can set to zero-pad the result, so $base(4, 10, 16, 2) would return '04', so there's no need for a $iif().

#76788 27/03/04 01:15 AM
Joined: Oct 2003
Posts: 101
R
root66 Offline OP
Vogon poet
OP Offline
Vogon poet
R
Joined: Oct 2003
Posts: 101
even better laugh


Link Copied to Clipboard