-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
ExtractPrivateKey.ps1
18 lines (15 loc) · 987 Bytes
/
ExtractPrivateKey.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Param(
[Parameter(Mandatory=$True,Position=1)]
[string] $pfxPwd
)
[string] $pfxFilePath = ".\src\ServiceFabric.Mocks\ServiceFabric.Mocks.pfx"
[string] $snkFilePath = [IO.Path]::GetFileNameWithoutExtension($pfxFilePath) + ".snk";
[byte[]] $certificateContent = Get-Content $pfxFilePath -AsByteStream;
$exportable = [Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable
$certificate = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($certificateContent, $pfxPwd, $exportable)
$rsaPrivateKey = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($certificate)
$rsaParameters = $rsaPrivateKey.ExportParameters($true)
$csp = New-Object Security.Cryptography.RSACryptoServiceProvider
$csp.ImportParameters($rsaParameters)
$certificateContent = $csp.ExportCspBlob($true)
[IO.File]::WriteAllBytes([IO.Path]::Combine([IO.Path]::GetDirectoryName($pfxFilePath), $snkFilePath), $certificateContent)