jakoby-b64.psm1

function b64 {

<#
        .SYNOPSIS
        Used to encode or decode from base64
 
        .DESCRIPTION
        Encodes either a string or file to base64
        Decodes either a string or file to base64
 
        .PARAMETER encString
        Encodes string in base64 format
 
        .PARAMETER encFile
        Encodes a file in base64 format
 
        .PARAMETER decString
        Decodes string in base64 format
 
        .PARAMETER decFile
        Encodes a file in base64 format
 
        .OUTPUTS
        " | clip" used to send output to the clipboard
 
        .EXAMPLE
        PS> B64 -encString 'start notepad'
        'cwB0AGEAcgB0ACAAbgBvAHQAZQBwAGEAZAA='
 
        .EXAMPLE
        PS> B64 -encFile "C:\Users\User\Desktop\example.txt"
        'cwB0AGEAcgB0ACAAbgBvAHQAZQBwAGEAZAA='
 
        .EXAMPLE
        PS> B64 -decString 'cwB0AGEAcgB0ACAAbgBvAHQAZQBwAGEAZAA='
        'start notepad'
 
        .EXAMPLE
        PS> B64 -decFile "C:\Users\User\Desktop\example.txt"
        'start notepad'
 
        .LINK
        GitHub reference: https://github.com/I-Am-Jakoby/PowerShell-for-Hackers/blob/main/Functions/B64.md
       
        .LINK
        YouTube tutorial: https://www.youtube.com/watch?v=RmhzrCXWVsY
#>


[CmdletBinding(DefaultParameterSetName="encFile")]
param(
    [Parameter(Position=0, ParameterSetName="encFile")]
    [Alias("ef")]
    [string]$encFile,

    [Parameter(Position=0, ParameterSetName="encString")]
    [Alias("es")]
    [string]$encString,

    [Parameter(Position=0, ParameterSetName="decFile")]
    [Alias("df")]
    [string]$decFile,

    [Parameter(Position=0, ParameterSetName="decString")]
    [Alias("ds")]
    [string]$decString

)

if ($psCmdlet.ParameterSetName -eq "encFile") {
        $encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $encFile -Raw -Encoding UTF8)))
        return $encoded
        }

elseif ($psCmdlet.ParameterSetName -eq "encString") {
        $encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($encString))
        return  $encoded
        }

elseif ($psCmdlet.ParameterSetName -eq "decFile") {
        $data = Get-Content $decFile
        $decoded = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($data))
        return $decoded        
        }

elseif ($psCmdlet.ParameterSetName -eq "decString") {        
        $decoded = [System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($decString))
        return $decoded
        }
}