Certificate pfx to Base64

Secuirty
Base64
Author

Gary Newport

Published

Tuesday, February 12, 2019

To convert certificate that is in .pfx to base64 format in PowerShell, you can use .NET namespace available in PowerShell to convert. I had a scenario where I was required to use base64 encoding to upload certificate to Azure to secure communication to backend instance. Since Microsoft Azure provides rich API to work with. I was able to make a patch request and push certificate to Azure. In this tutorial, I will show you how to convert certificate from .pfx to base64. Open PowerShell as an administrator. Now that we have PowerShell console opened. Let’s first load the content into a variable.

Param([string] $file) $file = '.\certificate.pfx' $pfxFileBytes = get-content $file -Encoding Byte [System.Convert]::ToBase64String($pfxFileBytes) | Out-File 'PfxFileBytes-Base64.txt'

Once you have your Base64 string you can insert it in the ARM template as shown below.

Azuredeploy.parameters.json

    "cert": {"value": "MIIT..."},
    "certPass": { "value": "password" }

Azuredeploy.json

  "parameters": {
      "cert": {
      "type": "securestring"
    },
    "certPass": {
      "type": "securestring"
    }
  }
  "variables": {
    "var_cert_name": "[concat( tolower(parameters('para_application_name')), uniqueString(resourceGroup().id))]",
  }
  "resources": [
      {
      "apiVersion": "2015-08-01",
      "location": "[resourceGroup().location]",
      "name": "[variables('var_cert_name')]",
      "properties": {
        "pfxBlob": "[parameters('cert')]",
        "password": "[parameters('certPass')]"
      },
      "scale": null,
      "tags": {
        "displayName": "Certificate"
      },
      "type": "Microsoft.Web/certificates"
    }
  ]