How to resolve the algorithm Vigenère cipher step by step in the PowerShell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Vigenère cipher step by step in the PowerShell programming language

Table of Contents

Problem Statement

Implement a   Vigenère cypher,   both encryption and decryption. The program should handle keys and text of unequal length, and should capitalize everything and discard non-alphabetic characters. (If your program handles non-alphabetic characters in another way, make a note of it.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Vigenère cipher step by step in the PowerShell programming language

Source code in the powershell programming language

# Author: D. Cudnohufsky
function Get-VigenereCipher
{
    Param
    (
        [Parameter(Mandatory=$true)]
        [string] $Text,
 
        [Parameter(Mandatory=$true)]
        [string] $Key,
 
        [switch] $Decode
    )
 
    begin
    {    
        $map = [char]'A'..[char]'Z'
    }
 
    process
    {
        $Key = $Key -replace '[^a-zA-Z]',''
        $Text = $Text -replace '[^a-zA-Z]',''

        $keyChars = $Key.toUpper().ToCharArray()
        $Chars = $Text.toUpper().ToCharArray()
 
        function encode
        {

            param
            (
                $Char,
                $keyChar,
                $Alpha = [char]'A'..[char]'Z'
            )

            $charIndex = $Alpha.IndexOf([int]$Char)
            $keyIndex = $Alpha.IndexOf([int]$keyChar)
            $NewIndex = ($charIndex + $KeyIndex) - $Alpha.Length
            $Alpha[$NewIndex]

        }
 
        function decode
        {

            param
            (
                $Char,
                $keyChar,
                $Alpha = [char]'A'..[char]'Z'
            )

            $charIndex = $Alpha.IndexOf([int]$Char)
            $keyIndex = $Alpha.IndexOf([int]$keyChar)
            $int = $charIndex - $keyIndex
            if ($int -lt 0) { $NewIndex = $int + $Alpha.Length }
            else { $NewIndex = $int }
            $Alpha[$NewIndex]
        }

        while ( $keyChars.Length -lt $Chars.Length ) 
        {
            $keyChars = $keyChars + $keyChars
        }

        for ( $i = 0; $i -lt $Chars.Length; $i++ )
        {

            if ( [int]$Chars[$i] -in $map -and [int]$keyChars[$i] -in $map )
            {
                if ($Decode) {$Chars[$i] = decode $Chars[$i] $keyChars[$i] $map}
                else {$Chars[$i] = encode $Chars[$i] $keyChars[$i] $map}

                $Chars[$i] = [char]$Chars[$i]
                [string]$OutText += $Chars[$i]
            }

        }
 
        $OutText
        $OutText = $null
    }
}


  

You may also check:How to resolve the algorithm Vector products step by step in the Mercury programming language
You may also check:How to resolve the algorithm McNuggets problem step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the F# programming language
You may also check:How to resolve the algorithm Circles of given radius through two points step by step in the Raku programming language
You may also check:How to resolve the algorithm Magic 8-ball step by step in the Fortran programming language