locked
[Tweak needed] - Generate a Complex Password without the number 0 (zero) RRS feed

  • Question

  • Hi there,

    I've been reading this forum a lot, and happens to be the first time I need to post something on my own.

    I've found this very useful piece of code from your script repository and I've been able to tweak it for my needs but for one thing: eliminate the number 0 (zero) from the list of possible list of numbers to be used.

    I know I could read the string and if 0 (zero) is found, reject the password, but would rather understand/find a way how to make it not an option at all to generate the password.

    Thanks a lot in advance for your answers.

    Patrick
    • Edited by P Me Wednesday, July 23, 2014 8:39 PM html code that was not supposed to be there
    Wednesday, July 23, 2014 8:38 PM

Answers

  • Change the following line in the script:

    iAsc = Int(Rnd*10)+48

    To: 

    iAsc = Int(Rnd*9)+49

    The original code generates a random ASCII character value for the numbers 0 to 9 (ASCII 48 to 57). The new code generates a random ASCII character value for the numbers 1 to 9 (ASCII 49 to 57).

    As has been mentioned already, by not allowing zeros you are effectively (slightly) weakening your algorithm for generating complex passwords.


    • Marked as answer by P Me Thursday, July 24, 2014 4:22 PM
    Wednesday, July 23, 2014 10:43 PM

All replies

  • What?  What are you asking? You want to make it not an option to generate a password.  Just don't ask for a password.

    I am afraid you question is missing some valuable bits of information.

    The function just generates a random password.  That is what it does. 


    ¯\_(ツ)_/¯

    Wednesday, July 23, 2014 8:55 PM
  • I think I get it.  You want to automatically reject all passwords that contain  a zero.  Is that right?

    Just test for zero and loop again.


    ¯\_(ツ)_/¯

    Wednesday, July 23, 2014 8:56 PM
  • Why?

    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 23, 2014 9:12 PM
  • Hi jrv,

    I've figured out that as well, but wondered if there would be a smarter way to do that instead of adding a check to discard the generated password and generate a new one until one is found without the 0.

    What I also would like, is to understand how this works a little bit better. I'm used to have dictionaries or arrays that contains the characters I want to use to generate something random, this code is nothing like that.

    @Bill Steward, the reason why is that I need to update a tool made in another language and the 0 was excluded, for some reasons I ignore, but I should generate a random password having the same criteria from the old code, which used arrays of letters and numbers.

    Wednesday, July 23, 2014 9:20 PM
  • What happens if the 0 is included?

    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 23, 2014 9:22 PM
  • If you don't mind PowerShell, you can do this:

    $passwords = @()
    Add-Type -AssemblyName System.Web
    1..10 |ForEach-Object{$Passwords += [System.Web.Security.Membership]::GeneratePassword(10,1)}
    $passwords |Where-Object {$_ -notmatch "0"}

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ('6F6C646B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    Wednesday, July 23, 2014 9:23 PM
  • @Bill Stewart: I don't know that. And I would rather concentrate the effort on the code, rather than in discussions on why/whatif if you don't mind :)
    Wednesday, July 23, 2014 9:39 PM
  • Nothing against PowerShell, but in this case, it's a VB.Net application.

    I keep your code handy anyways in case I need it for other purposes :)

    Thanks

    Wednesday, July 23, 2014 9:41 PM
  • I am trying to point out that you may be trying to write code to solve a non-existent problem.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 23, 2014 9:46 PM
  • And I understand where you come from. I don't have the luxury right now to question the why unfortunately.

    I had to provide something asap, so I went with adding an additional check to invalidate any generated password that contains a 0 inside. I still would like to understand better how the script works in the background and if there is a smarter way to do so.

    Wednesday, July 23, 2014 10:01 PM
  • Hello,

    Since it's a VB.Net application, I'd ask in the Visual Basic .Net Language forum on MSDN.

    This is a scripting forum :)

    Karl


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer.
    My Blog: Unlock PowerShell
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ('6F6C646B61726C406F75746C6F6F6B2E636F6D'-split'(?<=\G.{2})'|%{if($_){[char][int]"0x$_"}})

    Wednesday, July 23, 2014 10:12 PM
  • Change the following line in the script:

    iAsc = Int(Rnd*10)+48

    To: 

    iAsc = Int(Rnd*9)+49

    The original code generates a random ASCII character value for the numbers 0 to 9 (ASCII 48 to 57). The new code generates a random ASCII character value for the numbers 1 to 9 (ASCII 49 to 57).

    As has been mentioned already, by not allowing zeros you are effectively (slightly) weakening your algorithm for generating complex passwords.


    • Marked as answer by P Me Thursday, July 24, 2014 4:22 PM
    Wednesday, July 23, 2014 10:43 PM
  • While this is true, the script itself comes from the script repository of the Scripting Guys and I've followed a link from there that redirected me directly to open a new topic right here.
    Wednesday, July 23, 2014 10:57 PM
  • Thanks a lot. I will test that tomorrow.

    I'm not giving the constraints, just trying to apply them in this case :)

    Wednesday, July 23, 2014 10:59 PM
  • Works great, thanks a lot.
    Thursday, July 24, 2014 4:22 PM