functions/ConvertFrom-EncryptedString.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Function ConvertFrom-EncryptedString {
    <#
    .SYNOPSIS
    Converts an encrypted string to a SecureString object.
     
    .DESCRIPTION
    Converts an encrypted string to a SecureString object with an option to use a custom key.
     
    Key path can be defined by:
    PS> Get/Set-DBODefaultSetting -Name security.encryptionkey
 
    Custom key is enforced in a Unix environment by a default setting security.usecustomencryptionkey
    PS> Get/Set-DBODefaultSetting -Name security.usecustomencryptionkey
    
    .PARAMETER String
    String to be decrypted
 
    .EXAMPLE
    # Converts a password provided by user to an encrypted string
    $encrypted = ConvertTo-EncryptedString -String (Read-Host -AsSecureString)
    $decrypted = ConvertFrom-EncryptedString -String $encrypted
     
    .NOTES
     
    #>

    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline, Mandatory)]
        [String]$String
    )
    $params = @{ String = $String }
    if (Get-DBODefaultSetting -Name security.usecustomencryptionkey -Value) {
        $key = Get-EncryptionKey
        if ($null -eq $key) {
            Stop-PSFFunction -Message "Encryption key not found" -EnableException $true
        }
        $params += @{ Key = Get-EncryptionKey }
    }
    try {
        ConvertTo-SecureString @params -ErrorAction Stop
    }
    catch {
        Stop-PSFFunction -Message "Failed to decrypt the secure string" -ErrorRecord $_ -EnableException $true
    }
}