PureGremlin

Powershell split cert to pem

Aug 6th, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##$thumb=""
  2. $thumb = (Get-ChildItem Cert:\LocalMachine\My\|select subject,notafter,thumbprint | ogv -PassThru ).thumbprint
  3.  
  4. $cert=Get-ChildItem Cert:\LocalMachine\My\$thumb
  5. ##Public key
  6. $CertBase64 = [System.Convert]::ToBase64String($cert.RawData, [System.Base64FormattingOptions]::InsertLineBreaks)
  7. ## Private key
  8. $RSACng = [System.Security.Cryptography.X509Certificates.RSACertificateExtensions]::GetRSAPrivateKey($cert)
  9. $KeyBytes = $RSACng.Key.Export([System.Security.Cryptography.CngKeyBlobFormat]::Pkcs8PrivateBlob)
  10. $KeyBase64 = [System.Convert]::ToBase64String($KeyBytes, [System.Base64FormattingOptions]::InsertLineBreaks)
  11.  
  12. ## Build pem files
  13. $Pemkey = @"
  14. -----BEGIN PRIVATE KEY-----
  15. $KeyBase64
  16. -----END PRIVATE KEY-----
  17. "@
  18. $Pemcert = @"
  19. -----BEGIN CERTIFICATE-----
  20. $CertBase64
  21. -----END CERTIFICATE-----
  22. "@
  23. ## Export
  24. $pemkey | out-file -filepath .\PEMkey.key
  25. $pemcert | out-file -filepath .\PEMcert.crt
  26.  
  27.  
  28.  
Add Comment
Please, Sign In to add comment