What is node crypto.createPublicKey(Key)?

The createPublicKey() function generates a new key object, with a public key, and returns it. This function no longer necessitates complex implementation methodologies and setups because the crypto module is built into Node.js.

Syntax

The crypto module of Node.js includes the createPublicKey() function. The syntax is:

crypto.createPublicKey(key)

Parameters

The createPublicKey() function only takes in one parameter:

  • key the data that will be used to make the public key object. It can be any of the following types:
    • String
    • Buffer
    • Object
    • TypedArray
    • ArrayBuffer

According to the Node.js Documentation if the key is a string or a Buffer, the format is presumed to be ‘pem’. If the key is a KeyObject of type ‘private’, the public key is extracted from the specified private key. Otherwise, the key must be a String or a Buffer.

Return value

createPublicKey() produces a KeyObject that holds the public key as its return value.

Example

The following code sample shows how to use the createPublicKey() function to generate a new key object with a public key:

//import crypto module
import crypto from 'crypto';
//generate key pair
const { generateKeyPair } = require('crypto');
generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'pkcs1',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs1',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: 'top secret'
}
// Handle errors and use the generated key pair
}, (err, publicKey, privateKey) => {
publicKey
});
//Assign key to variable
const publicKeyString = `-----BEGIN RSA PUBLIC KEY-----
MIICCgKCAgEAzXlBBes4SpVk5BxPVuY+LR5+pxcjTNVzlVl4ugix3husiWPSfnYl
K+z0zmWwGRBXncVgjRb/SliBhHDKSyqJ+gvk8RU1rO7qSFK0hGTrO63EKy4fbGgz
ghO0RMi+IznBqDWldd1XeBnxVUlHhjBsYNSWdFCSh6rwNTtG2Dn8GYWpXs5AUXZn
L97WQot7cAekZA+P5iNjsAZoh74tY0+bRRLfIvKaXPFiH1I72R7Ky8F1NwBmOx0k
Lk++7OGlpDDmJuI/1d3SDmznoVWeEo1evgnCYrjGeF1QCAWe0SSRWYaDCddy2dUi
1BfUIwyMHN4OSxX6TKohAPEhkbU7dU4CUyH6oEaXddhhl427UYrtv9WY32kKnY1d
q/3ZjZ1LRE3cwPywHQichjyGMOZF6l6tPE6ZVQHlrgsT3GTaSEO8I4F5Z0eaEAqZ
iqeg9sOf9DfLdTvA5oZ9NKymnHqWndEM3gIV3KA4LP+qwVlsD/zFcuxWRHjM5r1K
XvQgrLAHqfCQ7LwhUivjCjIfp2nYFYrzlDlUUZzg1AAKxhRJIUt58o7RnS/29zon
Pur5caj1iOrhfuTvkCM4uQyampk1A5+98aOObA97jCQ7mWbn8St8Hpdqim1c2v0s
QXeyRdm8aZ5xzJ5cZ2p2NWGvGoocx4c8i9GQuHUVktQE6nuMS7aKQlECAwEAAQ==
-----END RSA PUBLIC KEY-----`;
//Use the function to create and export the public key object
const publickKeyObject = crypto.createPublicKey(publicKeyString);
publickKeyObject.export({ format: 'pem', type: 'pkcs1' });
console.log(publickKeyObject)

Explanation

We loaded the crypto module first, then generated the key pairs in the second line. The publicKeyString variable holds the key. To return a keyObject containing a public key, the publicKeyString is supplied as an argument to the createPublicKey() function. The publicKeyObject is exported in PEM format and as a PKCS1 type.

The properties needed for encoding the pair of keys are:

  • Format: The format must be ‘pem’ or ‘der’, but the default format is ‘pem’.

  • Type: The type must be pkcs1 or pkcs8.

  • Cipher: The algorithm to be used for encoding the public or private key.

  • Passphrase: It is used for decryption if the private key is encrypted.

PEM, short for Privacy Enhanced Mail, is a file format for storing cryptographic keys.

The Public-Key Cryptography Standards (PKCS) is a mechanism for securely exchanging information over the Internet using a public key.

DER stands for Distinguished Encoding Rules. It is a binary format for the PEM file that follows certain structures for public keys.

Benefits

  • A message encrypted with a sender’s public key can only be decoded with the recipient’s paired personal key.
  • Signatures left using the personal key can also be verified using the public key.

Free Resources