I'm trying to import a self signed PFX certificate (with private key) in Azure Key Vault with the Import-AzKeyVaultCertificate
command using the -CertificateString
parameter.
But when I run this command I get the following error message:
Import-AzKeyVaultCertificate : The specified PKCS#12 X.509 certificate content can not be read. Please check if certificate is in valid PKCS#12 format. Status: 400 (Bad Request)
I can import the very same PFX certificate manually in Key Vault, without any problems. But I need to do this using -CertificateString
for a deployment script.
So I converted my PFX certificate into a Base64 string using PowerShell:
$fileContentBytes = get-content ".\myCert.pfx" -Encoding Byte
[System.Convert]::ToBase64String($fileContentBytes) | Out-File ".\pfx-base64.txt"
Multiple sites showed that this is the way to convert a PFX cert to a Base64 string. One of them is this one: https://learn.microsoft.com/en-us/answers/questions/258583/import-certificate-api-for-azure-key-vault.html
I then use that string in PowerShell like so:
$Secure_String_Pwd = ConvertTo-SecureString "MySecretPassword" -AsPlainText -Force;
Import-AzKeyVaultCertificate -VaultName "MyKeyVault" -Name "cert-signing" -CertificateString "MIIJagIBAzCCCSYGCS.....9oV21QwICB9A=" -Password $Secure_String_Pwd;
I don't understand why its throwing an error. The certificate seems to be fine when I upload it manually. Why doesn't it work in a Base64 form?