Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. const { generateKeyPairSync, publicEncrypt, privateDecrypt } = require('crypto');
  2. const { publicKey, privateKey } = generateKeyPairSync('rsa', {
  3. modulusLength: 4096,
  4. publicKeyEncoding: {
  5. type: 'spki',
  6. format: 'pem',
  7. },
  8. privateKeyEncoding: {
  9. type: 'pkcs8',
  10. format: 'pem'
  11. }
  12. });
  13.  
  14. console.log('privateKey', privateKey);
  15. console.log('publicKey', publicKey);
  16.  
  17. const message = {
  18. some: 'data',
  19. abc: 123
  20. };
  21.  
  22. console.log('\nEncrypted message using public key:');
  23. console.log(message);
  24.  
  25. const encryptedData = publicEncrypt(publicKey, Buffer.from(JSON.stringify(message)));
  26. console.log('\nEncrypted message (base 64):')
  27. console.log(encryptedData.toString('base64'));
  28.  
  29. const decryptedData = JSON.parse(privateDecrypt(privateKey, encryptedData).toString());
  30. console.log('\nDecrypted message with private key:');
  31. console.log(decryptedData);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement