Sign in to follow this  
Followers 0
Blue_Drache

Yet Another Password Generator

1 post in this topic

#1 ·  Posted (edited)

This is great for building on-the-fly passwords within scripts to help with string encryption and what not on a PER SESSION basis.

Opt("MustDeclareVars", 1) ; Option Explicit
Dim $x, $test = _GetKey(4, 1, 4, 8)
For $x = 1 To $test[0]
    MsgBox(0, "", $test[$x])
Next
; *****************************************************************************
; *                              _GetKey()                                    *
; *  USE:  $var = _GetKey($type, $case, $number, $length)                     *
; *  $var = returned array (IE: "x5$pL_qf9")                                  *
; *  $type = Integer value representing type of key returned.                 *
; *              0 = Most ASCII (default), 1 = Most lower bit ascii,          *
; *              2 = AlphaNeumeric, 3 = Alpha only, 4 = Neumeric only         *
; *  $case = Integer value representing case sense of the returned value.     *
; *              0 = Mixed case (default), 1 = Upper case, 2 = Lower case     *
; *  $number = Integer value representing the number of keys to return.       *
; *              Default = 1.  Also returned as $var[0]                       *
; *  $length = Integer value representing the length of key to return.        *
; *              Default = 128.  Max of 2048.                                 *
; *  Success: Returns an array containing the randomly generated keys.  The   *
; *              zero array index contains the number of elements.  Similar   *
; *              to StringSplit()                                             *
; *  Failure: Returns a non-array numeric zero and sets @error = 1            *
; *                                                                           *
; *****************************************************************************
Func _GetKey($i_Type = 0, $i_Case = 0, $i_Number = 1, $i_Length = 128)
    Local $sz_Key, $int_Chr, $int_Count = 0, $a_RetArray[$i_Number + 1]
    $a_RetArray[0] = $i_Number
    if $i_Length > 2048 Then
        SetError(1)
        Return 0
    EndIf
    Do
        Switch $i_Type
            Case 0 ; Most ASCII
                $sz_Key = ""
                Do
                    $int_Chr = Random(33, 254, 1)
                    Switch $int_Chr
                        Case 34, 37, 39, 44, 46, 47, 58 To 63, 91 To 96, 128 To 187
                            ; reroll 
                        Case Else
                            $sz_Key = $sz_Key & Chr($int_Chr)
                    EndSwitch
                Until StringLen($sz_Key) = $i_Length
            Case 1 ; Most lower bit only
                $sz_Key = ""
                Do
                    $int_Chr = Random(33, 126, 1)
                    Switch $int_Chr
                        Case 34, 37, 39, 44, 46, 47, 58 To 63, 91 To 96
                            ; reroll
                        Case Else
                            $sz_Key = $sz_Key & Chr($int_Chr)
                    EndSwitch
                Until StringLen($sz_Key) = $i_Length
            Case 2 ; AlphaNeumeric only
                $sz_Key = ""
                Do
                    $int_Chr = Random(48, 122, 1)
                    Switch $int_Chr
                        Case 58 To 64, 91 To 96
                            ; reroll
                        Case Else
                            $sz_Key = $sz_Key & Chr($int_Chr)
                    EndSwitch
                Until StringLen($sz_Key) = $i_Length
            Case 3 ; Alpha only
                $sz_Key = ""
                Do
                    $int_Chr = Random(65, 122, 1)
                    Switch $int_Chr
                        Case 91 To 96
                            ; reroll
                        Case Else
                            $sz_Key = $sz_Key & Chr($int_Chr)
                    EndSwitch
                Until StringLen($sz_Key) = $i_Length
            Case 4 ; Numbers only
                $sz_Key = ""
                Do
                    $int_Chr = Random(48, 57, 1)
                    $sz_Key = $sz_Key & Chr($int_Chr)
                Until StringLen($sz_Key) = $i_Length
        EndSwitch
        Switch $i_Case
            Case 1
                $sz_Key = StringUpper($sz_Key)
            Case 2
                $sz_Key = StringLower($sz_Key)
        EndSwitch
        $int_Count = $int_Count + 1
        $a_RetArray[$int_Count] = $sz_Key
    Until $int_Count >= $i_Number
    Return $a_RetArray
EndFunc   ;==>_GetKeyoÝ÷ ØLZ^jëh×6#include <String.au3>
Dim $TextFile = @TempDir & "\Sht" & Random(10000, 99999, 1) & ".txt", $pw = _GetKey(), $fTemp


    If Not FileExists($TextFile) Then
        $hndl_TextFile = FileOpen($TextFile, 2)
        If $hndl_TextFile = -1 Then
            DBP($version, "File Creation Error", 20080, $i_debug)
            Exit 20080
        EndIf
        Do
            $fTemp = InputBox("Password Entry", "Please enter password", "", "*")
            Switch @error
                Case 1
                    DBP($version, "User pressed cancel.", 20051, $i_debug)
                    Exit 20051
                Case 2
                    DBP($version, "Timeout.", 20052, $i_debug)
                    Exit 20052
                Case 3
                    DBP($version, "Bad Inputbox.", 20053, $i_debug)
                    Exit 20053
            EndSwitch
        Until $fTemp <> "" And @error = 0
        $fTemp = _StringEncrypt(1, $fTemp, $pw[1], 4)
        FileWrite($hndl_TextFile, $fTemp)
        FileClose($hndl_TextFile)
    Else
        $hndl_TextFile = FileOpen($TextFile, 0)
        If $hndl_TextFile = -1 Then
            DBP($version, "File Open Error", 20090, $i_debug)
            Exit 20090
        EndIf
        $fTemp = FileReadLine($hndl_TextFile)
    EndIf
    _IEFormElementSetValue($o_RTB_InputPass, _StringEncrypt(0, $fTemp, $pw[1], 4))

Func DBP($s_ver = "", $s_msg = "", $i_err = 0, $i_flag = 0)
    ; s_msg is the message displayed, $i_err is the error code
    ; $i_flag toggles messagebox display.  Easily converts your debug notes into a custom errorhandler
    If $s_ver = "" Then $s_ver = "DBP: Error notification"
    If $s_msg = "" Then $s_msg = "Undefined message and/or error"
    $s_msg = "[" & @HOUR & ":" & @MIN & ":" & @SEC & "] " & $s_msg
    ConsoleWrite($s_msg & "  Error: " & $i_err & @LF)
    If $i_flag = 1 Then MsgBox(0x1010, $s_ver, $s_msg & @LF & "Error: " & $i_err)
    SetError($i_err) ; returns $i_err as @error
EndFunc   ;==>DBP

The user entered password is never stored in memory nor on disk in a non-encrypted form. This is not 100% fool proof, but it's decent enough for what I need it for. Perhaps you may find it useful as well. Thank you for reading!

Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

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