MyEarth

"Secure" random characters

7 posts in this topic

#1 ·  Posted (edited)

Hello,

There are many example of "random data generator" or similar with this language on the forum but i don't have found nothing that use a method can be considerated secure or cryptographic strong. We have the function in one of the UDF like:

_Crypt_GenRandom

CryptAcquireContext ( internal of Crypt.au3 )

CryptReleaseContext ( internal of Crypt.au3 )

/dev/urandom ( autoit Random? )

But none example in this way. I'd like to start from an array of characters like this:

Local $aKSet[52] = ['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', _
        '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']

Because i what to choice what char include in the random final string ( like only upper, or from A to K or remove some char etc )

I hope someone can provide an example using this input, thanks

Some reference:

http://en.wikipedia.org/wiki/Random_password_generator#Stronger_methods

http://en.wikipedia.org/wiki/CryptGenRandom

Edited by MyEarth

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I wrote a very rudimentary Random String Generator quickly last year for creating stronger passwords for websites. I incorporated a GUI with a few options for creating passwords based on a few different sites, and the rules that applied to making passwords. Again it's simple and to the point, nothing extraordinary, but I'm happy to share it with you if it may give you an idea on how to develop your applicaton

#cs TITLE - Random String Generator

    Title:          Random String Generator
    Filename:       generator\generator.au3
    Description:
    Author:         Realm a.k.a MicroRealm
    Version:        2014.0.0.8
    Initial Alpha:  April 24th, 2014
    Last Update:    July 2nd, 2014

    Script state Requirements:
        AutoIt3 3.3.10.0 or higher

    Other Reguirements:
        None

#ce

#cs DEVELOPMENT LOG

    v 2014.0.0.8 - July 2nd, 2014
    -Added Character Support for eBay

    v 0.0.0.7 - June 11th, 2014
    -Added Full Character support
    *Realm 1.5

    v 0.0.0.6 - April 26th, 2014
    -Fixed 2014-1: Array variable has incorrect number of subscripts or subscript dimension range exceeded.(259)
    -------------> Modified _Singular_Randomizer() to repopulate character pools once the original pool is exhausted.
    *Realm 0.5

    v 0.0.0.5 - April 25th, 2014
    -Created: _String_Randomiser() to simply resort strings randomly.
    *Realm 0.5
    -Cleaned up Comments
    *Realm 0.5

    v 0.0.0.4 - April 25th, 2014
    -Modified: _Singular_Randomiser to randomize the generatorlists and strings before picking
    *Realm 2.25

    v 0.0.0.3 - April 25th, 2014
    -Created _Singular_Randomizer() Algorithm to deliver random passwords without duplicate characters.
    *Realm 1.5

    v 0.0.0.2 - April 24th, 2014
    -Created GUI
    -Created Internal Global Settings for objects
    *Realm 3.25

    v 0.0.0.1 - April 24th, 2014
    -Alpha Stage Initiated.
    *Realm 1.25

#ce ============================================================================================================================

#Region - Inlcudes
    #include <GUIConstantsEx.au3>
    #include <StaticConstants.au3>
    #include <Array.au3>
#EndRegion - Includes

#Region - Main Script

    ;Easy GUI Size Adjustments
    Local $iCols = 2
    Local $iRows = 11
    Local $iHdrs = 3
    Local $iPadding_border = 5
    Local $iPadding_column = 2
    Local $iPadding_row    = 2
    Local $iDepth_AllObjects = 20
    Local $iWidth_Col_a      = 75
    Local $iWidth_Col_b      = 213
    Local $hdrDepth = 30


    Local $hdrWidth = FindGreatestObject($iWidth_Col_a, $iWidth_Col_b)
    Local $iWidth_gui      = ($iPadding_border * 2) + ($iPadding_column * ($iCols - 1)) + ($iWidth_Col_a + $iWidth_Col_b)
    Local $iDepth_gui      = ($iPadding_border * 2) + ($iPadding_row * (($iHdrs - 1) + ($iRows - 1))) + ($iDepth_AllObjects * $iRows) + ($hdrDepth * $iHdrs)
    Local $iTop_MainModule = $iPadding_border + 1

    Local $hdrLeft  = 0

    Local $butDepth = 20
    Local $butWidth = 150

    Local $cbWidth  = $iDepth_AllObjects
    Local $cbDepth  = $iDepth_AllObjects
    Local $cbLeft   = $iPadding_border + 1 + $iWidth_Col_a - $cbWidth

    Local $inpWidth = 60
    Local $inpDepth = $iDepth_AllObjects
    Local $inpLeft  = $iPadding_border + 1 + ($iWidth_Col_a - $inpWidth)

    Local $lblWidth = $iWidth_Col_b
    Local $lblLeft  = $iPadding_border + 1 + $iWidth_Col_a + $iPadding_column

    Local $iTop = $iTop_MainModule
    Global $cb_rul_characters, $cb_rul_spclA
    ;GUI
        Local $guiMain = GUICreate($iWidth_gui & 'x' & $iDepth_gui & ' generator', $iWidth_gui, $iDepth_gui)
        GUISetFont(12.5)

        ;Settings Section
        GUICtrlCreateLabel('Settings:', $hdrLeft, $iTop, $hdrWidth, $hdrDepth )
        GUICtrlSetFont(-1, 16, 400)
        $iTop += ($hdrDepth + $iPadding_row)

            Local $iLimitMax = 512
            Local $iLimitMin = 1
            Local $inp_chars = GUICtrlCreateInput('16', $inpLeft, $iTop, $inpWidth, $inpDepth, $SS_RIGHT)
            GUICtrlSetLimit($inp_chars, $iLimitMax, $iLimitMin)
            GUICtrlCreateUpdown($inp_chars)
            GUICtrlCreateLabel('Random Characters', $lblLeft, $iTop, $iWidth_Col_b, $iDepth_AllObjects )
            $iTop += ($inpDepth + $iPadding_row)

        ;Inlcude Section
        GUICtrlCreateLabel('Include:', $hdrLeft, $iTop, $hdrWidth, $hdrDepth)
        GUICtrlSetFont(-1, 16, 400)
        $iTop += ($hdrDepth + $iPadding_row)

            $cb_rul_letters = GUICtrlCreateCheckbox('', $cbLeft, $iTop, $cbWidth, $cbDepth)
            GUICtrlSetState(-1, $GUI_CHECKED)
            GUICtrlCreateLabel('Consist of letters', $lblLeft, $iTop, $lblWidth, $cbDepth)
            $iTop += ($cbDepth + $iPadding_row)

            $cb_rul_numbers = GUICtrlCreateCheckbox('', $cbLeft, $iTop, $cbWidth, $cbDepth)
            GUICtrlSetState(-1, $GUI_CHECKED)
            GUICtrlCreateLabel('Consist of numbers', $lblLeft, $iTop, $lblWidth, $cbDepth)
            $iTop += ($cbDepth + $iPadding_row)

            $cb_rul_characters = GUICtrlCreateCheckbox('', $cbLeft, $iTop, $cbWidth, $cbDepth)
            GUICtrlSetState(-1, $GUI_CHECKED)
            GUICtrlCreateLabel('Consist of characters', $lblLeft, $iTop, $lblWidth, $cbDepth)
            $iTop += ($cbDepth + $iPadding_row)

            $cb_rul_spclA = GUICtrlCreateCheckbox('', $cbLeft, $iTop, $cbWidth, $cbDepth)
            GUICtrlSetState(-1, $GUI_UNCHECKED)
            GUICtrlCreateLabel('Only Chars: (!,@,#,%)', $lblLeft, $iTop, $lblWidth, $cbDepth )
            $iTop += ($cbDepth + $iPadding_row)

            ;eBay
            $cb_rul_spclB = GUICtrlCreateCheckbox('', $cbLeft, $iTop, $cbWidth, $cbDepth)
            GUICtrlSetTip($cb_rul_spclB, 'eBay', 'Examples:')
            GUICtrlSetState(-1, $GUI_UNCHECKED)
            GUICtrlCreateLabel('No Chars: (<,>,[,],/)', $lblLeft, $iTop, $lblWidth, $cbDepth )
            $iTop += ($cbDepth + $iPadding_row)

        ;Rules Section
        GUICtrlCreateLabel('Rules:', $hdrLeft, $iTop, $hdrWidth, $hdrDepth)
        GUICtrlSetFont(-1, 16, 400)
        $iTop += ($hdrDepth + $iPadding_row)

            $cb_inc_lower = GUICtrlCreateCheckbox('', $cbLeft, $iTop, $cbWidth, $cbDepth)
            GUICtrlSetState(-1, $GUI_CHECKED)
            GUICtrlCreateLabel('Include a lower case letter', $lblLeft, $iTop, $lblWidth, $cbDepth )
            $iTop += ($cbDepth + $iPadding_row)

            $cb_inc_upper = GUICtrlCreateCheckbox('', $cbLeft, $iTop, $cbWidth, $cbDepth)
            GUICtrlSetState(-1, $GUI_CHECKED)
            GUICtrlCreateLabel('Include an upper case letter', $lblLeft, $iTop, $lblWidth, $cbDepth )
            $iTop += ($cbDepth + $iPadding_row)

            $cb_Inc_number = GUICtrlCreateCheckbox('', $cbLeft, $iTop, $cbWidth, $cbDepth)
            GUICtrlSetState(-1, $GUI_CHECKED)
            GUICtrlCreateLabel('Include a number', $lblLeft, $iTop, $lblWidth, $cbDepth)
            $iTop += ($cbDepth + $iPadding_row)


        ;Run Module button
        $iTop += ($iDepth_AllObjects + $iPadding_row) ;Added for spacer between last section and button

            $but_run = GUICtrlCreateButton('Run', ($iWidth_gui - $butWidth) / 2, $iTop-10, $butWidth, $butDepth+10)
            $iTop += ($cbDepth + $iPadding_row)

    GUISetState(@SW_SHOW)

    While 1
        $msg = GUIGetMsg()

        Select

            Case $msg = -3 ;$GUI_EVENT_CLOSE
                Exit

            Case $msg = $but_run
                Local $iChar = 0
                If _IsChecked($cb_rul_spclB) Then
                        $iChar = 4
                    ElseIf _IsChecked($cb_rul_spclA) Then
                        $iChar = 2
                    ElseIf _IsChecked($cb_rul_characters) Then
                        $iChar = 1
                EndIf
                Local $sReturn = _Singular_Randomizer(Int(GUICtrlRead($inp_chars)),_IsChecked($cb_rul_letters),_IsChecked($cb_Inc_number),$iChar,_IsChecked($cb_inc_lower),_IsChecked($cb_inc_upper),_IsChecked($cb_rul_numbers))
                ClipPut($sReturn)
                TrayTip('New ' & StringLen($sReturn) & ' Character Password Created.', 'Password hass been placed in your clipboard.', 30)

            ;Rules CheckBox Group "Changes in this Group must be changed in Function CheckBoxGroup_Rules() as well"
            Case $msg = $cb_rul_characters
                If _IsChecked($cb_rul_characters) Then CheckBoxGroup_Rules($cb_rul_characters)
            Case $msg = $cb_rul_spclA
                If _IsChecked($cb_rul_characters) Then CheckBoxGroup_Rules($cb_rul_spclA)
            Case $msg = $cb_rul_spclB
                If _IsChecked($cb_rul_characters) Then CheckBoxGroup_Rules($cb_rul_spclB)
        EndSelect

    WEnd

#EndRegion - Main Script

#Region - Core Functions

;Checkbox Group - Characters
    Func CheckBoxGroup_Rules($controlID)

        If $cb_rul_characters <> $controlID Then GUICtrlSetState($cb_rul_characters, $GUI_UNCHECKED)
        If $cb_rul_spclA <> $controlID Then GUICtrlSetState($cb_rul_spclA, $GUI_UNCHECKED)
        If $cb_rul_spclB <> $controlID Then GUICtrlSetState($cb_rul_spclB, $GUI_UNCHECKED)

    EndFunc

;FindGreatest Object
    Func FindGreatestObject($iParam1, $iParam2 = 0, $iParam3 = 0, $iParam4 = 0, $iParam5 = 0, $iParam6 = 0, $iParam7 = 0, $iParam8 = 0, $iParam9 = 0, $iParam10 = 0, $iParam11 = 0, $iParam20 = 0, $iParam21 = 0, $iParam22 = 0, $iParam23 = 0, $iParam24 = 0, $iParam25 = 0)
        Local $iObjects = @NumParams
        Local $str = 'iParam'
        Local $iGreatestObject = 0, $iObj
        For $i = 1 To $iObjects
            $iObj = Int(Eval($str & $i))
            If $iObj > $iGreatestObject Then $iGreatestObject = $iObj
        Next
        Return $iGreatestObject
    EndFunc

;_IsChecked
    Func _IsChecked($iControlID)
        Return BitAND(GUICtrlRead($iControlID), $GUI_CHECKED) = $GUI_CHECKED
    EndFunc   ;==>_IsChecked

    Func _Singular_Randomizer($iCount, $letters, $numbers, $iChar, $incLower, $incUpper, $incNumber)
        Local $string, $list, $ub = 0, $sListRestore
        If $letters Then
            If $incLower Then
                $sListRestore &= _String_Randomiser('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
                $aTemp = StringSplit( _String_Randomiser('ABCDEFGHIJKLMNOPQRSTUVWXYZ'), '', 1+2)
                $ub = Random(0,25,1)
                $string &= $aTemp[$ub]
                _ArrayDelete($aTemp,$ub)
                For $i = 0 To UBound($aTemp)-1
                    $list &= $aTemp[$i]
                Next
                $iCount -= 1
            EndIf
            If $incUpper Then
                $sListRestore &= _String_Randomiser('abcdefghijklmnopqrstuvwxyz')
                $aTemp = StringSplit( _String_Randomiser('abcdefghijklmnopqrstuvwxyz'), '', 1+2)
                $ub = Random(0,25,1)
                $string &= $aTemp[$ub]
                _ArrayDelete($aTemp,$ub)
                For $i = 0 To UBound($aTemp)-1
                    $list &= $aTemp[$i]
                Next
                $iCount -= 1
            EndIf
        EndIf
        If $numbers Then
            $sListRestore &= _String_Randomiser('0123456789')
            $aTemp = StringSplit( _String_Randomiser('0123456789'), '', 1+2)
            $ub = Random(0,9,1)
            $string &= $aTemp[$ub]
            _ArrayDelete($aTemp,$ub)
            For $i = 0 To UBound($aTemp)-1
                $list &= $aTemp[$i]
            Next
            $iCount -= 1
        EndIf

        If $iChar Then
            Local $sChar
            If BitAND($iChar, 2) Then
                $sChar = '!@#%'
            Else
                ;0-255
                $sChar &= Chr(33)
                For $i = 33 To 46
                    $sChar &= Chr($i)
                Next
                If BitAND($iChar, 1) Then $sChar &= Chr(47)
                For $i = 58 To 59
                    $sChar &= Chr($i)
                Next
                If BitAND($iChar, 1) Then $sChar &= Chr(60)
                $sChar &= Chr(61)
                If BitAND($iChar, 1) Then $sChar &= Chr(62)
                For $i = 63 To 64
                    $sChar &= Chr($i)
                Next
                If BitAND($iChar, 1) Then $sChar &= Chr(91)
                $sChar &= Chr(92)
                If BitAND($iChar, 1) Then $sChar &= Chr(93)
                For $i = 94 To 96
                    $sChar &= Chr($i)
                Next
                For $i = 123 To 126
                    $sChar &= Chr($i)
                Next
                $sListRestore &= $sChar
                $aTemp = StringSplit( _String_Randomiser($sChar), '', 1+2)
                $ub = Random(0,3,1)
                $string &= $aTemp[$ub]
                _ArrayDelete($aTemp,$ub)
                For $i = 0 To UBound($aTemp)-1
                    $list &= $aTemp[$i]
                Next
                $iCount -= 1
            EndIf
        EndIf

;~      If $spclA Then
;~          $sListRestore &= _String_Randomiser('!@#%')
;~          $aTemp = StringSplit( _String_Randomiser('!@#%'), '', 1+2)
;~          $ub = Random(0,3,1)
;~          $string &= $aTemp[$ub]
;~          _ArrayDelete($aTemp,$ub)
;~          For $i = 0 To UBound($aTemp)-1
;~              $list &= $aTemp[$i]
;~          Next
;~          $iCount -= 1
;~      EndIf

        Local $aGenList = StringSplit( _String_Randomiser($list), '', 1+2)

        While $iCount
            $ub = Random(0, Int(UBound($aGenList))-1, 1)
            $string &= $aGenList[$ub]
            _ArrayDelete($aGenList,$ub)
            $iCount -= 1
            If Not UBound($aGenList) Then
                $aGenList = StringSplit( _String_Randomiser($sListRestore), '', 1+2)
            EndIf
        WEnd

        ;Randomize String before returning
        Return _String_Randomiser($string)

    EndFunc

    Func _String_Randomiser($sString)
        $aString = StringSplit($sString, '', 1+2)
        $sString = ''
        Do
            $ub = Random(0,Int(UBound($aString))-1,1)
            $sString &= $aString[$ub]
            _ArrayDelete($aString,$ub)
        Until Not UBound($aString)
        Return $sString
    EndFunc

#EndRegion - Core Functions
Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Your script, can be useful for others so thanks for posting, don't answer in any way to my question. I'm still searching, if i'm resolving i'll post the result.

Edited by MyEarth

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Sorry it didn't help, but I thought it might have been relevant and would give you an idea how to develop yours. Maybe I misunderstood your question. I thought that the section(s) in my script that had special rules such as using a number, or only uppercase would have helped you.

If I understand your question correctly wouldn't the random function fit as a simple solution?

$iRandom = Random(27,52,1)
$sRandomChar = $aKSet[$iRandom]

Edit: A rudimentary form of making sure not to include the letters K, L, or M:

While 1
    $iRandom = Random(27,52,1)
    $sRandomChar = $aKSet[$iRandom]
    If Not StringInStr( 'K,L,M', $sRandomChar) Then ExitLoop
WEnd
Edited by Realm

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites

msgbox (0, '' , _randm(6 , "abcdefghijklmnopqrstuvw" , 1))


func _randm($length , $exclude = "" , $case = 0)  ; exclude is a string of characters
                                                  ; case = 0 ALL , case = 1 lower ,  case = 2 upper

Local $aKSet[52] = ['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', _
        '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']

    If $case = 0 Then
        $min = 0
        $max = 51
    Endif

    If $case = 1 then
        $min = 0
        $max = 25
    EndIf

    If $case = 2 then
        $min = 26
        $max = 51
    EndIf

$sFinal = ""

for $i = 1 to $length
    $num = random($min , $max , 1)
    $randchar = $aKSet[$num]
    If stringinstr($exclude , $randchar , 1) then
        $i -= 1
        ContinueLoop
    EndIf

    $sFinal &= $randchar
Next

return $sFinal

EndFunc


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

>This may interest you...

edit: Latin Square Generator by BrewmanNH

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

@boththose:

AutoIt uses the Mersenne Twister pseudo-random generator, and according to their FAQ ( http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/efaq.html ), it is not considered cryptographically secure.

 

@MyEarth:

Is there a specific reason you want only ascii letters?  I ask because in my mind, the most expedient route would be to use _Crypt_GenRandom to generate a given amount of bytes, and then I'd turn that into Base64 - which consists of all printable characters.  Granted your password would include 0->9 along with + and /.

If the extra characters are unacceptable, then I'd simply generate a bit too much data, and toss out the unacceptable characters.


How's my riding? Dial 1-800-Wait-There

Trying to use a computer with McAfee installed is like trying to read a book at a rock concert.

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