<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>grokgarble.com &#187; Encryption</title>
	<atom:link href="http://jeffmurr.com/blog/?feed=rss2&#038;tag=encryption" rel="self" type="application/rss+xml" />
	<link>http://jeffmurr.com/blog</link>
	<description>Database, Software, and System Engineering</description>
	<lastBuildDate>Tue, 26 Sep 2017 19:07:40 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.30</generator>
	<item>
		<title>Asymmetric Encryption of Text using x.509 Certificates in PowerShell</title>
		<link>http://jeffmurr.com/blog/?p=228</link>
		<comments>http://jeffmurr.com/blog/?p=228#comments</comments>
		<pubDate>Sat, 07 Dec 2013 18:29:00 +0000</pubDate>
		<dc:creator><![CDATA[Jeff Murr]]></dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://jeffmurr.com/blog/?p=228</guid>
		<description><![CDATA[I&#8217;ve been requested to encryption sections of my script (user name/password) using a certificate issued by a trusted CA.  No problem. To do this in the blog, I&#8217;ll I created a self-signed x.509 certificate and a CN of &#8220;PowerShellCert&#8221;.  I will use the public portion of the certificate in a file that&#8217;s base64 .cer to encrypt the [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been requested to encryption sections of my script (user name/password) using a certificate issued by a trusted CA.  No problem.</p>
<p>To do this in the blog, I&#8217;ll I created a self-signed <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms721636(v=vs.85).aspx#_security_x.509_gly">x.509</a> certificate and a CN of &#8220;PowerShellCert&#8221;.  I will use the public portion of the certificate in a file that&#8217;s base64 .cer to encrypt the sections.  That certificate I&#8217;ll place in the Windows key store and referenced by thumbprint for decryption.  For the actual work, I&#8217;ll import the CA trusted certificate into the same key store, replace the thumbprint with that CA trusted one and make sure the certificate is trusted either by the user or machine the script is running.</p>
<p>Here&#8217;s the command used with the <a href="http://msdn.microsoft.com/en-us/library/bfsktky3(v=vs.110).aspx">makecert </a>utility to create a 2048 length cert:</p>
<pre class="brush:shell">C:\Program Files\Microsoft SDKs\Windows\v7.1&gt;makecert.exe -r -pe -n "CN=PowerShellCert" -ss my -sr localmachine -eku 1.3.6.1.5.5.7.3.2 -len 2048 -e 01/01/2030 C:\Scripts\PowerShell\Asymmetrical-Encryption\PowerShellAsymmetrical.cer</pre>
<p>Here&#8217;s the encryption function:</p>
<pre class="brush:powershell">Function Encrypt-Asymmetric {
	[CmdletBinding()]
	[OutputType([System.String])]
	param(
		[Parameter(Position=0, Mandatory=$true)][ValidateNotNullOrEmpty()][System.String]
		$ClearText,
		[Parameter(Position=1, Mandatory=$true)][ValidateNotNullOrEmpty()][ValidateScript({Test-Path $_ -PathType Leaf})][System.String]
		$PublicCertFilePath
	)
    # Encrypts a string with a public key
    $PublicCert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($PublicCertFilePath)
    $ByteArray = [System.Text.Encoding]::UTF8.GetBytes($ClearText)
	$EncryptedByteArray = $PublicCert.PublicKey.Key.Encrypt($ByteArray,$true)
	$Base64String = [Convert]::ToBase64String($EncryptedByteArray)

	Return $Base64String 
}</pre>
<p>&#8230;here&#8217;s the decryption function:</p>
<pre class="brush:powershell">Function Decrypt-Asymmetric
{
	[CmdletBinding()]
	[OutputType([System.String])]
	param(
		[Parameter(Position=0, Mandatory=$true)][ValidateNotNullOrEmpty()][System.String]
		$EncryptedBase64String,
		[Parameter(Position=1, Mandatory=$true)][ValidateNotNullOrEmpty()][System.String]
		$CertThumbprint
	)
    # Decrypts cipher text using the private key
    # Assumes the certificate is in the LocalMachine\My (Personal) Store
    $Cert = Get-ChildItem cert:\LocalMachine\My | where { $_.Thumbprint -eq $CertThumbprint }
	if($Cert) {
	    $EncryptedByteArray = [Convert]::FromBase64String($EncryptedBase64String)
	    $ClearText = [System.Text.Encoding]::UTF8.GetString($Cert.PrivateKey.Decrypt($EncryptedByteArray,$true))
    }
	Else {Write-Error "Certificate with thumbprint: $CertThumbprint not found!"}

	Return $ClearText
}</pre>
<p>To Encrypt clear text:</p>
<pre class="brush:shell">PS C:\&gt;Encrypt-Asymmetric -ClearText "CLEAR TEXT DATA" -PublicCertFilePath "C:\Scripts\PowerShell\Asymmetrical-Encryption\PowerShellAsymmetricalTest.cer"</pre>
<p>To Decrypt clear text:</p>
<pre>PS C:\&gt;Decrypt-Asymmetric -EncryptedBase64String $Base64String -CertThumbprint "83D2D68907681EF3D823B23DEE24B0CBB3FA3C51"</pre>
<p>To find your Certificate&#8217;s thumbprint (assuming you gave it the same name as I did above):</p>
<pre class="brush:powershell">PS C:\&gt; Get-ChildItem cert:\LocalMachine\My | where { $_.Subject -eq "CN=PowerShellCert" } | select Thumbprint</pre>
<p>Now, encryption keys can be garded under the Windows key store.</p>
<div data-counters='1' data-style='square' data-size='regular' data-url='http://jeffmurr.com/blog/?p=228' data-title='Asymmetric Encryption of Text using x.509 Certificates in PowerShell' class='linksalpha_container linksalpha_app_3'><a href='//www.linksalpha.com/share?network='facebook' class='linksalpha_icon_facebook'></a><a href='//www.linksalpha.com/share?network='twitter' class='linksalpha_icon_twitter'></a><a href='//www.linksalpha.com/share?network='googleplus' class='linksalpha_icon_googleplus'></a><a href='//www.linksalpha.com/share?network='mail' class='linksalpha_icon_mail'></a></div><div data-position='' data-url='http://jeffmurr.com/blog/?p=228' data-title='Asymmetric Encryption of Text using x.509 Certificates in PowerShell' class='linksalpha_container linksalpha_app_7'><a href='//www.linksalpha.com/share?network='facebook' class='linksalpha_icon_facebook'></a><a href='//www.linksalpha.com/share?network='twitter' class='linksalpha_icon_twitter'></a><a href='//www.linksalpha.com/share?network='googleplus' class='linksalpha_icon_googleplus'></a><a href='//www.linksalpha.com/share?network='mail' class='linksalpha_icon_mail'></a></div>
<!-- NgfbSharing::get_buttons content filter skipped: buttons not allowed in rss feeds -->
]]></content:encoded>
			<wfw:commentRss>http://jeffmurr.com/blog/?feed=rss2&#038;p=228</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
