I'm looking forward to a little vodka myself.

I was looking to implement RSA encryption via Kix. Started toying around with this. RSA


Anyhow, I found a VBScript that does RC4 and I converted it. Problem is, Kix doesn't gracefully handle chr(0).

Code:
;endecrypt.k2k 
; ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; '::: :::
; '::: This script performs 'RC4' Stream Encryption :::
; '::: (Based on what is widely thought to be RSA's RC4 :::
; '::: algorithm. It produces output streams that are identical :::
; '::: to the commercial products) :::
; '::: :::
; '::: This script is Copyright © 1999 by Mike Shaffer :::
; '::: ALL RIGHTS RESERVED WORLDWIDE :::
; '::: :::
; ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
; ':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;

$string="jim"
$pw="joe"

;EnDeCrypt($string,$pw)
EnDeCrypt(EnDeCrypt($string,$pw),$pw)


Function EnDeCrypt($plaintxt, $psw)
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
;::: This routine does all the work. Call it both to ENcrypt :::
;::: and to DEcrypt your data. :::
;:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

dim $temp
dim $a
dim $i
dim $j
dim $k
dim $cipherby
dim $cipher

dim $tempSwap
dim $a
dim $b

Dim $sbox[255]
Dim $key[255]

$i = 0
$j = 0

$strPwd=$Psw
$intLength = len($strPwd)
For $a = 0 To 255
$key[$a] = asc(Substr($strpwd, ($a mod $intLength)+1, 1))
$sbox[$a] = $a
next

$b = 0
For $a = 0 To 255
$b = ($b + $sbox[$a] + $key[$a]) Mod 256
$tempSwap = $sbox[$a]
$sbox[$a] = $sbox[$b]
$sbox[$b] = $tempSwap
;? "A: "+$sbox[$a]
Next

For $a = 1 To Len($plaintxt)
;? "COUNT "$a
$i = ($i + 1) Mod 256
$j = ($j + $sbox[$i]) Mod 256
$temp = $sbox[$i]
$sbox[$i] = $sbox[$j]
$sbox[$j] = $temp

;? "WTF "+($sbox[$i] + $sbox[$j]) Mod 256
$k = $sbox[($sbox[$i] + $sbox[$j]) Mod 256]
;? "K "+$K
;$cipherby = Asc(substr($plaintxt, $a, 1)) Xor $k
;? "ASC "Asc(substr($plaintxt, $a, 1))
$cipherby = XOr(Asc(substr($plaintxt, $a, 1)),$k)
;? "CI "+$Cipherby

$cipher = $cipher + Chr($cipherby)

? "PRODUCT "+$Cipher
Next

$EnDeCrypt = $cipher
?
EndFunction


function XOR($ExpN1,$ExpN2)
? "XORING1: "$ExpN1
? "XORING2: "$ExpN2
; performs an exclusive or, given two numbers.
$ExpN1=0+$ExpN1
$ExpN2=0+$ExpN2
$XOR=($ExpN1 | $ExpN2)-($ExpN1 & $ExpN2)
? "X: "+$XOR
endfunction



Here's the VBS which works fine.
Code:

'endecrypt.vbs
dim h
Dim sbox(255)
Dim key(255)

wscript.echo EnDeCrypt("jim", "joe")

wscript.echo EnDeCrypt(EnDeCrypt("jim", "joe"), "joe")

Sub RC4Initialize(strPwd)
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: This routine called by EnDeCrypt function. Initializes the :::
'::: sbox and the key array) :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

dim tempSwap
dim a
dim b

intLength = len(strPwd)
For a = 0 To 255
key(a) = asc(mid(strpwd, (a mod intLength)+1, 1))
sbox(a) = a
next

b = 0
For a = 0 To 255
b = (b + sbox(a) + key(a)) Mod 256
tempSwap = sbox(a)
sbox(a) = sbox(b)
sbox(b) = tempSwap
Next

End Sub

Function EnDeCrypt(plaintxt, psw)
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'::: This routine does all the work. Call it both to ENcrypt :::
'::: and to DEcrypt your data. :::
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

dim temp
dim a
dim i
dim j
dim k
dim cipherby
dim cipher

i = 0
j = 0

RC4Initialize psw

For a = 1 To Len(plaintxt)
i = (i + 1) Mod 256
j = (j + sbox(i)) Mod 256
temp = sbox(i)
sbox(i) = sbox(j)
sbox(j) = temp

k = sbox((sbox(i) + sbox(j)) Mod 256)

cipherby = Asc(Mid(plaintxt, a, 1)) Xor k
cipher = cipher & Chr(cipherby)
Next

EnDeCrypt = cipher

End Function

_________________________
-Jim

...the sort of general malaise that only the genius possess and the insane lament.