Dear all,There have been moments when i needed to store a user's password or the one of a special "service / su" account in registry or files. That way, you could make that account log on automatically (e.g. to install Office2000 with elevated rights). I don't like storing a plain-text password in registry (even with security set on the key), as it is not exactly what i would call "secure"
So, I made an algorythm to "encrypt" those passwords. The algorythm is not too complicated: it returns a string inverted, converts it to ANSI char code and strips the separators.
e.g:
My "PaSsW)oRD" after Encrypt becomes
6882111è4187115è83978040121è77
(For stronger encryption, use 2/3 pass encryption)
Syntax:
Encrypt(ItemToEncrypt,Number of codepasses)
Decrypt(ItemToDecrypt,Number of codepasses)
;-----------------------------------Start-----------
Break ON
$o=SetConsole("AlwaysOnTop")
$o=SetAscii("ON")
Cls
"Enter a string to en- and decode:>"
Gets $String
"Enter the number of passes to encrypt the code:>"
Get $Passes
$RES=Encrypt($String,$Passes)
$p=Decrypt($res,$Passes)
?
?"The encrypted Code ="
?
?"$Res"
?
?"And the unencrypted result ="
?
?"$p"
?
?
;Encoder******************************************************************************
FUNCTION Encrypt($Source,$pass)
FOR $t=1 TO $pass STEP 1
$l=Len($Source)
Dim $Cont[$l]
Dim $i,$j
Redim $Inv[$l]
$Enc=""
$k=""
GLOBAL $Enc
?"lenght of String = $l"
?
$i=0 $j=0
FOR $i=0 TO $l-1 STEP 1
$Cont[$i]=Substr($Source,$l-$i,1)
?"Array this far = "+ $Cont[$i]
NEXT
FOR EACH $Cell IN $Cont
$k="$k" + "$Cell"
GLOBAL $Inv[$l]
$Enl=Asc($Cell)
IF $j < $l
SELECT
CASE
Len($Enl)=3
$Inv[$j]="$Enl" +"!"
CASE 1
$Inv[$j]="$Enl"
ENDSELECT
$j=$j+1
ENDIF
NEXT
?
?"The new inverted String = $k"
?
FOR EACH $El IN $Inv
$Enc="$Enc" + "$El"
?"Encrypted Array so far = $Enc"
NEXT
?
?"The Encrypted String = $Enc"
$Source=$Enc
NEXT
$Encrypt=$Enc
? "Show $Encrypt as the result"
ENDFUNCTION
;Decoder******************************************************************************
FUNCTION Decrypt($Des,$Pass)
FOR $t=1 TO $Pass STEP 1
?"Decrypt gets this input: $des, $pass"
GLOBAL $Spl
GLOBAL $SpltD
$Dec=""
$SpltD=""
$Spl=Split($Des,"!")
FOR EACH $Eld IN $Spl
$LngthE=Len($Eld)
?
?"Length of the encrypted Array Element= $LngthE"
$OddOrEven=$LngthE/2 ;Would like to see the INT change in FLOAT, but in KIX "/" always returns INT
If $OddOrEven*2 <> $LngthE ;Hence this trick
$OddorEven=1,2
Endif
$ret=VarType($OddOrEven)
SELECT
CASE VarType($OddOrEven)=3 ;An even element - Read in groups of 2
FOR $i=0 TO $LngthE-1 STEP 2
$Eldf=Substr($Eld,$i+1,2)
$EldFD=CHR($Eldf)
$SpltD="$Spltd" + "$EldfD"
? "Unencrypted cell = $EldfD"
NEXT
CASE 1
FOR $i=0 TO $LngthE-4 STEP 2 ;An odd element -Read in groups of 2 until last 3
$Eldf=Substr($Eld,$i+1,2)
$EldFD=CHR($Eldf)
? "Unencrypted cell = $EldFD"
$SpltD="$SpltD" + "$EldFD"
NEXT
? "The encrypted String this far $SpltD"
$EldLastCell=Substr($Eld,$LngthE-2,3)
$EldLastCell=CHR("$EldLastCell")
? "The last Cell of 3: $EldLastCell"
$SpltD="$SpltD" + "$EldLastCell"
ENDSELECT
NEXT
?
? "The inverted, decoded String = $Spltd, Now inverting..."
?
$l=Len($Spltd)
Dim $DCont[$l]
Dim $i,$j
$i=0 $j=0
FOR $i=0 TO $l-1 STEP 1
$DCont[$i]=Substr($Spltd,$l-$i,1)
?"Array this far = "+ $DCont[$i]
NEXT
FOR EACH $Cell IN $DCont
$Dec="$Dec" + "$Cell"
NEXT
$Des=$Dec
NEXT
$Decrypt="$Dec"
ENDFUNCTION
;------------------------end------------------
greetz,
Grrrippp!
(Stefaan Degroote)