mIRC Home    About    Download    Register    News    Help

Print Thread
#172014 03/03/07 11:09 PM
Joined: Jan 2007
Posts: 259
K
Fjord artisan
OP Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
I'm trying to encrypt a string, by xor chaining it ($xor(a,b) -> $xor(b,c) -> $xor(c,<result of $xor(a,b))), then xoring it by the first letter of the password, xor chaining, then the second letter, etc.
However, I'm encountering problems when the password has a few characters ("test", for example) where it won't decrypt it properly. It also seems pretty random, as it sometimes decrypts, and sometimes dosen't.

Code so far:
Code:
alias xorc {
  var %x = 1, %l = $len($1), %op
  while %x <= %l {
    var %cchar = $mid($1,%x,1)
    if (%x == %l) var %nchar = $mid(%op,1,1)
    else var %nchar = $mid($1-,$calc(%x + 1),1)
    var %op = $+(%op,$chr($xor($asc(%cchar),$asc(%nchar))))
    inc %x
  }
  return %op
}
alias rxorc {
  var %x = $len($1), %l = $len($1), %op
  while %x > 0 {
    var %cchar = $mid($1,%x,1)
    if (%x == %l) var %nchar = $mid($1,1,1)
    else var %nchar = $mid(%op,1,1)
    var %op = $+($chr($xor($asc(%cchar),$asc(%nchar))),%op)
    dec %x
  }
  return %op
}
alias fxor {
  var %x = 1, %l = $len($1), %op
  while %x <= %l {
    var %cchar = $mid($1,%x,1)
    var %op = $+(%op,$chr($xor($asc(%cchar),$asc($2))))
    inc %x
  }
  return %op
}
alias xortest {
  ; $1 = data, $2 = password
  if ($len($2) == $null) return
  var %x = 1, %l = $len($2)
  echo -a *** Data: $1
  echo -a *** Password: $2
  var %res = $fxor($xorc($1),$mid($2,%x,1))
  echo -a *** Stage 1 (Chain and apply xor of $qt($mid($2,%x,1)) $+ ): %res
  inc %x
  while %x <= %l {
    var %ores = %res, %res = $fxor($xorc(%ores),$mid($2,%x,1))
    echo -a *** Stage %x (Chain and apply xor of $qt($mid($2,%x,1)) $+ ): %res
    inc %x
  }
  echo -a Final result: %res
  echo -a Attempting decrypt...
  echo -a *** Data: %res
  echo -a *** Password: $2
  var %x = $len($2)
  var %res = $rxorc($fxor(%res,$mid($2,%x,1))))
  echo -a *** Remove stage %x (Dechain and apply xor of $qt($mid($2,%x,1)) $+ ): %res
  dec %x
  while %x > 0 {
    var %ores = %res, %res = $rxorc($fxor(%ores,$mid($2,%x,1))))
    echo -a *** Remove stage %x (Dechain and apply xor of $qt($mid($2,%x,1)) $+ ): %res
    dec %x
  }
  echo -a Decrypted result: %res
}


Examples of it screwing itself:
Code:
*** Data: test
*** Password: a
*** Stage 1 (Chain and apply xor of "a"): pwf
Final result: pwf
Attempting decrypt...
*** Data: pwf
*** Password: a
*** Remove stage 1 (Dechain and apply xor of "a"): test
Decrypted result: test
--
*** Data: test
*** Password: aa
*** Stage 1 (Chain and apply xor of "a"): pwf
*** Stage 2 (Chain and apply xor of "a"): fpb
Final result: fpb
Attempting decrypt...
*** Data: fpb
*** Password: aa
*** Remove stage 2 (Dechain and apply xor of "a"): pwf
*** Remove stage 1 (Dechain and apply xor of "a"): test
Decrypted result: test
--
*** Data: test
*** Password: aaa
*** Stage 1 (Chain and apply xor of "a"): pwf
*** Stage 2 (Chain and apply xor of "a"): fpb
*** Stage 3 (Chain and apply xor of "a"): w
Final result: w
Attempting decrypt...
*** Data: w
*** Password: aaa
*** Remove stage 3 (Dechain and apply xor of "a"): b
*** Remove stage 2 (Dechain and apply xor of "a"): se
*** Remove stage 1 (Dechain and apply xor of "a"): pb
Decrypted result: pb
----
----
*** Data: test
*** Password: ssaddfef
*** Stage 1 (Chain and apply xor of "s"): bet
*** Stage 2 (Chain and apply xor of "s"): tbb
*** Stage 3 (Chain and apply xor of "a"): w
*** Stage 4 (Chain and apply xor of "d"): c
*** Stage 5 (Chain and apply xor of "d"): 
*** Stage 6 (Chain and apply xor of "f"): sga
*** Stage 7 (Chain and apply xor of "e"): qc
*** Stage 8 (Chain and apply xor of "f"): td
Final result: td
Attempting decrypt...
*** Data: td
*** Password: ssaddfef
*** Remove stage 8 (Dechain and apply xor of "f"): qc
*** Remove stage 7 (Dechain and apply xor of "e"): sga
*** Remove stage 6 (Dechain and apply xor of "f"): 
*** Remove stage 5 (Dechain and apply xor of "d"): c
*** Remove stage 4 (Dechain and apply xor of "d"): w
*** Remove stage 3 (Dechain and apply xor of "a"): b
*** Remove stage 2 (Dechain and apply xor of "s"): se
*** Remove stage 1 (Dechain and apply xor of "s"): b
Decrypted result: b
---
*** Data: test
*** Password: ssad
*** Stage 1 (Chain and apply xor of "s"): bet
*** Stage 2 (Chain and apply xor of "s"): tbb
*** Stage 3 (Chain and apply xor of "a"): w
*** Stage 4 (Chain and apply xor of "d"): c
Final result: c
Attempting decrypt...
*** Data: c
*** Password: ssad
*** Remove stage 4 (Dechain and apply xor of "d"): w
*** Remove stage 3 (Dechain and apply xor of "a"): b
*** Remove stage 2 (Dechain and apply xor of "s"): se
*** Remove stage 1 (Dechain and apply xor of "s"): b
Decrypted result: b


Any help would be appriciated.


Those who can, cannot. Those who cannot, can.
Kardafol #172018 04/03/07 01:36 AM
Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
I'm not sure if I'm missing how XOR encrypt works.. but it seems like your code is excessively complex. From what I read on google, this is how XOR encryption works:

Code:
data = abcde
pass = xyz

result = <data.1> XOR <pass.1>
result = result + <data.2> XOR <pass.2>
result = result + <data.3> XOR <pass.3>
result = result + <data.4> XOR <pass.1>
result = result + <data.5> XOR <pass.2>

result is 
asc: 25 27 27 28 28


Tell me if I'm wrong here.

To do exactly the above example, here is the code I used:

Code:
alias xor.enc {
  ;$1=data $2=pass
  var %data = $1, %d = 0, %dd = $len(%data), %pass = $2, %pp = $len(%pass), %conv
  while (%d < %dd) {
    inc %d
    %conv = %conv $+ $chr($xor($asc($mid(%data,%d,1)),$asc($mid(%pass,$calc(%d % %pp),1))))
  }
  return %conv
}


//echo -a $xor.enc(<data>,<pass>)

If <data> is unencrpyted data, it will return the encrypted version. If <data> is encrypted, it will return the unecrypted version.

The problem I ran into is when the data is encrypted, the resulting characters are sometimes $chr(13) or $chr(10) which are newline characters and screw up mIRC when you try to decrypt.

-genius_at_work

Joined: Jan 2007
Posts: 259
K
Fjord artisan
OP Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
I'm interlacing a chain, then a letter from the password, then another chain for added security.
So, for
Code:
data = abcde
pass = xyz

; I would get:
result = <data.1> XOR <data.2>
result = result + <data.2> XOR <data.3>
result = result + <data.3> XOR <data.4>
result = result + <data.4> XOR <data.5>
result = result + <data.5> XOR <result.1>
result1 = <result.1> XOR <pass.1>
result1 = result1 + <result.2> XOR <pass.1>
....
result = <result1.1> XOR <result1.2>
...
;Until it has gone through a chain, apply password, chain, apply password...
; untill it has used all the characters in the password.



Those who can, cannot. Those who cannot, can.
Joined: Jan 2007
Posts: 259
K
Fjord artisan
OP Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
Also:
Code:
//echo -a $xor.enc($xor.enc(this string is not screw'd,screw'd),screw'd)
thisstringi~~c2#7ruf0v



Those who can, cannot. Those who cannot, can.
Kardafol #172046 04/03/07 06:40 PM
Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
Ok, I made a portion of the code and I'm already running into the same problems as my last code. mIRC has special meanings for certain ASCII characters. 10 and 13 are newline chars, 0 is null, etc. Unless the data is sent in numerical format (hexcode maybe), the encrypted data can get corrupted during the conversion.

Here is the code I came up with. It is slightly different than the format you described. This version applies a password *before* the first chaining, and before each subsequent chaining. (Pass, chain, pass, chain, ..., pass, chain). I did this because it made the code simpler to write.

Code:
alias xor.enc {
  ;$1=data, $2=pass
  var %ca, %cb = $1
  var %d = 0, %dd = $len(%cb), %da, %db
  var %pass = $2, %p = 0, %pp = $len(%pass), %pb

  ;;; Loop thru all password chars
  ;
  while (%p < %pp) {
    inc %p
    echo -a loop- P. $+ %p

    ;;; Apply current password char
    ;
    %pb = $mid(%pass,%p,1)
    %ca = %cb
    %cb = $null
    %d = 0
    while (%d < %dd) {
      inc %d
      %da = $mid(%ca,%d,1)
      %cb = %cb $+ $chr($xor($asc(%da),$asc(%pb)))

      ;Debug
      var %tmp = $xor($asc(%da),$asc(%pb))
      var %tmp2 = $right($+(000,%tmp),3)
      echo $iif($istok(0 10 13,%tmp,32),4,3) -a   loop- D1. $+ %d  len.cb- $len(%cb)  char.asc- %tmp2  char- > $+ $chr(%tmp) $+ <
    }

    ;;; Chain thru data
    ;
    %ca = %cb
    %cb = $null
    %d = 0
    while (%d < %dd) {
      inc %d
      %da = $mid(%ca,%d,1)
      %db = $mid(%ca,$calc(%d + 1),1)
      if (%d == %dd) %db = $mid(%cb,1,1)
      %cb = %cb $+ $chr($xor($asc(%da),$asc(%db)))

      ;Debug
      var %tmp = $xor($asc(%da),$asc(%db))
      var %tmp2 = $right($+(000,%tmp),3)
      echo $iif($istok(0 10 13,%tmp,32),5,2) -a   loop- D2. $+ %d  len.cb- $len(%cb)  char.asc- %tmp2  char- > $+ $chr(%tmp) $+ <
    }
  }
  echo $iif($len(%cb) != %dd,6,10) -a Output: len.cb- $len(%cb)  cb- > $+ %cb $+ <
}


Here is the sample I used which caused the problems:
//clear | noop $xor.enc(abcde,xyz)

I have included debug lines in the code to show exactly what is happening internally. The debug lines are color coded. GREEN, BLUE, CYAN are good/successful debug lines, RED, MAROON, PURPLE are bad/erroneous debug lines. RED/MAROON means that the converted character is a problem character (0, 10, 13). PURPLE means that the final output isn't the same length as the input data.

In the sample above, you will see a maroon line where the converted character is chr(0) which mIRC removes from strings. Obviously this corrupts the entire encryption. The chr(10) and chr(13) characters won't get corrupted like chr(0), but the two newline characters cannot be sent to another client using mIRC as the transport, thus it is essentially useless.

I'm not sure what can be done about this problem.

-genius_at_work


Joined: Jan 2007
Posts: 259
K
Fjord artisan
OP Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
I'm going to try making it output it as hex, since that might solve the problem.

[edit]
My progress so far:
Code:
; /hexxor string
;
alias a2h {
  var %x = 1, %f = $len($1-), %dat
  while (%x <= %f) var %dat = %dat $+ $base($asc($mid($1-,%x,1)),10,16,2), %x = $calc(%x + 1)
  return %dat
}
alias h2a {
  var %x = 1, %f = $len($1-), %dat
  while (%x <= %f) var %dat = $+($mid(%dat,1,-1),$chr($base($mid($1-,%x,2),16,10)),.), %x = $calc(%x + 2)
  return $mid(%dat,1,-1)
}
alias hexxor {
  echo -a String: $1-
  var %dat = $a2h($1-)
  echo -a Hex String: %dat
  var %dat = $chain.hex(%dat)
  echo -a Chained Hex String: %dat
  var %dat = $dechain.hex(%dat)
  echo -a Dechained Hex String: %dat
  var %dat = $h2a(%dat)
  echo -a String: %dat
}
alias n2h return $base($1,10,16,2)
alias h2n return $base($1,16,10)
alias chain.hex {
  var %x = 1, %l = $len($1-), %dat
  while %x <= %l {
    var %xor1 = $h2n($mid($1-,%x,2))
    if (%x == $calc(%l -1)) var %xor2 = $h2n($mid(%dat,1,2))
    else var %xor2 = $h2n($mid($1-,$calc(%x + 2),2))
    var %dat = $+(%dat,$n2h($xor(%xor1,%xor2))), %x = $calc(%x + 2)
  }
  return %dat
}
alias dechain.hex {
  var %x = $len($1), %l = $calc(%x -1), %dat
  while %x > 0 {
    var %x = $calc(%x - 1), %xor1 = $h2n($mid($1,%x,2))
    if (%x == %l) var %xor2 = $h2n($mid($1,1,2))
    else var %xor2 = $h2n($mid(%dat,1,2))
    var %dat = $+($n2h($xor(%xor1,%xor2)),%dat), %x = $calc(%x - 1)
  }
  return %dat
}

Last edited by Kardafol; 04/03/07 11:42 PM.

Those who can, cannot. Those who cannot, can.
Kardafol #172145 06/03/07 04:36 AM
Joined: Oct 2005
Posts: 1,741
G
Hoopy frood
Offline
Hoopy frood
G
Joined: Oct 2005
Posts: 1,741
Here is my code for XOR encrypt to HEX output.

Code:
alias xor.debug return 0
alias xor.ch return $regsubex($1,/(.)/g,$base($asc(\1),10,16,2))
alias xor.hc return $regsubex($1,/(..)/g,$chr($base(\1,16,10)))
alias xor.dh return $base($1,10,16,2)
alias xor.hd return $base($1,16,10)


alias xor.enc {
  ;$1=data, $2=pass
  var %ca, %cb = $xor.ch($left($1,400))
  var %d = 0, %dd = $len(%cb), %da, %db
  var %pass = $xor.ch($2), %p = 1, %pp = $len(%pass), %pb

  if ($xor.debug) echo 10 -a Input: len.cb- %dd  cb- > $+ %cb $+ < $xor.hc(%cb)

  ;;; Loop thru all password chars
  ;
  while (%p < %pp) {
    if ($xor.debug) echo -a loop- P. $+ %p

    if (1) {
      ;;; Apply current password char
      ;
      %pb = $mid(%pass,%p,2)
      if ($xor.debug) echo -a PB- %pb
      %ca = %cb
      %cb = $null
      %d = 1
      while (%d < %dd) {
        %da = $mid(%ca,%d,2)
        if ($xor.debug) echo -a DA- %da PB- %pb
        %cb = %cb $+ $xor.dh($xor($xor.hd(%da),$xor.dh(%pb)))

        ;Debug
        if ($xor.debug) var %tmp = $xor.dh($xor($xor.hd(%da),$xor.dh(%pb)))
        if ($xor.debug) echo 3 -a   loop- D1. $+ %d  len.cb- $len(%cb)  char.asc- %tmp  char- > $+ $xor.hc(%tmp) $+ <
        ;/Debug

        inc %d 2
      }

      if ($xor.debug) echo -a %cb
    }

    if (1) {
      if ($len(%cb) > 2) {
        ;;; Chain thru data
        ;
        %ca = %cb
        %cb = $null
        %d = 1
        while (%d < %dd) {
          %da = $mid(%ca,%d,2)
          %db = $mid(%ca,$calc(%d + 2),2)
          if (%d == $calc(%dd - 1)) %db = $mid(%cb,1,2)
          if ($xor.debug) echo -a DA- %da DB- %db DX- $xor.dh($xor($xor.hd(%da),$xor.hd(%db))) DD- %dd D- %d
          %cb = %cb $+ $xor.dh($xor($xor.hd(%da),$xor.hd(%db)))

          ;Debug
          if ($xor.debug) var %tmp = $xor.dh($xor($xor.hd(%da),$xor.hd(%db)))
          if ($xor.debug) echo 2 -a   loop- D2. $+ %d  len.cb- $len(%cb)  char.asc- %tmp  char- > $+ $xor.hc(%tmp) $+ <
          ;/Debug

          inc %d 2    
        }

        if ($xor.debug) echo -a %cb
      }
    }

    inc %p 2
  }
  if ($xor.debug) echo 10 -a Output: len.cb- $len(%cb)  cb- > $+ %cb $+ <

  return %cb
}



alias xor.dec {
  ;$1=data, $2=pass
  var %ca, %cb = $1
  var %d = $calc($len(%cb) - 1), %dd = %d, %da, %db
  var %pass = $xor.ch($2), %p = $calc($len(%pass) - 1), %pb

  if ($xor.debug) echo 10 -a Input: len.cb- $calc(%dd + 1)  cb- > $+ %cb $+ <

  ;;; Loop thru all password chars
  ;
  while (%p > 0) {
    if ($xor.debug) echo -a loop- P. $+ %p


    if (1) {
      if ($len(%cb) > 2) {
        ;;; Chain thru data
        ;
        %ca = %cb
        %cb = $null
        %d = %dd
        while (%d > 0) {
          %da = $mid(%ca,%d,2)
          %db = $mid(%cb,1,2)
          if (%d == %dd) %db = $mid(%ca,1,2)
          if ($xor.debug) echo -a DA- %da DB- %db DX- $xor.dh($xor($xor.hd(%da),$xor.hd(%db))) DD- %dd D- %d
          %cb = $xor.dh($xor($xor.hd(%da),$xor.hd(%db))) $+ %cb

          ;Debug
          if ($xor.debug) var %tmp = $xor.dh($xor($xor.hd(%da),$xor.hd(%db)))
          if ($xor.debug) echo 2 -a   loop- D2. $+ %d  len.cb- $len(%cb)  char.asc- %tmp  char- > $+ $xor.hc(%tmp) $+ <
          ;/Debug

          dec %d 2    
        }

        if ($xor.debug) echo -a %cb
      }
    }


    if (1) {
      ;;; Apply current password char
      ;
      %pb = $mid(%pass,%p,2)
      if ($xor.debug) echo -a PB- %pb
      %ca = %cb
      %cb = $null
      %d = %dd
      while (%d > 0) {
        %da = $mid(%ca,%d,2)
        if ($xor.debug) echo -a DA- %da PB- %pb
        %cb = $xor.dh($xor($xor.hd(%da),$xor.dh(%pb))) $+ %cb

        ;Debug
        if ($xor.debug) var %tmp = $xor.dh($xor($xor.hd(%da),$xor.dh(%pb)))
        if ($xor.debug) echo 3 -a   loop- D1. $+ %d  len.cb- $len(%cb)  char.asc- %tmp  char- > $+ $xor.hc(%tmp) $+ <
        ;/Debug

        dec %d 2
      }

      if ($xor.debug) echo -a %cb
    }

    dec %p 2
  }
  if ($xor.debug) echo 10 -a Output: len.cb- $len(%cb)  cb- > $+ %cb $+ < $xor.hc(%cb)
  return $xor.hc(%cb)
}


Usage: $xor.enc(plain text,password)
Usage: $xor.dec(encrypted text,password)

Let me know if you find any bugs.

-genius_at_work

Joined: Jan 2007
Posts: 259
K
Fjord artisan
OP Offline
Fjord artisan
K
Joined: Jan 2007
Posts: 259
My code:
Code:
alias a2h {
  var %x = 1, %f = $len($1-), %dat
  while (%x <= %f) var %dat = %dat $+ $base($asc($mid($1-,%x,1)),10,16,2), %x = $calc(%x + 1)
  return %dat
}
alias h2a {
  var %x = 1, %f = $len($1-), %dat
  while (%x <= %f) var %dat = $+($mid(%dat,1,-1),$chr($base($mid($1-,%x,2),16,10)),.), %x = $calc(%x + 2)
  return $mid(%dat,1,-1)
}
alias n2h return $base($1,10,16,2)
alias h2n return $base($1,16,10)
alias chain.hex {
  var %x = 1, %l = $len($1-), %l2 = $calc(%l -1), %dat
  while (%x <= %l) var %dat = $+(%dat,$n2h($xor($h2n($mid($1-,%x,2)),$iif(%x == %l2,$h2n($mid(%dat,1,2)),$h2n($mid($1-,$calc(%x + 2),2)))))), %x = $calc(%x + 2)
  return %dat
}
alias dechain.hex {
  var %x = $len($1), %l = $calc(%x -1), %dat
  while (%x > 0) var %x = $calc(%x - 1), %dat = $+($n2h($xor($h2n($mid($1,%x,2)),$iif(%x == %l,$h2n($mid($1,1,2)),$h2n($mid(%dat,1,2))))),%dat), %x = $calc(%x - 1)
  return %dat
}
alias xor.hex {
  var %data = $1, %d = 1, %dd = $len(%data), %pass = $2, %pp = $len(%pass), %conv
  while (%d < %dd) var %conv = $+(%conv,$n2h($xor($h2n($mid(%data,%d,2)),$h2n($mid(%pass,$calc(%d % %pp),2))))), %d = $calc(%d + 2) 
  return %conv
}
alias crypt.hex {
  if (!$prop) || ($prop == ec) {
    var %dat = $a2h($1), %pass = $2, %pl = $numtok(%pass,58)
    ; Password in format: XXXXXXXX:XXXXXXXX:XXXXXXXX...
    while (%pl) var %tp = $gettok(%pass,%pl,58), %dat = $xor.hex($chain.hex(%dat),%tp), %pl = $calc(%pl - 1)
    var %ff = $calc($len(%dat) /2)
    while (%ff > 0) var %dat = $chain.hex(%dat), %ff = $calc(%ff - 1)
    return %dat
  }
  if $prop = dc {
    var %dat = $1, %pass = $2, %pl = $numtok(%pass,58), %x = 0
    var %ff = $calc($len(%dat) /2)
    while (%ff > 0) var %dat = $dechain.hex(%dat), %ff = $calc(%ff - 1)
    while %x < %pl { inc %x
      var %tp = $gettok(%pass,%x,58), %dat = $dechain.hex($xor.hex(%dat,%tp))
    }
    return $h2a(%dat)
  }
}
alias rpass {
  if ($1) {
    while ($calc($1 % 4)) tokenize 32 $calc($1 + 1)
  }
  var %len = $iif($1,$1,64), %x = $calc(%len / 4), %pwd
  while (%x) var %pwd = $+($iif(%pwd,$+(%pwd,:)),$rhex(4,4)), %x = $calc(%x - 1)
  return %pwd
}
alias rhex {
  var %l = $rand($1,$2)
  while (%l) var %ret = $+(%ret,$base($rand(0,255),10,16,2))), %l = $calc(%l -1)
  return %ret
}
alias rasc {
  var %l = $rand($1,$2)
  while (%l) var %ret = $+(%ret,$chr($rand(1,255))), %l = $calc(%l -1)
  return %ret
}
on *:text:?HX*:*: {
  var %msg = $crypt.hex($mid($1-,4),%hpass).dc, %msg1 = $mid(%msg,$len($+($gettok(%msg,1,58),::)),$gettok(%msg,1,58))
  echo $iif($chan,$chan,$nick) $+([,$nick,]) %msg1
}
alias hex {
  msg $active ?HX $+ $crypt.hex($+($calc($len($1-)),:,$1-,$rasc(1,5)),%hpass)
}

However, I'm still tweaking it for speed, and I'm going to remove some of the aliases and replace them.


Those who can, cannot. Those who cannot, can.

Link Copied to Clipboard