How to create a password?

You can talk about anything related to LB Booster here, not covered in another category
sarmednafi
Posts: 43
Joined: Fri Apr 06, 2018 6:27 am

How to create a password?

Post by sarmednafi »

Hi everybody,

I want to use an encryption program to encrypt a serial Numbers using a certain seed to generate a password.
Is that correct?
Or am I going in the wrong direction?

Please advise.
xxgeek
Posts: 17
Joined: Tue Nov 28, 2023 1:36 pm

Re: How to create a password?

Post by xxgeek »

Not an answer, although you may find something to help at the JB forums under the "Code Discussion" section.

xcoder has been writing encryption code for quite a while using a few different methods.

https://justbasiccom.proboards.com/boar ... iscussions
sarmednafi
Posts: 43
Joined: Fri Apr 06, 2018 6:27 am

Re: How to create a password?

Post by sarmednafi »

Thankyou Geek,

Sarmed
xxgeek
Posts: 17
Joined: Tue Nov 28, 2023 1:36 pm

Re: How to create a password?

Post by xxgeek »

Did you get an answer to your question there?

Not exactly sure what you're trying to do.
sarmednafi
Posts: 43
Joined: Fri Apr 06, 2018 6:27 am

Re: How to create a password?

Post by sarmednafi »

Hi Geek,

Actually, it is not easy to find instructions on how to create a password, but what I expect is some guidance.

The most important thing is to find an encryption code that gives results in text and numbers, not Chinese characters, as I have seen elsewhere.

Moreover, the code must be simple and short, not filled with endless functions. For me, the best thing that comes to mind is to simulate the Enigma Machine. The code will have arrays and variables, without Random, Randomize, etc.

This is all I can say for now.

Thank you very much for your help, Geek.
xxgeek
Posts: 17
Joined: Tue Nov 28, 2023 1:36 pm

Re: How to create a password?

Post by xxgeek »

Here is a short bit of code to create passwords, but does use random letters and numbers from a variable.

Code: Select all

    nomainwin

    WindowWidth = 500
    WindowHeight = 220
    statictext #main.l1, "Password length:", 15, 30, 150, 25
    textbox #main.len, 165, 27, 80, 25
    button #main.gen, "Generate", [generate], UL, 270, 27, 100, 30
    statictext #main.l2, "Password:", 65, 80, 100, 25
    textbox #main.pw, 165, 77, 300, 25
    open "Password Generator" for window as #main
    print #main, "trapclose [quit]"
    print #main.len, "16"
    #main "font arial 12 bold"
wait

[generate]
    print #main.len, "!contents? length$"
    length = val(length$)
    password$ = ""
    characters$ = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    for i = 1 to length
        position = int(rnd(0) * len(characters$)) + 1
        password$ = password$ + mid$(characters$, position, 1)
    next i
    print #main.pw, password$
wait

[quit]
    close #main
    end
 
Another more elaborate way taking advantage of Windows Powershell .net) capability'
Both ways might give you some ideas.

Code: Select all

     nomainwin
    global scriptPath$
    global passwordPath$
    global resultPath$
    global storedPath$
    global operation$
    global checkCount
    scriptPath$ = DefaultDir$ + "\PasswordHash.ps1"
    passwordPath$ = DefaultDir$ + "\PasswordInput.txt"
    resultPath$ = DefaultDir$ + "\PasswordResult.txt"
    storedPath$ = DefaultDir$ + "\StoredPasswordHash.txt"

    call MakePowerShellScript

    WindowWidth = 650
    WindowHeight = 360
    stylebits #main.pw, _ES_PASSWORD, 0, 0, 0
    textbox #main.pw, 120, 25, 450, 25
    statictext #main.lab, "Password:", 25, 29, 85, 20
    button #main.hash, "Create Hash", [CreateHash], UL, 25, 70, 125, 30
    button #main.verify, "Verify Password", [VerifyPassword], UL, 165, 70, 145, 30
    statictext #main.reslab, "Result:", 25, 120, 85, 20
    texteditor #main.result, 25, 145, 545, 125
    open "PBKDF2 Password Hash" for window as #main
    print #main, "trapclose [Quit]"
    wait

[CreateHash]
    print #main.pw, "!contents? password$"
    if password$ = "" then
        print #main.result, "!cls"
        print #main.result, "Enter a password first."
        wait
    end if
    open passwordPath$ for output as #passwordFile
    print #passwordFile, password$
    close #passwordFile
    open resultPath$ for output as #resultFile
    close #resultFile
    operation$ = "HASH"
    checkCount = 0
    command$ = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File "
    command$ = command$ + chr$(34)
    command$ = command$ + scriptPath$
    command$ = command$ + chr$(34)
    command$ = command$ + " HASH"

    run command$, HIDE
    timer 100, [CheckResult]
    wait

[VerifyPassword]
    print #main.pw, "!contents? password$"
    if password$ = "" then
        print #main.result, "!cls"
        print #main.result, "Enter a password first."
        wait
    end if
    open passwordPath$ for output as #passwordFile
    print #passwordFile, password$
    close #passwordFile
    open resultPath$ for output as #resultFile
    close #resultFile
    operation$ = "VERIFY"
    checkCount = 0
    command$ = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File "
    command$ = command$ + chr$(34)
    command$ = command$ + scriptPath$
    command$ = command$ + chr$(34)
    command$ = command$ + " VERIFY"
    run command$, HIDE
    timer 100, [CheckResult]
    wait

[CheckResult]
    checkCount = checkCount + 1
    open resultPath$ for input as #resultFile
    resultSize = lof(#resultFile)
    if resultSize = 0 then
        close #resultFile
        if checkCount >= 100 then
            timer 0
            print #main.result, "!cls"
            print #main.result, "PowerShell did not return a result."
            wait
        end if
        timer 100, [CheckResult]
        wait
    end if
    result$ = input$(#resultFile, resultSize)
    close #resultFile
    timer 0
    print #main.result, "!cls"
    if operation$ = "HASH" then
        print #main.result, "Password hash:"
        print #main.result, ""
        print #main.result, result$
    end if
    if operation$ = "VERIFY" then
        print #main.result, result$
    end if
    wait

[Quit]
    timer 0
    close #main
    end

sub MakePowerShellScript
    open scriptPath$ for output as #ps
    print #ps, "$ErrorActionPreference = 'Stop'"
    print #ps, "$passwordFile = '" + passwordPath$ + "'"
    print #ps, "$resultFile = '" + resultPath$ + "'"
    print #ps, "$storedFile = '" + storedPath$ + "'"
    print #ps, "$iterations = 600000"
    print #ps, "$saltLength = 16"
    print #ps, "$hashLength = 32"
    print #ps, ""
    print #ps, "function ToHex([byte[]] $bytes)"
    print #ps, "{"
    print #ps, "    return ([BitConverter]::ToString($bytes) -replace '-', '').ToLower()"
    print #ps, "}"
    print #ps, ""
    print #ps, "function FromHex([string] $hex)"
    print #ps, "{"
    print #ps, "    $bytes = New-Object byte[] ($hex.Length / 2)"
    print #ps, "    for ($i = 0; $i -lt $bytes.Length; $i++)"
    print #ps, "    {"
    print #ps, "        $bytes[$i] = [Convert]::ToByte($hex.Substring($i * 2, 2), 16)"
    print #ps, "    }"
    print #ps, "    return $bytes"
    print #ps, "}"
    print #ps, ""
    print #ps, "function CompareBytes([byte[]] $a, [byte[]] $b)"
    print #ps, "{"
    print #ps, "    if ($a.Length -ne $b.Length)"
    print #ps, "    {"
    print #ps, "        return $false"
    print #ps, "    }"
    print #ps, "    $different = 0"
    print #ps, "    for ($i = 0; $i -lt $a.Length; $i++)"
    print #ps, "    {"
    print #ps, "        $different = $different -bor ($a[$i] -bxor $b[$i])"
    print #ps, "    }"
    print #ps, "    return ($different -eq 0)"
    print #ps, "}"
    print #ps, ""
    print #ps, "try"
    print #ps, "{"
    print #ps, "    $password = [System.IO.File]::ReadAllText($passwordFile)"
    print #ps, "    $password = $password.TrimEnd([char]13, [char]10)"
    print #ps, ""
    print #ps, "    if ($args[0] -eq 'HASH')"
    print #ps, "    {"
    print #ps, "        $salt = New-Object byte[] $saltLength"
    print #ps, "        $rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()"
    print #ps, "        $rng.GetBytes($salt)"
    print #ps, "        $rng.Dispose()"
    print #ps, ""
    print #ps, "        $pbkdf = New-Object System.Security.Cryptography.Rfc2898DeriveBytes("
    print #ps, "            $password,"
    print #ps, "            $salt,"
    print #ps, "            $iterations,"
    print #ps, "            [System.Security.Cryptography.HashAlgorithmName]::SHA256"
    print #ps, "        )"
    print #ps, ""
    print #ps, "        $hash = $pbkdf.GetBytes($hashLength)"
    print #ps, "        $pbkdf.Dispose()"
    print #ps, ""
    print #ps, "        $saltHex = ToHex $salt"
    print #ps, "        $hashHex = ToHex $hash"
    print #ps, ""
    print #ps, "        $stored = 'PBKDF2-SHA256:' + $iterations + ':' + $saltHex + ':' + $hashHex"
    print #ps, ""
    print #ps, "        [System.IO.File]::WriteAllText($storedFile, $stored)"
    print #ps, "        [System.IO.File]::WriteAllText($resultFile, $stored)"
    print #ps, "    }"
    print #ps, ""
    print #ps, "    if ($args[0] -eq 'VERIFY')"
    print #ps, "    {"
    print #ps, "        if (-not (Test-Path $storedFile))"
    print #ps, "        {"
    print #ps, "            [System.IO.File]::WriteAllText($resultFile, 'NO STORED PASSWORD HASH')"
    print #ps, "            Remove-Item $passwordFile -Force -ErrorAction SilentlyContinue"
    print #ps, "            exit"
    print #ps, "        }"
    print #ps, ""
    print #ps, "        $stored = [System.IO.File]::ReadAllText($storedFile)"
    print #ps, "        $parts = $stored.Split(':')"
    print #ps, ""
    print #ps, "        $iterations = [int]$parts[1]"
    print #ps, "        $salt = FromHex $parts[2]"
    print #ps, "        $expected = FromHex $parts[3]"
    print #ps, ""
    print #ps, "        $pbkdf = New-Object System.Security.Cryptography.Rfc2898DeriveBytes("
    print #ps, "            $password,"
    print #ps, "            $salt,"
    print #ps, "            $iterations,"
    print #ps, "            [System.Security.Cryptography.HashAlgorithmName]::SHA256"
    print #ps, "        )"
    print #ps, ""
    print #ps, "        $actual = $pbkdf.GetBytes($expected.Length)"
    print #ps, "        $pbkdf.Dispose()"
    print #ps, ""
    print #ps, "        if (CompareBytes $actual $expected)"
    print #ps, "        {"
    print #ps, "            [System.IO.File]::WriteAllText($resultFile, 'PASSWORD MATCH')"
    print #ps, "        }"
    print #ps, "        else"
    print #ps, "        {"
    print #ps, "            [System.IO.File]::WriteAllText($resultFile, 'PASSWORD DOES NOT MATCH')"
    print #ps, "        }"
    print #ps, "    }"
    print #ps, "}"
    print #ps, "catch"
    print #ps, "{"
    print #ps, "    $message = 'ERROR: ' + $_.Exception.Message"
    print #ps, "    [System.IO.File]::WriteAllText($resultFile, $message)"
    print #ps, "}"
    print #ps, "Remove-Item $passwordFile -Force -ErrorAction SilentlyContinue"
    close #ps
end sub
 
xxgeek
Posts: 17
Joined: Tue Nov 28, 2023 1:36 pm

Re: How to create a password?

Post by xxgeek »

Here is what I believe you are looking for.
No Random, and no RND(). Uses the system timer.

Code: Select all

 ' Example usage: print a 16-character password - can be changed to any number
    print generatePassword$(16)
    end

    function generatePassword$(length)
        ' Define the character pool: lowercase, uppercase, and numbers (62 chars total)
        pool$ = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        poolLen = len(pool$)
        result$ = ""
        ' time$("ms") returns a numeric millisecond value
        ms = time$("ms")
        for i = 1 to length
            ' Mix the changing millisecond count with position-based math
            idx = ((ms + (i * 73) + (i * i * 37)) mod poolLen) + 1
            result$ = result$ + mid$(pool$, idx, 1)
        next i
        generatePassword$ = result$
    end function
sarmednafi
Posts: 43
Joined: Fri Apr 06, 2018 6:27 am

Re: How to create a password?

Post by sarmednafi »

Thank you, Geek,

That's great.
Those ms represent the day ms; it is better to use seconds since last year or the Century. To prevent any chance of repeating numbers.
However, I am diving into translating the ENIGMA MACHINE into code.
Using the emulator on the site:
<https://piotte13.github.io/enigma-cipher/>
The Last tap.

Thank you, Geek
You are wonderful