Skip to content

Instantly share code, notes, and snippets.

@MichalBrylka
Last active December 29, 2022 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichalBrylka/b242894d6f507f2247e6e7f1d61b1cb8 to your computer and use it in GitHub Desktop.
Save MichalBrylka/b242894d6f507f2247e6e7f1d61b1cb8 to your computer and use it in GitHub Desktop.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
using namespace System.Security.Cryptography
using namespace System.Security.Cryptography.X509Certificates
param (
[Parameter(Mandatory=$True)] [Alias("in")] [string]$inputFile,
[Parameter(Mandatory=$True)] [Alias("pass")] [string]$password,
[Parameter(Mandatory=$False)] [Alias("out")] [string]$output
)
function ConvertToPem([string]$filename, [string] $password)
{
try {
$cert = [X509Certificate2]::new($filename, $password, [X509KeyStorageFlags]::PersistKeySet + [X509KeyStorageFlags]::Exportable)
$certPem = [string]::new([PemEncoding]::Write("CERTIFICATE", $cert.RawData))
$certAlgorithm =
[RSACertificateExtensions]::GetRSAPrivateKey($cert) ??
[ECDsaCertificateExtensions]::GetECDsaPrivateKey($cert) ??
[DSACertificateExtensions]::GetDSAPrivateKey($cert) ??
$cert.GetECDiffieHellmanPrivateKey()
if ($null -eq $certAlgorithm) {
Write-Error "Unknown certificate algorithm"
Exit 10
}
$keyPem = [string]::new([PemEncoding]::Write("PRIVATE KEY", $certAlgorithm.ExportPkcs8PrivateKey()))
return "$certPem`n$keyPem"
}
catch {
Write-Error "An error occurred during certificate conversion"
Write-Error $_
Exit 20
}
finally
{
$cert.Dispose()
$certAlgorithm.Dispose()
}
}
if (-Not($output)){
$output = [io.path]::ChangeExtension($inputFile, ".pem")
}
$pem = ConvertToPem -filename $inputFile -password $password
$pem | Out-File -FilePath $output
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
var (input, password, output) = args.Length switch
{
2 => (args[0], args[1], Path.ChangeExtension(args[0], ".pem")),
3 => (args[0], args[1], args[2]),
_ => throw new ArgumentException("Pass either 2 (input, password) or 3 arguments (input, password, output)", nameof(args)),
};
var pem = ConvertToPem(input, password);
File.WriteAllText(output, pem);
static string ConvertToPem(string filename, string password)
{
using var cert = new X509Certificate2(filename, password, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
var certPem = new string(PemEncoding.Write("CERTIFICATE", cert.RawData));
using var certAlgorithm =
cert.GetRSAPrivateKey() as AsymmetricAlgorithm ??
cert.GetECDsaPrivateKey() as AsymmetricAlgorithm ??
cert.GetDSAPrivateKey() as AsymmetricAlgorithm ??
cert.GetECDiffieHellmanPrivateKey() as AsymmetricAlgorithm ??
throw new CryptographicException("Unknown certificate algorithm");
var keyPem = new string(PemEncoding.Write("PRIVATE KEY", certAlgorithm.ExportPkcs8PrivateKey()));
return certPem + Environment.NewLine + keyPem;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment