Skip to content

Instantly share code, notes, and snippets.

@heiny
Last active February 18, 2025 06:52
Show Gist options
  • Select an option

  • Save heiny/e45ea0ff0ad8e5c8d107eee665fbacef to your computer and use it in GitHub Desktop.

Select an option

Save heiny/e45ea0ff0ad8e5c8d107eee665fbacef to your computer and use it in GitHub Desktop.

Revisions

  1. heiny revised this gist Aug 20, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion EncryptDecryptRDCMan.ps1
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # There is no facility to replace passwords in RDCMan once they are stored. The only way is to create a new custom credential.
    # If you open your *.rdg file in a text editor, locate the stored <password>, you can then decrypt it using this script.
    # You can also use this script to encrypt a plain text password in the correct format, and overwrite an existing one in the xml in order to update it.
    # This script can also encrypt a plain text password in rdg format which can be used to overwrite an existing one in the xml.
    Add-Type -AssemblyName System.Security;

    Function EncryptPassword {
  2. heiny created this gist Aug 20, 2018.
    54 changes: 54 additions & 0 deletions EncryptDecryptRDCMan.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    # There is no facility to replace passwords in RDCMan once they are stored. The only way is to create a new custom credential.
    # If you open your *.rdg file in a text editor, locate the stored <password>, you can then decrypt it using this script.
    # You can also use this script to encrypt a plain text password in the correct format, and overwrite an existing one in the xml in order to update it.
    Add-Type -AssemblyName System.Security;

    Function EncryptPassword {
    [CmdletBinding()]
    param([String]$PlainText = $null)

    # convert to RDCMan format: (null terminated chars)
    $withPadding = @()
    foreach($char in $PlainText.ToCharArray()) {
    $withPadding += [int]$char
    $withPadding += 0
    }

    # encrypt with DPAPI (current user)
    $encrypted = [Security.Cryptography.ProtectedData]::Protect($withPadding, $null, 'CurrentUser')
    return $base64 = [Convert]::ToBase64String($encrypted)
    }

    Function DecryptPassword {
    [CmdletBinding()]
    param([String]$EncodedPasswordString = $null)

    $decoded = [Convert]::FromBase64String($EncodedPasswordString)
    $decryptedBytes = [Security.Cryptography.ProtectedData]::Unprotect($decoded, $null, 'CurrentUser')
    $decryptedString = [Text.Encoding]::ASCII.GetString($decryptedBytes)

    # trim null terminating chars from padding (does not account for pwds with spaces in them)
    $sb = [System.Text.StringBuilder]::new()
    foreach($char in $decryptedString.ToCharArray()) {
    if($char -ne 0) {
    $sb.Append($char) > $null
    }
    }
    return $sb.ToString()
    }

    # round trip test
    $plainText = 'AllYourPasswordsAreBelongToUs'

    # encrypt
    $encrypted = EncryptPassword($plainText)
    Write-Host "Encrypted Base64 Encoded PWD: $encrypted"

    # decrypt
    $decrypted = DecryptPassword($encrypted)
    Write-Host "Decrypted PWD: $decrypted"

    # assert equality
    if($plainText -ne $decrypted) {
    Write-Error "Round trip failed!"
    }