Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const path = require('path');
  2. const fs = require('fs');
  3. const crypto = require('crypto');
  4.  
  5. const licensePath = path.resolve('z32_k2.lic');
  6. const SYM_KEY_PREFIX = Buffer.from('SYMKEY').toString('base64');
  7.  
  8. try {
  9.   const licenseFileInput = fs.readFileSync(licensePath, 'utf-8');
  10.  
  11.   const publicKey = readPublicKey(licenseFileInput);
  12.   console.log(publicKey);
  13.   const licenseData = readLicenseData(licenseFileInput, publicKey);
  14.   console.log(licenseData);
  15.  
  16. } catch(err) {
  17.   console.log(err);
  18. }
  19.  
  20. function readPublicKey(licenseFileInput) {
  21.     const lines = licenseFileInput.split('\n');
  22.     const indexKeyEnd = lines.indexOf('-----END PUBLIC KEY-----');
  23.  
  24.     var key = '';
  25.  
  26.     for (var i = 0; i <= indexKeyEnd; i++) {
  27.       key += lines[i];
  28.       key += i < indexKeyEnd ? '\n' : '';
  29.     }
  30.  
  31.     return key;
  32. }
  33.  
  34. function readLicenseData(licenseFileInput, publicKey) {
  35.   const lines = licenseFileInput.split('\n');
  36.  
  37.   for (var i = 0; i < lines.length; i++) {
  38.       if (lines[i].startsWith(SYM_KEY_PREFIX)) {
  39.           const b64SymKey = lines[i];
  40.           const b64LicenseData = lines[i + 1];
  41.  
  42.           const encSymKey = Buffer.from(b64SymKey.substring(SYM_KEY_PREFIX.length), 'base64');
  43.           const symKey = Buffer.from(crypto.publicDecrypt(publicKey, encSymKey), 'base64');
  44.           console.log(symKey);
  45.  
  46.           const encLicenseData = Buffer.from(b64LicenseData, 'base64');
  47.           const decipher = crypto.createDecipher('aes-256-cbc', symKey);
  48.  
  49.           var licenseData = Buffer.concat([decipher.update(encLicenseData), decipher.final()]);
  50.           licenseData = Buffer.from(licenseData, 'base64').toString('utf-8');
  51.           console.log(licenseData);
  52.           return;
  53.       }
  54.  
  55.       else if (i + 1 === lines.length) {
  56.           var b64LicenseData = lines[i];
  57.           var encLicenseData = Buffer.from(b64LicenseData, 'base64');
  58.           var licenseData = crypto.publicDecrypt(publicKey, encLicenseData);
  59.  
  60.           return Buffer.from(licenseData, 'base64').toString('utf-8');
  61.       }
  62.   }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement