DataEncode.psm1

<#
    ===========================================================================
     Created by: Rhys M
     Contact: RhysM.PS@gmail.com
     PS Gallery: https://www.powershellgallery.com/profiles/RhysM/
 
     Filename: DataEncode.psm1
    -------------------------------------------------------------------------
     Module Name: DataEncode
    ===========================================================================
#>


<#
    .EXTERNALHELP DataEncode.psm1-Help.xml
#>

Function DataEncode-CSVEncode
{
    
    Param (
        [Parameter(Mandatory = $True, ValueFromPipeline = $true, HelpMessage = "CSV file(s) you are encoding")]
        $CSVFile
    )
    
    Foreach ($FileForProcessing in $CSVFile)
    {
        $EncodedOutput = ($FileForProcessing + ".Encoded")
        $CSV = Import-Csv $CSVFile
        # ===== INITIALISE HEADERS ENCRYPT CONTENT ===== #
        $GLOBAL:ArrayHeaders = (Get-Content $CSVFile | Select-Object -First 1).Split(",")
        $GLOBAL:ArrayHeaders_Output = @()
        foreach ($Header in $GLOBAL:ArrayHeaders)
        {
            $Enc_HeaderOutput_Stage1 = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Header))
            $Enc_HeaderOutput_Stage2 = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Enc_HeaderOutput_Stage1))
            $Enc_HeaderOutput_Stage3 = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Enc_HeaderOutput_Stage2))
            $Enc_HeaderOutput_Stage4 = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Enc_HeaderOutput_Stage3))
            $Enc_HeaderOutput_Stage5 = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Enc_HeaderOutput_Stage4))
            $Enc_HeaderOutput_Stage6 = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Enc_HeaderOutput_Stage5))
            $GLOBAL:ArrayHeaders_Output += $Enc_HeaderOutput_Stage6
        }
        # ===== INITIALISE HEADERS ENCRYPT CONTENT ===== #
        
        function BeginEncode
        {
            $Encoded_Stage1 = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($ComponentEntry.$Component))
            $Encoded_Stage2 = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Encoded_Stage1))
            $Encoded_Stage3 = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Encoded_Stage2))
            $Encoded_Stage4 = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Encoded_Stage3))
            $Encoded_Stage5 = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Encoded_Stage4))
            $Encoded_Stage6 = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($Encoded_Stage5))
            write-host "Currently Processing: "
            write-host $ComponentEntry.$Component -ForegroundColor Green
            write-host "Encoded Version: "
            write-host $Encoded_Stage6 -ForegroundColor Magenta
            write-host ""
            # Output Encrypted Content Here
            $ComponentEntry.$Component = $Encoded_Stage6
        }
        
        write-host "Total Component Headers: " -ForegroundColor Green
        $GLOBAL:ArrayHeaders.count
        [array]$PSObjectProcessing = $NULL
        
        foreach ($Component in $GLOBAL:ArrayHeaders)
        {
            $VARTEST = Test-Path variable:Header_$Component
            If ($VARTEST -eq $True)
            {
                
                write-host "Variable presently exists - removing variable and re creating for utilisation" -ForegroundColor Cyan
                Remove-Variable "Header_$Component" -ErrorAction SilentlyContinue | Out-Null
                Remove-Variable "Value_$Component" -ErrorAction SilentlyContinue | out-null
                write-host "Variable Removed." -ForegroundColor Green
                New-Variable -Name "Header_$Component" -Value $Component -Scope global -ErrorAction SilentlyContinue | Out-Null
                write-host "New Global Variable Created: " -ForegroundColor Magenta
                write-host "Header_$Component"
                $RequiredComponent = $CSV | select -ExpandProperty "$Component"
                New-Variable -Name "Value_$Component" -Value $RequiredComponent
                write-host "New Value Variable Created: " -ForegroundColor Cyan
                write-host "Value_$Component"
                $ComponentCall = Get-Variable -Name "Value_$Component" -ValueOnly
                foreach ($ComponentEntry in $CSV)
                {
                    # Initiate Encryption String Here
                    BeginEncode
                }
                $CSV | Export-CSV $EncodedOutput -delimiter "," -notypeinformation
                $HeaderArrayJoined = ($GLOBAL:ArrayHeaders_Output) -join ","
                $temp = Get-Content $EncodedOutput
                $temp[0] = $temp[0].replace($temp[0], "$HeaderArrayJoined")
                Set-Content -Path $EncodedOutput -Value $temp
            }
            else
            {
                write-host "Variable does not exist - creating global variable" -ForegroundColor Green
                New-Variable -Name "Header_$Component" -Value $Component -Scope global
                write-host "New Global Variable Created: " -ForegroundColor Magenta
                write-host "Header_$Component"
                $RequiredComponent = $CSV | select -ExpandProperty "$Component"
                New-Variable -Name "Value_$Component" -Value $RequiredComponent
                write-host "New Value Variable Created: " -ForegroundColor Magenta
                write-host "Value_$Component"
                $ComponentCall = Get-Variable -Name "Value_$Component" -ValueOnly
                foreach ($ComponentEntry in $CSV)
                {
                    # Initiate Encryption String Here
                    BeginEncode
                }
                $CSV | Export-CSV $EncodedOutput -delimiter "," -notypeinformation
                $HeaderArrayJoined = ($GLOBAL:ArrayHeaders_Output) -join ","
                $temp = Get-Content $EncodedOutput
                $temp[0] = $temp[0].replace($temp[0], "$HeaderArrayJoined")
                Set-Content -Path $EncodedOutput -Value $temp
            }
        }
        # ========== Initial Encoding Complete ========== #
    }
}

<#
    .EXTERNALHELP DataEncode.psm1-Help.xml
#>

Function DataEncode-CSVDecode
{
    
    Param (
        [Parameter(Mandatory = $True, ValueFromPipeline = $true, HelpMessage = "CSV file(s) you are decoding")]
        $CSVFile
    )
    
    Foreach ($FileForProcessing in $CSVFile)
    {
        $EncodedOutput = ($FileForProcessing + ".Decoded")
        $CSV = Import-Csv $CSVFile
        # ===== INITIALISE HEADERS DECRYPT CONTENT ===== #
        $GLOBAL:ArrayHeaders = (Get-Content $CSVFile | Select-Object -First 1).Split(",")
        $GLOBAL:ArrayHeaders_Output = @()
        foreach ($Header in $GLOBAL:ArrayHeaders)
        {
            $Enc_HeaderOutput_Stage1 = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Header))
            $Enc_HeaderOutput_Stage2 = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Enc_HeaderOutput_Stage1))
            $Enc_HeaderOutput_Stage3 = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Enc_HeaderOutput_Stage2))
            $Enc_HeaderOutput_Stage4 = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Enc_HeaderOutput_Stage3))
            $Enc_HeaderOutput_Stage5 = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Enc_HeaderOutput_Stage4))
            $Enc_HeaderOutput_Stage6 = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Enc_HeaderOutput_Stage5))
            $GLOBAL:ArrayHeaders_Output += $Enc_HeaderOutput_Stage6
        }
        # ===== INITIALISE HEADERS DECRYPT CONTENT ===== #
        
        Function BeginDecode
        {
            $Decoded_Stage1 = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($ComponentEntry.$Component))
            $Decoded_Stage2 = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Decoded_Stage1))
            $Decoded_Stage3 = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Decoded_Stage2))
            $Decoded_Stage4 = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Decoded_Stage3))
            $Decoded_Stage5 = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Decoded_Stage4))
            $Decoded_Stage6 = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Decoded_Stage5))
            write-host "Currently Processing: "
            write-host $ComponentEntry.$Component -ForegroundColor Green
            write-host "Decoded Version: "
            write-host $Decoded_Stage6 -ForegroundColor Magenta
            write-host ""
            # Output Decoded Content Here
            $ComponentEntry.$Component = $Decoded_Stage6
        }
        
        write-host "Total Component Headers: " -ForegroundColor Green
        $GLOBAL:ArrayHeaders.count
        [array]$PSObjectProcessing = $NULL
        
        foreach ($Component in $GLOBAL:ArrayHeaders)
        {
            $VARTEST = Test-Path variable:Header_$Component
            If ($VARTEST -eq $True)
            {
                
                write-host "Variable presently exists - removing variable and re creating for utilisation" -ForegroundColor Cyan
                Remove-Variable "Header_$Component" -ErrorAction SilentlyContinue | Out-Null
                Remove-Variable "Value_$Component" -ErrorAction SilentlyContinue | out-null
                write-host "Variable Removed." -ForegroundColor Green
                New-Variable -Name "Header_$Component" -Value $Component -Scope global -ErrorAction SilentlyContinue | Out-Null
                write-host "New Global Variable Created: " -ForegroundColor Magenta
                write-host "Header_$Component"
                $RequiredComponent = $CSV | select -ExpandProperty "$Component"
                New-Variable -Name "Value_$Component" -Value $RequiredComponent
                write-host "New Value Variable Created: " -ForegroundColor Cyan
                write-host "Value_$Component"
                $ComponentCall = Get-Variable -Name "Value_$Component" -ValueOnly
                foreach ($ComponentEntry in $CSV)
                {
                    # Initiate Decoded String Here
                    BeginDecode
                }
                $CSV | Export-CSV $EncodedOutput -delimiter "," -notypeinformation
                $HeaderArrayJoined = ($GLOBAL:ArrayHeaders_Output) -join ","
                $temp = Get-Content $EncodedOutput
                $temp[0] = $temp[0].replace($temp[0], "$HeaderArrayJoined")
                Set-Content -Path $EncodedOutput -Value $temp
            }
            else
            {
                write-host "Variable does not exist - creating global variable" -ForegroundColor Green
                New-Variable -Name "Header_$Component" -Value $Component -Scope global
                write-host "New Global Variable Created: " -ForegroundColor Magenta
                write-host "Header_$Component"
                $RequiredComponent = $CSV | select -ExpandProperty "$Component"
                New-Variable -Name "Value_$Component" -Value $RequiredComponent
                write-host "New Value Variable Created: " -ForegroundColor Magenta
                write-host "Value_$Component"
                $ComponentCall = Get-Variable -Name "Value_$Component" -ValueOnly
                foreach ($ComponentEntry in $CSV)
                {
                    # Initiate Decoded String Here
                    BeginDecode
                }
                $CSV | Export-CSV $EncodedOutput -delimiter "," -notypeinformation
                $HeaderArrayJoined = ($GLOBAL:ArrayHeaders_Output) -join ","
                $temp = Get-Content $EncodedOutput
                $temp[0] = $temp[0].replace($temp[0], "$HeaderArrayJoined")
                Set-Content -Path $EncodedOutput -Value $temp
            }
        }
        # ========== Initial Encoding Complete ========== #
    }
}


Export-ModuleMember -Function DataEncode-CSVEncode,
                    DataEncode-CSVDecode