Skip to content

Instantly share code, notes, and snippets.

@NickVanderPyle
Created October 20, 2016 17:39
Show Gist options
  • Save NickVanderPyle/f82711ddcb4a131e47fd429d9151045b to your computer and use it in GitHub Desktop.
Save NickVanderPyle/f82711ddcb4a131e47fd429d9151045b to your computer and use it in GitHub Desktop.
Powershell module to do machine-specific encryption.
Add-Type -AssemblyName "System.Security"
function ConvertTo-EncryptedString{
[cmdletbinding()]
[OutputType([string])]
param(
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ValidateNotNull()]
[string]$stringToEncrypt)
Process{
[System.Reflection.Assembly]::LoadWithPartialName("System.Security")
$bytesToEncrypt = [System.Text.Encoding]::UTF8.GetBytes($stringToEncrypt)
$encryptedBytes = [System.Security.Cryptography.ProtectedData]::Protect(
$bytesToEncrypt,
$null,
[System.Security.Cryptography.DataProtectionScope]::LocalMachine)
[System.Convert]::ToBase64String($encryptedBytes)
}
}
function ConvertTo-DecryptedString{
[cmdletbinding()]
[OutputType([string])]
param(
[parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ValidateNotNull()]
[string]$stringToDecrypt)
Process{
$bytesToDecrypt = [System.Convert]::FromBase64String($stringToDecrypt)
$decryptedBytes = [System.Security.Cryptography.ProtectedData]::Unprotect(
$bytesToDecrypt,
$null,
[System.Security.Cryptography.DataProtectionScope]::LocalMachine)
[System.Text.Encoding]::UTF8.GetString($decryptedBytes)
}
}
Export-ModuleMember -Function 'ConvertTo-*'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment