Jump to content

StringReplace Func Returns 0...


layer
 Share

Recommended Posts

Well, I'm making an Encrypter/Decrypter based on Pythons 1337 7r4/\/51470r and for some reason when i made my Func Encrypt ($text) and I try to encrypt soemthing, it returns 0... Here's the code

#include <GUIConstants.au3>
GUICreate ("AutoEncrypt", 400, 300)
GUISetState (@SW_SHOW)
$input= GUICtrlCreateInput ("", 10, 50)
$outcome= GUICtrlCreateInput ("", -1, 100)
GUICtrlCreateLabel ("Type what you want Encrypted or Decrypted:", 10, 20)
GUICtrlCreateLabel ("Results:", -1, 75)
$e= GUICtrlCreateButton ("Encrypt", 10, 150)
$d= GUICtrlCreateButton ("Decrypt", 10, 200)
While 1
   $get= GUIGetMsg ()
Select 
Case $get= $GUI_EVENT_CLOSE 
   ExitLoop
Case $get= $e
  $encrypted= Encrypt (GUIRead($input))
  GUICtrlSetData ($outcome, $encrypted, 1) 
EndSelect
WEnd
   Func Encrypt ($en)
      $a= StringReplace ($en, "a", "z")
      $b= StringReplace ($a, "b", "a")
      $c= StringReplace ($b, "c", "b")
      $d= StringReplace ($c, "d", "c")
      $e= StringReplace ($d, "e", "d")
      $f= StringReplace ($e, "f", "e")
      $g= StringReplace ($f, "g", "f")
      $h= StringReplace ($g, "h", "g")
      $i= StringReplace ($h, "i", "h")
      $j= StringReplace ($i, "j", "i")
      $k= StringReplace ($j, "k", "j")
      $l= StringReplace ($k, "l", "k")
      $m= StringReplace ($l, "m", "l")
      $n= StringReplace ($m, "n", "m")
      $o= StringReplace ($n, "o", "n")
      $p= StringReplace ($o, "p", "o")
      $q= StringReplace ($p, "q", "p")
      $r= StringReplace ($q, "r", "q")
      $s= StringReplace ($r, "s", "r")
      $t= StringReplace ($s, "t", "s")
      $u= StringReplace ($t, "u", "t")
      $v= StringReplace ($u, "v", "u")
      $w= StringReplace ($v, "w", "v")
      $x= StringReplace ($w, "x", "w")
      $y= StringReplace ($x, "y", "x")
      $z= StringReplace ($y, "z", "y")
   EndFunc

Could someone point me in the right direction? I didn't make the decryption func yet because I want to make the encryption one work first and also the Decryption func is going to be a little hard =/

Thanks

FootbaG
Link to comment
Share on other sites

There is no return value, so it returns 0.

If you want the modification applied to the $en variant directly you have to use:

Func Encrypt (ByRef $en)

So calling Encrypt ($Test) will change test.

Or if you want a return value you should add a Return $variant name.

I am not sure why you made so many different variant, but still my message apply in general.

Link to comment
Share on other sites

Ok, this is what I did, I did the ByRef thingy, and I put Return $en at the bottom of the encrypt function, like this...

#include <GUIConstants.au3>
GUICreate ("AutoEncrypt", 400, 300)
GUISetState (@SW_SHOW)
$input= GUICtrlCreateInput ("", 10, 50)
$outcome= GUICtrlCreateInput ("", -1, 100)
GUICtrlCreateLabel ("Type what you want Encrypted or Decrypted:", 10, 20)
GUICtrlCreateLabel ("Results:", -1, 75)
$e= GUICtrlCreateButton ("Encrypt", 10, 150)
$d= GUICtrlCreateButton ("Decrypt", 10, 200)
While 1
   $get= GUIGetMsg ()
Select 
Case $get= $GUI_EVENT_CLOSE 
   ExitLoop
Case $get= $e
  $encrypted= Encrypt (GUIRead($input))
  GUICtrlSetData ($outcome, $encrypted, 1) 
EndSelect
WEnd
   Func Encrypt (ByRef $en)
      $a= StringReplace ($en, "a", "z")
      $b= StringReplace ($a, "b", "a")
      $c= StringReplace ($b, "c", "b")
      $d= StringReplace ($c, "d", "c")
      $e= StringReplace ($d, "e", "d")
      $f= StringReplace ($e, "f", "e")
      $g= StringReplace ($f, "g", "f")
      $h= StringReplace ($g, "h", "g")
      $i= StringReplace ($h, "i", "h")
      $j= StringReplace ($i, "j", "i")
      $k= StringReplace ($j, "k", "j")
      $l= StringReplace ($k, "l", "k")
      $m= StringReplace ($l, "m", "l")
      $n= StringReplace ($m, "n", "m")
      $o= StringReplace ($n, "o", "n")
      $p= StringReplace ($o, "p", "o")
      $q= StringReplace ($p, "q", "p")
      $r= StringReplace ($q, "r", "q")
      $s= StringReplace ($r, "s", "r")
      $t= StringReplace ($s, "t", "s")
      $u= StringReplace ($t, "u", "t")
      $v= StringReplace ($u, "v", "u")
      $w= StringReplace ($v, "w", "v")
      $x= StringReplace ($w, "x", "w")
      $y= StringReplace ($x, "y", "x")
      $z= StringReplace ($y, "z", "y")
   Return $en
   EndFunc

Now I get an error when I try encrypting some text...

[error]

Line 41

$encrypted= Encrypt (GUIRead($input))

$encrypted= Encrypt (^ ERROR

Error: Expected a variable in user function call.

[/error]

Now I think this may have something with the GUIRead coming in the Encrypt function, no?

Anyy suggestions?

Thanks.

EDIT: I changed return $en to Return (Encrypt (GUIRead($input)) but i still get the error... :)

Edited by layer
FootbaG
Link to comment
Share on other sites

Piece of advice (Which you won't take again): Throw away that garbage script from Python and start from scratch using something by Larry or somebody else who actually has a clue (Or one of the later Python scripts after he fixed the stupid variable... stupidity that was going on, as is with yours). Also, study ByRef in the help file and Return as well.

Link to comment
Share on other sites

Won't you read my post again and stopping being a whiney bitch about it. I stated that the code is poor and that you need a better understanding of both ByRef and Return. Now I encourage any experienced veteran such as Larry, this-is-me, CyberSlug, JdeB or many others to disagree with my assessment of the things you need to focus on to get this to work correctly.

Edit: And if you wonder why the code is bad, you have unnecessary complexity by using all those different variables. You are also causing 26x the memory to be used than what is necessary.

Edited by Valik
Link to comment
Share on other sites

Again, would someone else please help me with this, I will study ByRef and Return a little more, but I don't want your advice anymore Valik.

Thanks

EDIT: I take that back, so I would use StringSplit?

Edited by layer
FootbaG
Link to comment
Share on other sites

  • Administrators

The ByRef thing in the function declaration tells AutoIt that it should expect a variable so that it can change the result. So it is expecting something like:

$myvar = GUIRead($input)
$encrypted= Encrypt ($myvar)

rather than:

$encrypted= Encrypt (GUIRead($input))

It's a little hard to explain but run these 3 scripts and see if you can work out what is happening (or ask). I can't remember how well these are documented.

$a = 1
Test($a)
MsgBox(0, "", $a)

Func Test($myvar)
  $myvar = 1000
EndFunc

$a = 1
Test($a)
MsgBox(0, "", $a)

Func Test(ByRef $myvar)
  $myvar = 1000
EndFunc

$a = 1
$a = Test()
MsgBox(0, "", $a)

Func Test()
  return 1000
EndFunc

Edit: Like Valik says there are better ways to do the script you posted, but this is just ByRef/Return advice.

Edited by Jon
Link to comment
Share on other sites

So, I did a little research in the help file... And instead of that whole big blob of code, you could do this. I hope this is right:

$replace= StringSplit ("z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, ",")
$keys= StringSplit ("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",") for
$replace= StringReplace ($input, $keys, $replace)

Thanks

<b>EDIT:</b> My bad:

$replace= StringSplit ("z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
$keys= StringSplit ("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
$do= StringReplace ($input, $keys, $replace)
Edited by layer
FootbaG
Link to comment
Share on other sites

Ok, now whats wrong? Because now it's returning nothing, it just leaves the field blank, I know it has something to do with all that ByRef stuff, no?

#include <GUIConstants.au3>
GUICreate ("AutoEncrypt", 400, 300)
GUISetState (@SW_SHOW)
$input= GUICtrlCreateInput ("", 10, 50)
$outcome= GUICtrlCreateInput ("", -1, 100)
GUICtrlCreateLabel ("Type what you want Encrypted or Decrypted:", 10, 20)
GUICtrlCreateLabel ("Results:", -1, 75)
$e= GUICtrlCreateButton ("Encrypt", 10, 150)
$d= GUICtrlCreateButton ("Decrypt", 10, 200)
While 1
   $get= GUIGetMsg ()
     $myen= GUIRead($input)
  $encrypted= Encrypt ($myen)
Select 
Case $get= $GUI_EVENT_CLOSE 
   ExitLoop
Case $get= $e
 GUICtrlSetData ($outcome, $encrypted, 1)
EndSelect
WEnd
   Func Encrypt (ByRef $myen)
$replace= StringSplit ("z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
$keys= StringSplit ("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
$do= StringReplace ($input, $keys, $replace)
return $encrypted
EndFunc

Any suggestions?(Valik, you can too suggest =))

Thanks

EDIT: Hmmmm, I just realized, $myen is sitting there doing nothing...What should I put after ByRef then?

Edited by layer
FootbaG
Link to comment
Share on other sites

I am afraid I would have to go with Val on this, although I wouldn't have stated it in his clear but Valik way. :)

First besides the way the code is written, it has all sorts of flaws. Take the word Kazza.

you change all the a to z so it becomes Kzzzz then you change the K to J so it becomes jzzzz then you change z to a so it is jaaaa. Now decrypt it. See my first point?

Better to start over with Stringsplit. And set a to @ and after z is changed, set it to z. There are many slow and memory things as well, like 27 variables instead of 1.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

  • Developers

You are not looping throught the array... Like:

Func Encrypt ($en)
   $replace= StringSplit ("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
   $keys= StringSplit ("b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,a", ",")
   For $x = 1 to $keys[0]
      $en = StringReplace($en, $keys[$x], $replace[$x])
   Next
   Return $en
EndFunc

BUT you have a logic error ....

When the input contains an "b" it will be replace in the gebinning by an 'a' and at the end by a 'z' .... so you need to rethink your logic.... EDIT:Like Larry stated.

Edited by JdeB

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

ok, try your code in the simple way first before putting it into a gui:

not going to work.

$y="Kazza"
$x=Encrypt($y)
msgbox(1,$x,$y)

Func Encrypt (ByRef $myen)
$replace= StringSplit ("z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
$keys= StringSplit ("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
$do= StringReplace ($myen, $keys, $replace)
return $do
EndFunc

Not much better, but:

$y="Kazza"
$x=Encrypt($y)
msgbox(1,$x,$y)

Func Encrypt (ByRef $myen)
$replace= StringSplit ("&,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
$keys= StringSplit ("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,&", ",")
for $i=1 to 27
$myen= StringReplace ($myen, $keys[$i], $replace[$i])
next
EndFunc

edit.. oh yea, if you do ByRef, it changes the variable dynamically, so you don't need to return a value.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

I tried to repair the script and came to conclusion that StringSplit, StringReplace doesn't work.

Mr. Wizard made the perfect encrypt/decrypt UDF the other day.

Here's the result:

#include <GUIConstants.au3>

GUICreate ("AutoEncrypt", 400, 300)

$input = GUICtrlCreateInput ("", 10, 50)
$outcome= GUICtrlCreateInput ("", -1, 100)

GUICtrlCreateLabel ("Type what you want Encrypted or Decrypted:", 10, 20)
GUICtrlCreateLabel ("Results:", -1, 75)

$enc_btn = GUICtrlCreateButton ("Encrypt", 10, 150)
$decr_btn = GUICtrlCreateButton ("Decrypt", 10, 200)

GUISetState (@SW_SHOW)

While 1
   $get = GUIGetMsg ()
   Select
      Case $get = $GUI_EVENT_CLOSE
         ExitLoop
         
      Case $get = $enc_btn
         $myen = GUIRead($input)
         $encrypted = EncDec($myen)
         GUICtrlSetData ($outcome, $encrypted)
      
      Case $get = $decr_btn
         $myen = GUIRead($outcome)
         $decrypted = EncDec($myen)
         GUICtrlSetData ($input, $decrypted)
         
   EndSelect
WEnd

Func EncDec($original)
  ;Original author: "Mr.Wizard"
    dim $modified
    dim $key = "I am the magic key"
    dim $pos_original, $pos_key
    
    For $pos_original = 1 To StringLen($original)
        If $pos_key = StringLen($key) Then
            $pos_key = 1
        Else
            $pos_key = $pos_key + 1
        EndIf

        $modified = $modified & Chr(BitXOR(Asc(StringMid($original, $pos_original, 1)), Asc(StringMid($key, $pos_key, 1)), 255))
    Next
        
    Return $modified
EndFunc
Edited by SlimShady
Link to comment
Share on other sites

did you try my repair?

$y="Kazza"
$original=$y
Encrypt($y)
msgbox(1,"Encrypted " & $original,$y)
decrypt($y)
msgbox(1,"Decrypted " & $original,$y)

Func Encrypt (ByRef $myen)
$replace= StringSplit ("&,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
$keys= StringSplit    ("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,&", ",")
for $i=1 to 27
$myen= StringReplace ($myen, $keys[$i], $replace[$i])
next
EndFunc

Func DEcrypt (ByRef $myen)
$replace= StringSplit ("&,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
$keys= StringSplit    ("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,&", ",")
for $i= 27 to 1 step -1
$myen= StringReplace ($myen, $replace[$i], $keys[$i])
next
EndFunc

Not that I like how it works, but it does work.

AutoIt3, the MACGYVER Pocket Knife for computers.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...