Sign in to follow this  
Followers 0
layer

StringReplace Func Returns 0...

31 posts in this topic

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Wait, im confused, could you explain it a little more please?

Thanks

BTW: I think you should make that Rot127 thingy you were talking about, that would be neat =)


FootbaG

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

You have to explicitly Return(what you want to have instead of 0) in your function.

Edited by this-is-me

Who else would I be?

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

How about you let me code the way I want and stop flaming me for it?


FootbaG

Share this post


Link to post
Share on other sites

Badly? You want to code badly? Well, to each their own, I guess.

Share this post


Link to post
Share on other sites

Can someone else give me some pointers? This is all i wanted, was a little help, and I get flamed...


FootbaG

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

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

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

btw, make sure you step backwards on the decrypt, otherwise you change all the A's to B's, then B's to C's, and by the end every letter is Z


AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
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
Sign in to follow this  
Followers 0