Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Access a .pem public key from .env file

I am storing a public key in a env variable as a string. This public key is from a .pem file. When I try to use it in my code, I get the following error

error:0909006C:PEM routines:get_name:no start line

I have tried what other users have suggested, by converting it to base64 and then using the key, but I still get the same error.

env variable for the public key

PUB_KEY='-----BEGIN PUBLIC KEY-----randomgibberish-----END PUBLIC KEY-----'

Code for converting it to base64

const pubKey = process.env.PUB_KEY
const buff = Buffer.from(pubKey).toString('base64');
console.log(buff)

Using it in the createPublicKey method here

crypto.createPublicKey({
                key: buff,
                format: 'pem',
            });

Any idea what could be going wrong? TIA

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your code is breaking because of line break in the public keys.

No need to use base64, I suggest you to use line breaks (\n) in env file to store the key just like this:

If assuming your key is something like this:

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlkH/R64I0H1awndlU1w6BseS9
5ygy2v6rwV7BA/xlNGCtWfsQ8UlbJl7fawZ1hgL7H8FcUkGk/RsWB7xRpUeHypnE
8UU2bbhaS+X8Bze2kdoayerb5+YK6kZlyPvmI+WVxksKUEChcKE+t83mqVpnQnO9
TkTXhzvual4cG+WatwIDAQAB
-----END PUBLIC KEY-----

then save is it like this (\n on the end of the line and making it a whole string, you can see how i have stored it)

export publicKey = '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlkH/R64I0H1awndlU1w6BseS9\n5ygy2v6rwV7BA/xlNGCtWfsQ8UlbJl7fawZ1hgL7H8FcUkGk/RsWB7xRpUeHypnE\n8UU2bbhaS+X8Bze2kdoayerb5+YK6kZlyPvmI+WVxksKUEChcKE+t83mqVpnQnO9\nTkTXhzvual4cG+WatwIDAQAB\n-----END PUBLIC KEY-----'

And the in the code use:

const { publicKey } = process.env
const originalPublicKey = publicKey.replace(/\\n/g, '\n')

This will do the work, it will generate your original keys.

const publicKey = '-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDlkH/R64I0H1awndlU1w6BseS9\n5ygy2v6rwV7BA/xlNGCtWfsQ8UlbJl7fawZ1hgL7H8FcUkGk/RsWB7xRpUeHypnE\n8UU2bbhaS+X8Bze2kdoayerb5+YK6kZlyPvmI+WVxksKUEChcKE+t83mqVpnQnO9\nTkTXhzvual4cG+WatwIDAQAB\n-----END PUBLIC KEY-----'

const originalPublicKey = publicKey.replace(/\\n/g, '\n')

console.log(originalPublicKey)

then you can simply use this in your code.

crypto.createPublicKey({
                key: originalPublicKey,
                format: 'pem',
            });

Let me know in the comments, if have any issue after this too.

about 4 years ago · Juan Pablo Isaza Report

0

Your key seems to be a PEM encoded public key in X.509/SPKI format. However, the line breaks are missing. These are to be set so that header and footer are each on a single line. In the body there is a line break after every 64 characters.

A correctly formatted PEM key can be processed directly by createPublicKey(). The key will be accepted even if the line breaks in the body are missing, but header and footer must be in different lines, otherwise the posted error message will be displayed: error:0909006C:PEM routines:get_name:no start line.

Example:

var crypto = require('crypto')

var x509 = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAunF5aDa6HCfLMMI/MZLT
5hDk304CU+ypFMFiBjowQdUMQKYHZ+fklB7GpLxCatxYJ/hZ7rjfHH3Klq20/Y1E
bYDRopyTSfkrTzPzwsX4Ur/l25CtdQldhHCTMgwf/Ev/buBNobfzdZE+Dhdv5lQw
KtjI43lDKvAi5kEet2TFwfJcJrBiRJeEcLfVgWTXGRQn7gngWKykUu5rS83eAU1x
H9FLojQfyia89/EykiOO7/3UWwd+MATZ9HLjSx2/Lf3g2jr81eifEmYDlri/OZp4
OhZu+0Bo1LXloCTe+vmIQ2YCX7EatUOuyQMt2Vwx4uV+d/A3DP6PtMGBKpF8St4i
GwIDAQAB
-----END PUBLIC KEY-----`;

// Import
var importedPubKey = crypto.createPublicKey({
    key: x509,
    format: 'pem' // default, can also be omitted
});

// Export
console.log(importedPubKey.export({type: 'spki', format: 'pem'}))

The key can also be written as one-liner with line breaks as escape sequences (\n), e.g. for header and footer in single lines:

var x509 = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAunF5aDa6HCfLMMI/MZLT5hDk304CU+ypFMFiBjowQdUMQKYHZ+fklB7GpLxCatxYJ/hZ7rjfHH3Klq20/Y1EbYDRopyTSfkrTzPzwsX4Ur/l25CtdQldhHCTMgwf/Ev/buBNobfzdZE+Dhdv5lQwKtjI43lDKvAi5kEet2TFwfJcJrBiRJeEcLfVgWTXGRQn7gngWKykUu5rS83eAU1xH9FLojQfyia89/EykiOO7/3UWwd+MATZ9HLjSx2/Lf3g2jr81eifEmYDlri/OZp4OhZu+0Bo1LXloCTe+vmIQ2YCX7EatUOuyQMt2Vwx4uV+d/A3DP6PtMGBKpF8St4iGwIDAQAB\n-----END PUBLIC KEY-----"

All this also works if the key is stored in an environment variable (e.g. X509ENV) and retrieved (via process.env.X509ENV) as long as the line breaks are correctly taken into account.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!