| 123456789101112131415161718192021222324 |
- const crypto = require('crypto');
- const { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
- modulusLength: 2048,
- publicKeyEncoding: { type: 'spki', format: 'pem' },
- privateKeyEncoding: { type: 'pkcs1', format: 'pem' },
- });
- const privB64 = privateKey
- .replace('-----BEGIN RSA PRIVATE KEY-----\n', '')
- .replace('\n-----END RSA PRIVATE KEY-----', '')
- .replace(/\n/g, '');
- const fs = require('fs');
- fs.writeFileSync('_priv.txt', privB64);
- fs.writeFileSync('_pub.txt', publicKey);
- // Verify
- const privObj = crypto.createPrivateKey(privateKey);
- const pubObj = crypto.createPublicKey(privObj);
- const pubOut = pubObj.export({ type: 'spki', format: 'pem' });
- console.log('Generated and verified.');
- console.log('Private base64 length:', privB64.length);
- console.log('Public PEM:');
- console.log(pubOut);
|