Created
October 20, 2016 17:39
-
-
Save NickVanderPyle/f82711ddcb4a131e47fd429d9151045b to your computer and use it in GitHub Desktop.
Powershell module to do machine-specific encryption.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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