Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const { generateKeyPairSync, publicEncrypt, privateDecrypt } = require('crypto');
- const { publicKey, privateKey } = generateKeyPairSync('rsa', {
- modulusLength: 4096,
- publicKeyEncoding: {
- type: 'spki',
- format: 'pem',
- },
- privateKeyEncoding: {
- type: 'pkcs8',
- format: 'pem'
- }
- });
- console.log('privateKey', privateKey);
- console.log('publicKey', publicKey);
- const message = {
- some: 'data',
- abc: 123
- };
- console.log('\nEncrypted message using public key:');
- console.log(message);
- const encryptedData = publicEncrypt(publicKey, Buffer.from(JSON.stringify(message)));
- console.log('\nEncrypted message (base 64):')
- console.log(encryptedData.toString('base64'));
- const decryptedData = JSON.parse(privateDecrypt(privateKey, encryptedData).toString());
- console.log('\nDecrypted message with private key:');
- console.log(decryptedData);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement