Jump to content
Sign in to follow this  
Patil

Can any one help to code for validation of username and password

Recommended Posts

Patil

Test.txt

Hi,

Can any one help me for my code.

Let me explain what i need to do, i have stored the user name and password in one file with comma seperated in one line.

In the file each line contains username and password for relevent username. I am trying to validate this through the script i am not getting exact solution for this

Finally i coded like this

$file = FileOpen("C:\Documents and Settings\santosh\Desktop\username.txt", 0)

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

$result = StringCompare($line, "manager")

if $result == 0 Then ExitLoop

;Next

WEnd

$file = FileOpen("C:\Documents and Settings\santosh\Desktop\password.txt", 0)

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

$result1 = StringCompare($line, "manager")

if $result1 == 0 Then ExitLoop

WEnd

MsgBox(0, "String of :",$result & "," & $result1)

For this script what i have done i created a seperated file for username and password. it is working fine but its fine when all user name are updated regularly.

Suppose if the user name is santosh and password for this username is admin i am not entering the admin for this user but i have a password named admin for the user name admin at that time its wrong is it not? coz at this time also it is going to return 0.......

even i will attach the doument for the user name and password

Share this post


Link to post
Share on other sites
PsaltyDS

First the obvious: A list of user names and passwords in a plain text file?! :P

Then you can try this:

#include <file.au3>

; Each line of file is "User Name, Password"
Global $sFile = "C:\Temp\test.txt"
Global $avFile, $sPasswd = "<not found>"

; Get user name to search for
$sUser = InputBox("User Data", "Enter user name:")
If @error Then Exit

; Read file into an array
_FileReadToArray($sFile, $avFile)
If @error Then
    MsgBox(16, "Error", "Error opening file: " & $sFile)
    Exit
EndIf

; Search array for user name and get password
For $i = 1 To $avFile[0]
    If StringLeft($avFile[$i], StringLen($sUser) + 1) = $sUser & ","  Then
        $sPasswd = StringStripWS(StringMid($avFile[$i], StringInStr($avFile[$i], ",") + 1), 8)
        ExitLoop
    EndIf
Next

; Display results
MsgBox(64, "Results", "User:  " & $sUser & @CRLF & "Passwd:  " & $sPasswd)

;)


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
John117

As an addition to Psalty's code, the follow adapation will allow you to use an Encrypted file to pull the names from. All you will need to do to encrypt the names on the txt file is to open the demo located at (C:\Program Files\AutoIt3\Examples\GUI\Advanced\encrypt.au3 - if you default installed) and you can test out 12345 as the password and 1 as the level. You will need to encrypt the name and password together but do not encrypt two names at once. -It will put them on a single line and they will then belong to the same array location. Then you can run the script and give it a try.

(The whole purpose being to have the txt file encrypted.)

#include <file.au3>
#include <guiconstants.au3>
#include <string.au3>

; Each line of file is "User Name, Password"
Global $sFile = "D:\Autoit Help\test.txt"
Global $avFile, $sPasswd = "<not found>"

; Get user name to search for
$sUser = InputBox("User Data", "Enter user name:")
If @error Then Exit

; Read file into an array
_FileReadToArray($sFile, $avFile)
If @error Then
    MsgBox(16, "Error", "Error opening file: " & $sFile)
    Exit
EndIf

; Search array for user name and get password
For $i = 1 To $avFile[0]
    ;The Below line decrypts at level "1" using "12345" as password. "1" Being lowest, "10" being highest.
    $Temp = _StringEncrypt(0,$avFile[$i], "12345", "1")
    If StringLeft($Temp, StringLen($sUser) + 1) = $sUser & ","  Then
        $sPasswd = StringStripWS(StringMid($Temp, StringInStr($Temp, ",") + 1), 8)
        ExitLoop
    EndIf
Next

; Display results
MsgBox(64, "Results", "User:  " & $sUser & @CRLF & "Passwd:  " & $sPasswd)
Edited by Hatcheda

Share this post


Link to post
Share on other sites
Patil

As an addition to Psalty's code, the follow adapation will allow you to use an Encrypted file to pull the names from. All you will need to do to encrypt the names on the txt file is to open the demo located at (C:\Program Files\AutoIt3\Examples\GUI\Advanced\encrypt.au3 - if you default installed) and you can test out 12345 as the password and 1 as the level. You will need to encrypt the name and password together but do not encrypt two names at once. -It will put them on a single line and they will then belong to the same array location. Then you can run the script and give it a try.

(The whole purpose being to have the txt file encrypted.)

#include <file.au3>
#include <guiconstants.au3>
#include <string.au3>

; Each line of file is "User Name, Password"
Global $sFile = "D:\Autoit Help\test.txt"
Global $avFile, $sPasswd = "<not found>"

; Get user name to search for
$sUser = InputBox("User Data", "Enter user name:")
If @error Then Exit

; Read file into an array
_FileReadToArray($sFile, $avFile)
If @error Then
    MsgBox(16, "Error", "Error opening file: " & $sFile)
    Exit
EndIf

; Search array for user name and get password
For $i = 1 To $avFile[0]
    ;The Below line decrypts at level "1" using "12345" as password. "1" Being lowest, "10" being highest.
    $Temp = _StringEncrypt(0,$avFile[$i], "12345", "1")
    If StringLeft($Temp, StringLen($sUser) + 1) = $sUser & ","  Then
        $sPasswd = StringStripWS(StringMid($Temp, StringInStr($Temp, ",") + 1), 8)
        ExitLoop
    EndIf
Next

; Display results
MsgBox(64, "Results", "User:  " & $sUser & @CRLF & "Passwd:  " & $sPasswd)
Hi,

Thanks for your code........

but in this it is validating only if the user name is exists. I want to validate both username as well password.

for example assume that my file named username.txt is the data base which contains all user details like username and password, and want to validate those username and password like if i am giving some username and password that has to validated from the database it needs to retrieve from the date base and validate whether the given username and password is valid or not

Share this post


Link to post
Share on other sites
Patil

As an addition to Psalty's code, the follow adapation will allow you to use an Encrypted file to pull the names from. All you will need to do to encrypt the names on the txt file is to open the demo located at (C:\Program Files\AutoIt3\Examples\GUI\Advanced\encrypt.au3 - if you default installed) and you can test out 12345 as the password and 1 as the level. You will need to encrypt the name and password together but do not encrypt two names at once. -It will put them on a single line and they will then belong to the same array location. Then you can run the script and give it a try.

(The whole purpose being to have the txt file encrypted.)

#include <file.au3>
#include <guiconstants.au3>
#include <string.au3>

; Each line of file is "User Name, Password"
Global $sFile = "D:\Autoit Help\test.txt"
Global $avFile, $sPasswd = "<not found>"

; Get user name to search for
$sUser = InputBox("User Data", "Enter user name:")
If @error Then Exit
Thanks for your code........
but in this it is validating only if the user name is exists. I want to validate both username as well password.
for example assume that my file named username.txt is the data base which contains all user details like username and password, and want to validate those username and password like if i am giving some username and password that has to validated from the database it needs to retrieve from the date base and validate whether the given username and password is valid or not
; Read file into an array
_FileReadToArray($sFile, $avFile)
If @error Then
    MsgBox(16, "Error", "Error opening file: " & $sFile)
    Exit
EndIf

; Search array for user name and get password
For $i = 1 To $avFile[0]
    ;The Below line decrypts at level "1" using "12345" as password. "1" Being lowest, "10" being highest.
    $Temp = _StringEncrypt(0,$avFile[$i], "12345", "1")
    If StringLeft($Temp, StringLen($sUser) + 1) = $sUser & ","  Then
        $sPasswd = StringStripWS(StringMid($Temp, StringInStr($Temp, ",") + 1), 8)
        ExitLoop
    EndIf
Next

; Display results
MsgBox(64, "Results", "User:  " & $sUser & @CRLF & "Passwd:  " & $sPasswd)

Share this post


Link to post
Share on other sites
John117

um-k - as currently set, It does validate both. You type the username. If the return password is <not found> the username is wrong. If it does exsist, both the username and password are displayed. Then you can see that the password matches or not. -if you did not encrypt like I said, you will never see the password. you must encrypt at level 1 with password 12345

Dont undersand?

add this to your file

DACECD90DEE05DE268AC584823115BEA03E94710911BC4A2BEC1

DDCFCD9EDFE05D9B13A85C4D261121EE029C

-now try the username

Hatcheda

or

Yoda

if you would like one that does both and requires you to type in the password use the following. but it will require that the password you type is correct. -if it is wrong it will not work unless you leave the debug msgbox on. (shows what you typed and what is in the txt file.

;Include Autoit Functions
#include <file.au3>
#include <guiconstants.au3>
#include <string.au3>
;Set Hot Keys - (They call Functions listed near the bottom)
HotKeySet("{ESC}", "Terminate")
; Each line of file is "User Name, Password"
Global $sFile = "D:\Autoit Help\test.txt"
Global $avFile, $sPasswd = "<not found>"
;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays
;******************************************************************************************************************************************
; Read file into an array
_FileReadToArray($sFile, $avFile)
If @error Then
    MsgBox(16, "Error", "Error opening file: " & $sFile)
    Exit
EndIf
;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI
;******************************************************************************************************************************************
;Create the basic GUI
$window = GUICreate("MY Messenger", 610, 600, 300, 250)
$Group1 = GUICtrlCreateGroup("Logon:", 24, 40, 561, 100)
$Edit1 = GUICtrlCreateEdit("Username", 300, 80, 150, 17, $ES_WANTRETURN)
$Edit2 = GUICtrlCreateEdit("Password", 300, 100, 150, 17, $ES_WANTRETURN)
$Button1 = GUICtrlCreateButton("Submit", 495, 120, 60, 17)
;Show the finished resluts of the GUI
GUISetState(@SW_SHOW)
;Keep the GUI open and monitoring.
While 1
    $nMsg = GUIGetMsg()
    $cursorinfo = GUIGetCursorInfo($window)
    If $cursorinfo[4] = $Edit1 Then
        If $cursorinfo[2] = 1 Then
            GUICtrlSetData($Edit1, "")
        EndIf
    EndIf
    If $cursorinfo[4] = $Edit2 Then
        If $cursorinfo[2] = 1 Then
            GUICtrlSetData($Edit2, "")
        EndIf
    EndIf
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
        Case ($nMsg = $Button1)
            Submit()
    EndSelect
WEnd
;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS
;*******************************************************************************************************************************************
Func Submit()
    ; Search array for user name and get password
    For $i = 1 To $avFile[0]
        ;The Below line decrypts at level "1" using "12345" as password. "1" Being lowest, "10" being highest.
        $Temp = _StringEncrypt(0, $avFile[$i], "12345", "1")
        If StringLeft($Temp, StringLen(GUICtrlRead($Edit1)) + 1) = GUICtrlRead($Edit1) & "," Then
            $sPasswd = StringStripWS(StringMid($Temp, StringInStr($Temp, ",") + 1), 8)
            ExitLoop
        EndIf
    Next
    ; Display results
    If $sPasswd = GUICtrlRead($Edit2) Then
        MsgBox(64, "Verified", "You could do whatever else here after it's verified.")
    Else
        MsgBox(64, "Woops", "Try again")
        MsgBox(64, "Stored", $sPasswd) ;used for debug only
        MsgBox(64, "Typed", GUICtrlRead($Edit2)) ;used for debug only
    EndIf
EndFunc   ;==>Submit
Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

is this what you were asking?

Edited by Hatcheda

Share this post


Link to post
Share on other sites
Patil

Hi,

Can any one help me for my code.

Let me explain what i need to do, i have stored the user name and password in one file with comma seperated in one line.

In the file each line contains username and password for relevent username. I am trying to validate this through the script i am not getting exact solution for this,

I want to validate both username as well password.for example assume that my file named username.txt is the data base which contains all user details like username and password, and i want to validate those username and password.

like if i am giving some username and password that has to validated from the database it needs to be retrieve from the date base that is .txt file and validate whether the given username and password is valid/exists or not. if it is exists valid if not inbvalid rite?

i have tried with the following code its not working for me.......

$file = FileOpen("C:\Documents and Settings\santosh\Desktop\password.txt", 0)

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

$result1 = StringCompare($line, "manager")

if $result1 == 0 Then ExitLoop

WEnd

MsgBox(0, "String of :" , $result1)

Share this post


Link to post
Share on other sites
Patil

um-k - as currently set, It does validate both. You type the username. If the return password is <not found> the username is wrong. If it does exsist, both the username and password are displayed. Then you can see that the password matches or not. -if you did not encrypt like I said, you will never see the password. you must encrypt at level 1 with password 12345

Dont undersand?

add this to your file

DACECD90DEE05DE268AC584823115BEA03E94710911BC4A2BEC1

DDCFCD9EDFE05D9B13A85C4D261121EE029C

-now try the username

Hatcheda

or

Yoda

if you would like one that does both and requires you to type in the password use the following. but it will require that the password you type is correct. -if it is wrong it will not work unless you leave the debug msgbox on. (shows what you typed and what is in the txt file.

;Include Autoit Functions
#include <file.au3>
#include <guiconstants.au3>
#include <string.au3>
;Set Hot Keys - (They call Functions listed near the bottom)
HotKeySet("{ESC}", "Terminate")
; Each line of file is "User Name, Password"
Global $sFile = "D:\Autoit Help\test.txt"
Global $avFile, $sPasswd = "<not found>"
;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays;Arrays
;******************************************************************************************************************************************
; Read file into an array
_FileReadToArray($sFile, $avFile)
If @error Then
    MsgBox(16, "Error", "Error opening file: " & $sFile)
    Exit
EndIf
;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI;GUI
;******************************************************************************************************************************************
;Create the basic GUI
$window = GUICreate("MY Messenger", 610, 600, 300, 250)
$Group1 = GUICtrlCreateGroup("Logon:", 24, 40, 561, 100)
$Edit1 = GUICtrlCreateEdit("Username", 300, 80, 150, 17, $ES_WANTRETURN)
$Edit2 = GUICtrlCreateEdit("Password", 300, 100, 150, 17, $ES_WANTRETURN)
$Button1 = GUICtrlCreateButton("Submit", 495, 120, 60, 17)
;Show the finished resluts of the GUI
GUISetState(@SW_SHOW)
;Keep the GUI open and monitoring.
While 1
    $nMsg = GUIGetMsg()
    $cursorinfo = GUIGetCursorInfo($window)
    If $cursorinfo[4] = $Edit1 Then
        If $cursorinfo[2] = 1 Then
            GUICtrlSetData($Edit1, "")
        EndIf
    EndIf
    If $cursorinfo[4] = $Edit2 Then
        If $cursorinfo[2] = 1 Then
            GUICtrlSetData($Edit2, "")
        EndIf
    EndIf
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
        Case ($nMsg = $Button1)
            Submit()
    EndSelect
WEnd
;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS;FUNCTIONS
;*******************************************************************************************************************************************
Func Submit()
    ; Search array for user name and get password
    For $i = 1 To $avFile[0]
        ;The Below line decrypts at level "1" using "12345" as password. "1" Being lowest, "10" being highest.
        $Temp = _StringEncrypt(0, $avFile[$i], "12345", "1")
        If StringLeft($Temp, StringLen(GUICtrlRead($Edit1)) + 1) = GUICtrlRead($Edit1) & "," Then
            $sPasswd = StringStripWS(StringMid($Temp, StringInStr($Temp, ",") + 1), 8)
            ExitLoop
        EndIf
    Next
    ; Display results
    If $sPasswd = GUICtrlRead($Edit2) Then
        MsgBox(64, "Verified", "You could do whatever else here after it's verified.")
    Else
        MsgBox(64, "Woops", "Try again")
        MsgBox(64, "Stored", $sPasswd) ;used for debug only
        MsgBox(64, "Typed", GUICtrlRead($Edit2)) ;used for debug only
    EndIf
EndFunc   ;==>Submit
Func Terminate()
    Exit 0
EndFunc   ;==>Terminate

is this what you were asking?

Hi,

Thanks a lot......

m little bit confusion so i asked you once again, i didn't get an idea that by seeing that password's value we ll come to whether it is valid or not.......

Share this post


Link to post
Share on other sites
John117

Well, if you want to visually compare, then you can use my my post #3 if you care to encrypt the text file for security or P's #2 if you dont care to encrypt it.

If you dont want to visually compare your database to your result then you can just read the password from the database and type it in with the userid using the code in post #6

Remember that to use either of my posts you will have to use the encryption on your text file first. You can change the level to higher if you want and change the password. -just be sure that the encryption level you use to encrypt the txt file matches the script. (meaning change the script to match!)

Did you have any other questions, complaints, clarity issues?

Edited by Hatcheda

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  

×