Developer Tools for Random Data Generation // v2.6.1
root@generate-random:~/encryption-keys/javascript$ _

Generate Encryption Keys in JavaScript - Node.js Crypto Examples

Complete code tutorial with examples and best practices

[ Code Example - Quick Summary ]

Language: JavaScript

What: Generate AES and RSA encryption keys in JavaScript/Node.js using the crypto module and Web Crypto API for secure encryption.

Try it: Use our interactive Encryption-keys generator or integrate this code into your JavaScript application.

Generate AES and RSA encryption keys in JavaScript/Node.js using the crypto module and Web Crypto API for secure encryption. Looking for other languages? Check our code examples in PHP , Python , Java , C# , C++ , Ruby and Go or use our interactive web generator.

JavaScript Code Example

// Node.js - AES & RSA Key Generation
const crypto = require('crypto');

// AES-256 Key Generation
const aesKey = crypto.randomBytes(32); // 256 bits
const aesKeyHex = aesKey.toString('hex');
console.log('AES-256 Key (Hex):', aesKeyHex);

// AES-128 Key
const aes128Key = crypto.randomBytes(16).toString('hex');
console.log('AES-128 Key (Hex):', aes128Key);

// RSA Key Pair Generation (2048-bit)
const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
  modulusLength: 2048,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem'
  }
});

console.log('Private Key:\n', privateKey);
console.log('Public Key:\n', publicKey);

// Base64 encoding for storage
const aesKeyBase64 = aesKey.toString('base64');
console.log('AES Key (Base64):', aesKeyBase64);

// Web Crypto API (Browser)
// async function generateWebCryptoKey() {
//   const key = await crypto.subtle.generateKey(
//     { name: 'AES-GCM', length: 256 },
//     true,
//     ['encrypt', 'decrypt']
//   );
//   return key;
// }

[EXPLANATION]

Node.js provides crypto.randomBytes() for generating cryptographically secure random keys. For AES-256, use 32 bytes (256 bits); for AES-128, use 16 bytes (128 bits). Convert to hex or base64 for storage. crypto.generateKeyPairSync() creates RSA key pairs with configurable modulus length (2048 or 4096 bits). The publicKeyEncoding and privateKeyEncoding options control output format (PEM, DER). For browser environments, use the Web Crypto API (crypto.subtle.generateKey()) which returns CryptoKey objects suitable for encryption operations.

Expected Output

AES-256 Key (Hex): f3a7d2e5c1b8f4a9d6c3e7b2f5a8c1d4e7b9f2a5c8d1e4b7f3a6d2e5c1b8f4a9
AES-128 Key (Hex): d2f5a8c1e4b7f3a9d6c2e5b8f1a4c7d2
Private Key:
 -----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDH...
-----END PRIVATE KEY-----
Public Key:
 -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAx8...
-----END PUBLIC KEY-----
AES Key (Base64): 86fS5cG49KnWw+ey9ajB1Oe58qXI0eS389ptLlwbj0o=

Common Use Cases

  • Encrypt user data before database storage
  • Secure file upload/download systems
  • End-to-end encrypted messaging
  • Protect sensitive configuration files
  • Generate session encryption keys

Important Notes

  • crypto.randomBytes() is CSPRNG-backed
  • For async key generation, use generateKeyPair() (callback-based)
  • Web Crypto API is async and returns CryptoKey objects
  • Store keys in environment variables or secure vaults (AWS KMS, HashiCorp Vault)
  • Never expose private keys in client-side JavaScript

Try Our Interactive Generator

Don't want to write code? Use our free web-based Encryption-keys generator with instant results.

TRY ENCRYPTION-KEYS GENERATOR →