PEMEncrypt.psm1

# Base module contents

function Import-Assemblies {
    Param()
    Begin {
        $dllPath = if ($PSVersionTable.PSVersion.Major -ge 6) {
            [System.IO.Path]::Combine($PSScriptRoot,'bin','netstandard')
        }
        else {
            [System.IO.Path]::Combine($PSScriptRoot,'bin','netfx')
        }
    }
    Process {
        try {
            $bouncyCastleDll = Join-Path $dllPath 'BouncyCastle.Crypto.dll'
            Add-Type -Path $bouncyCastleDll -ErrorAction SilentlyContinue | Out-Null
        }
        catch {
            $Global:Error.Remove($Global:Error[0])
        }
        try {
            $PEMEncrypt = Join-Path $dllPath 'SCRTHQ.PEMEncrypt.dll'
            Add-Type -Path $PEMEncrypt -ReferencedAssemblies $bouncyCastleDll -ErrorAction SilentlyContinue | Out-Null
        }
        catch {
            $Global:Error.Remove($Global:Error[0])
        }
    }
}


function Protect-PEMString {
    [OutputType('System.String')]
    [CmdletBinding()]
    Param (
        [parameter(Mandatory,Position = 0,ValueFromPipeline)]
        [String[]]
        $StringToEncrypt,
        [parameter(Mandatory,Position = 1)]
        [String]
        $PublicKeyPath,
        [parameter(Position = 2)]
        [Int]
        $KeySize = 2048
    )
    Begin {
        Import-Assemblies
    }
    Process {
        foreach ($string in $StringToEncrypt) {
            try {
                [SCRTHQ.PEMEncrypt.Crypto]::Encrypt(
                    $string,
                    ([System.IO.File]::ReadAllText((Resolve-Path $PublicKeyPath).Path)),
                    $KeySize
                )
            }
            catch {
                $PSCmdlet.ThrowTerminatingError($_)
            }
        }
    }
}

Export-ModuleMember -Function 'Protect-PEMString'

function Unprotect-PEMString {
    [OutputType('System.String')]
    [CmdletBinding()]
    Param (
        [parameter(Mandatory,Position = 0,ValueFromPipeline)]
        [String[]]
        $StringToDecrypt,
        [parameter(Mandatory,Position = 1)]
        [String]
        $PrivateKeyPath,
        [parameter(Position = 2)]
        [AllowNull()]
        [SecureString]
        $Password
    )
    Begin {
        Import-Assemblies
    }
    Process {
        foreach ($string in $StringToDecrypt) {
            try {
                if ($PSBoundParameters.ContainsKey('Password')) {
                    [SCRTHQ.PEMEncrypt.Crypto]::Decrypt(
                        $string,
                        ([System.IO.File]::ReadAllText((Resolve-Path $PrivateKeyPath).Path)),
                        (New-Object PSCredential 'user',$Password).GetNetworkCredential().Password
                    )
                }
                else {
                    [SCRTHQ.PEMEncrypt.Crypto]::Decrypt(
                        $string,
                        ([System.IO.File]::ReadAllText((Resolve-Path $PrivateKeyPath).Path))
                    )
                }
            }
            catch {
                $PSCmdlet.ThrowTerminatingError($_)
            }
        }
    }
}

Export-ModuleMember -Function 'Unprotect-PEMString'