Skip to content

Instantly share code, notes, and snippets.

@xabim
Created June 5, 2018 14:54
Show Gist options
  • Save xabim/1c1fca2a6629559ce2f8e8a106322688 to your computer and use it in GitHub Desktop.
Save xabim/1c1fca2a6629559ce2f8e8a106322688 to your computer and use it in GitHub Desktop.
Encrypt a plain text with certificate in powershell with CMS
$Cert = Get-ChildItem Cert:\LocalMachine\My | Where-Object {$_.Subject -like 'CN=Script Automation'}
$EncryptedPwd = @'
-----BEGIN CMS-----
MIIBqAYJKoZIhvcNAQcDoIIBmTCCAZUCAQAxggFQMIIBTAIBADA0MCAxHjAcBgNVBAMMFVBvd2Vy
HrgQ84JxSV7RYpwSMPJRuSXlgVubOIew8KsYXr/E8kd/wOyT/2NPi3d+4xb67CLUM4infqOrt9Q+
ReAtINvfVB5EPc9wU8yDpdz+WKProT4RJ94nzGH5qW5SK4O1Siu0VSPJZaCNb+CmYNFNNvLu6MN4
-----END CMS-----
'@
$DecryptedPwd = $EncryptedPwd | Unprotect-CmsMessage -To $Cert | ConvertTo-SecureString -AsPlainText -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment