Page 1 of 1 1
Topic Options
#126840 - 2004-09-17 09:07 PM Large Number Math
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
I know, I know... Ask a dumb question...

Well, this just pisses me off. Mod should return 855. Any suggestions/workarounds?

Code:

Break On
$nul=SetOption("WrapAtEOL","On")
en()

Function En()
Dim $v
$v=Exp(123,17)
? "Value1: "+$v
? $v mod 3233
EndFunction

function Exp($base,$power)
dim $sub
$base=0+$base
$power=0+$power
$Exp=cdbl(1)
:exploop
if $power>0
$Exp=$Exp*$base
$power=$power-1
goto exploop
endif
endfunction

_________________________
-Jim

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

Top
#126841 - 2004-09-17 09:30 PM Re: Large Number Math
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
your code is kinda complex to follow with vodka in blood but lets say that you do integer calculations in your exp function.
that's no good for large numbers.

anyway, go ahead and tell us a task:
-what to input
-what output should be

and we can direct you.
ofcourse some dummie (like me) can give you code directly but mainly we probably give you the way to code.


anyway, this is not a dummy question, for sure.
these math Q's are nice as we don't hear them much.

thanks for asking.
_________________________
!

download KiXnet

Top
#126842 - 2004-09-17 11:07 PM Re: Large Number Math
jtokach Offline
Seasoned Scripter
*****

Registered: 2001-11-15
Posts: 513
Loc: PA, USA
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.

Top
#126843 - 2004-09-17 11:57 PM Re: Large Number Math
ShaneEP Moderator Offline
MM club member
*****

Registered: 2002-11-29
Posts: 2127
Loc: Tulsa, OK
mmmmm vodka .... dang where the smiley emoticon with the drool hanging out of the mouth when you need it.

oh sorry...back on topic

Top
#126844 - 2004-09-18 12:27 PM Re: Large Number Math
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
ja, chr(0) is a problem, which forces you to play with something else than pure kixtart...
_________________________
!

download KiXnet

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 601 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.057 seconds in which 0.028 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org