locked
arrary values into read-host and output is array values RRS feed

  • Question

  • First of all thank you guys for taking the time to answer my question, I am actually curious to know whether this is possible in PowerShell and how doable this project is.

    so lets say I provide a custom value to each letter of the alphabet (a-z) , the value of each letter is 3 characters for each letter (lowercase and uppercase) 

    $a = m3Q

    $A = xeR

    $b = ToH

    then I put this on an array 

    $array = @("$a", "$A", "$b")

    how would I go about using this for text substation? what I'm trying to achieve is to create a form of a cipher with PowerShell by hard coding the values into each letter of the alphabet and able to encrypt the text so is unreadable. in my case I have all letters and numbers with each character with a random value however I still cannot think of a way to use this array as input and output. if using input substitution is not possible I think I can look into python for something like this however I think PowerShell can do this.

    I am also aware of using different types of encryption in PowerShell like AES, but what if I want to disregard that and create my own?

    if anybody can point me in the right direction so I can break my head with this task I'll gladly appreciate it. 

    Sunday, August 18, 2019 2:58 AM

Answers

  • Keith is right.  PowerShell is case insensitive, so you'll need to use separate hash tables for lower and uppercase.   The code below will handle the input aspect, implementing the same concept in reverse should handle the output.

    Hope this helps ~

    $cipherL = @{'a' = 'm3Q'; 'b' = 'xeR'}
    $cipherU = @{'A' = 'ToH'; 'B' = 'Q7i'}

    function Encrypt-Password (
        [string]$Password
    )
    {
        $i = 0; $encrypt = @() 

        do
        {
            $char = $Password[$i]
            if($char -match '[A-Z]') {$encrypt += ($cipherU.GetEnumerator() | ? {$_.Key -eq $char}).Value}
            else {$encrypt += ($cipherL.GetEnumerator() | ? {$_.Key -eq $char}).Value}

            $i++
        } while ($i -lt $Password.Length)

        $encrypt -Join ''
    }

    Encrypt-Password -Password 'AabB'

    OUTPUT: ToHm3QxeRQ7i


        



    • Edited by Gregg71000 Sunday, August 18, 2019 5:48 AM
    • Marked as answer by DVelasquez05 Sunday, August 18, 2019 7:17 PM
    Sunday, August 18, 2019 4:07 AM

All replies

  • You want to start by learning about hashtables: About Hash Tables

    You may end up needing two, as key names are case-insensitive


    Keith


    Sunday, August 18, 2019 3:40 AM
  • Keith is right.  PowerShell is case insensitive, so you'll need to use separate hash tables for lower and uppercase.   The code below will handle the input aspect, implementing the same concept in reverse should handle the output.

    Hope this helps ~

    $cipherL = @{'a' = 'm3Q'; 'b' = 'xeR'}
    $cipherU = @{'A' = 'ToH'; 'B' = 'Q7i'}

    function Encrypt-Password (
        [string]$Password
    )
    {
        $i = 0; $encrypt = @() 

        do
        {
            $char = $Password[$i]
            if($char -match '[A-Z]') {$encrypt += ($cipherU.GetEnumerator() | ? {$_.Key -eq $char}).Value}
            else {$encrypt += ($cipherL.GetEnumerator() | ? {$_.Key -eq $char}).Value}

            $i++
        } while ($i -lt $Password.Length)

        $encrypt -Join ''
    }

    Encrypt-Password -Password 'AabB'

    OUTPUT: ToHm3QxeRQ7i


        



    • Edited by Gregg71000 Sunday, August 18, 2019 5:48 AM
    • Marked as answer by DVelasquez05 Sunday, August 18, 2019 7:17 PM
    Sunday, August 18, 2019 4:07 AM
  • Thank you!, I will give this a try.
    Sunday, August 18, 2019 3:23 PM
  • Sounds good... also I just realized that you might want to change the $encrypt -Join '' to just $encrypt.  This will return the array instead of a joined string.
    Sunday, August 18, 2019 4:54 PM
  • Keith is right.  PowerShell is case insensitive, so you'll need to use separate hash tables for lower and uppercase.   The code below will handle the input aspect, implementing the same concept in reverse should handle the output.

    Hope this helps ~

    $cipherL = @{'a' = 'm3Q'; 'b' = 'xeR'}
    $cipherU = @{'A' = 'ToH'; 'B' = 'Q7i'}

    function Encrypt-Password (
        [string]$Password
    )
    {
        $i = 0; $encrypt = @() 

        do
        {
            $char = $Password[$i]
            if($char -match '[A-Z]') {$encrypt += ($cipherU.GetEnumerator() | ? {$_.Key -eq $char}).Value}
            else {$encrypt += ($cipherL.GetEnumerator() | ? {$_.Key -eq $char}).Value}

            $i++
        } while ($i -lt $Password.Length)

        $encrypt -Join ''
    }

    Encrypt-Password -Password 'AabB'

    OUTPUT: ToHm3QxeRQ7i


        



    Thank you for your input!, this is what I was having a hard time figuring out, definitely will try this on reverse to see the results but I should be able to come up with what I need.

    thanks again!

    Sunday, August 18, 2019 7:17 PM
  • Ok, glad to help - gl
    Sunday, August 18, 2019 9:07 PM