Sign in to follow this  
Followers 0
GoogleDude

Simple Password Script

6 posts in this topic

#1 ·  Posted (edited)

A Simple Script I thought I would share that can be used to require a password to access/run a script/function! My 30 secs. in sharing some of what I have learned with others that might want to know how to password a script. You could even use in conjunction with Obfuscator for that extra "psudo encryption".

#NoTrayIcon
;InputBox features: Title=Yes, Prompt=Yes, Default Text=No, Pwd Char=*123, Mandatory
$password = "Password001"; Password needed to access script!
If Not IsDeclared("InputBoxAnswer") Then Local $InputBoxAnswer
$InputBoxAnswer = InputBox("Password Prompt!","Please type in a password to access this program!","","*M","-1","-1","-1","-1")
Select
    Case @Error = 0;OK - The string returned is valid
        If $InputBoxAnswer = $password Then
            MsgBox(1024,"Password was correct!","You typed the correct Password. You may access this file!"); What to do if password was entered correctly!
            Exit
        Else
            MsgBox(1024,"Password was wrong!","You typed the Wrong Password. You may not access this file!"); What to do if password was not entered correctly!
            Exit
        EndIf
    
    Case @Error = 1;The Cancel button was pushed
        MsgBox(1024,"You pressed the Cancel Button!","A Password is requuired to access this file!")
        Exit
    
    Case @Error = 3;The InputBox failed to open
    ;

EndSelect
Edited by GoogleDude

Share this post


Link to post
Share on other sites



Cool!! I could see a use for this..

Share this post


Link to post
Share on other sites

Badeasy but handy :whistle:


-jaenster

Share this post


Link to post
Share on other sites

@GoogleDude

To make the password check case sensitive use the double equals sign "=="

If $InputBoxAnswer == $password Then

In fact, you could probably tie the whole thing up into function. Might post sample later if I have the time.

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

You mean like this?

#NoTrayIcon
;InputBox features: Title=Yes, Prompt=Yes, Default Text=No, Pwd Char=*123, Mandatory
#cs
    $password = "Password001"; Password needed to access script!
    If Not IsDeclared("InputBoxAnswer") Then Local $InputBoxAnswer
    $MaxAttempts = 3
#ce

Password(3, "me")

Func Password($MaxAttempts, $Password)
    $Attempts = 0
    Do
        $InputBoxAnswer = InputBox("Password Prompt!", "Please type in a password to access this program!", "", "*M", "-1", "-1", "-1", "-1")
        Select
            Case @error = 0;OK - The string returned is valid
                If $InputBoxAnswer == $Password Then
                    MsgBox(1024, "Password was correct!", "You typed the correct Password. You may access this file!"); What to do if password was entered correctly!
                    Exit
                Else
                    If $Attempts < $MaxAttempts - 1 And $MaxAttempts <> 1 Then
                        MsgBox(1024, "Password was wrong!", "You typed the Wrong Password. You may try again " & ($MaxAttempts - 1) - $Attempts & " time(s)"); What to do if password was not entered correctly!
                    Else
                        MsgBox(1024, "Password was wrong!", "Sorry, You can not access this file!"); What to do if password was not entered correctly!
                    EndIf
                    $Attempts += 1
                    ;Exit
                EndIf
                
            Case @error = 1;The Cancel button was pushed
                MsgBox(1024, "You pressed the Cancel Button!", "A Password is requuired to access this file!")
                Exit
                
            Case @error = 3;The InputBox failed to open
                ;

        EndSelect
    Until $Attempts = $MaxAttempts Or $InputBoxAnswer == $Password
EndFunc   ;==>Password

Edit: Ran tidy on the script.

Edited by NeoTroniX

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You mean like this?

Yeah... just like that :)

I realized that I have a bit of redundant code in there: the

Until $Attempts = $MaxAttempts Or $InputBoxAnswer == $Password

Could just be

Until $Attempts = $MaxAttempts

Since we already are checking for the password verification earlier

EDIT: Actually not quite like that, I was thinking more along the lines of returning values from the function, that way you can execute other actions on failure in the body of your script, or even put your Success and final Failure messages there. Like this:

#NoTrayIcon
;InputBox features: Title=Yes, Prompt=Yes, Default Text=No, Pwd Char=*123, Mandatory
#cs
  $password = "Password001"; Password needed to access script!
  If Not IsDeclared("InputBoxAnswer") Then Local $InputBoxAnswer
  $MaxAttempts = 3
#ce
If Password(3, "me") Then
  MsgBox(1024, "Password was correct!", "You typed the correct Password. You may access this file!"); What to do if password was entered correctly!
  ; do rest of stuff here
ElseIf @error = 1 Then
  MsgBox(1024, "Password was wrong!", "Sorry, You can not access this file!"); What to do if password was not entered correctly!
  ; do other non-authorized attempt stuff here
  Exit
ElseIf @error = 2 Then
  MsgBox(1024, "You pressed the Cancel Button!", "A Password is requuired to access this file!")
  ; do other Cancelled stuff here
  Exit
ElseIf @error = 3 Then
  ; do inputbox failure stuff here
EndIf


Func Password($MaxAttempts, $Password)
  ;returns 1 if successfully authorized
  ;returns 0 if not and sets @error = 1 if not authorized,
  ;                          @error = 2 if cancelled authorization
  ;                          @error = 3 if Input box failed  to open
  $Attempts = 0
  Do
    $InputBoxAnswer = InputBox("Password Prompt!", "Please type in a password to access this program!", "", "*M", "-1", "-1", "-1", "-1")
    Select
      Case @error = 0;OK - The string returned is valid
        If $InputBoxAnswer == $Password Then
          Return 1
        Else
          If $Attempts < $MaxAttempts - 1 And $MaxAttempts <> 1 Then
            MsgBox(1024, "Password was wrong!", "You typed the Wrong Password. You may try again " & ($MaxAttempts - 1) - $Attempts & " time(s)"); What to do if password was not entered correctly!
          Else
            SetError(1)
            Return 0
          EndIf
          $Attempts += 1
          ;Exit
        EndIf
      Case @error = 1;The Cancel button was pushed
        SetError(2)
        Return 0
      Case @error = 3;The InputBox failed to open
        SetError(3)
        Return 0
    EndSelect
  Until $Attempts = $MaxAttempts
EndFunc   ;==>Password
Edited by ResNullius

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