Browse Source

this works

Girish Ramakrishnan 7 months ago
parent
commit
6266e6d010
2 changed files with 22 additions and 9 deletions
  1. 2 2
      bytestokey.js
  2. 20 7
      test.js

+ 2 - 2
bytestokey.js

@@ -57,7 +57,7 @@ if (cipher && passphrase !== undefined) {
     console.log(`WARNING: is fundamentally compromised.  You should fix this ASAP!`);
   }
 } else {
-  const [cipher, passphrase] = ['aes-128-cbc', 'secret'];
+  const [cipher, passphrase] = ['aes-256-cbc', 'secret'];
   const [key, iv] = compute(cipher, passphrase);
   console.log('Usage:');
   console.log(`  ${arg0} ${arg1} CIPHER PASSPHRASE`);
@@ -78,4 +78,4 @@ if (cipher && passphrase !== undefined) {
   console.log(`  const key = Buffer.from('${key}', 'hex');`);
   console.log(`  const iv  = Buffer.from('${iv}', 'hex');`);
   console.log(`  const cipher = crypto.createCipheriv('${cipher}', key, iv);`);
-}
+}

+ 20 - 7
test.js

@@ -23,22 +23,35 @@ function decrypt(blob, key) {
     assert(Buffer.isBuffer(blob));
     assert.strictEqual(typeof key, 'string');
 
-    var decipher = crypto.createDecipher('aes-256-cbc', key);
+    let decipher;
+
+    if (key === 'secret') {
+        const key = Buffer.from('5ebe2294ecd0e0f08eab7690d2a6ee6926ae5cc854e36b6bdfca366848dea6bb', 'hex');
+        const iv  = Buffer.from('e8c80b4b831fbb64b0d5c6c8499e541a', 'hex');
+
+        decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
+    } else {
+        decipher = crypto.createDecipher('aes-256-cbc', key);
+    }
+
     let raw = decipher.update(blob);
     raw = Buffer.concat([ raw, decipher.final() ]);
     return raw;
 }
 
 function main() {
-    let blob = new Buffer('foo'); // fs.readFileSync('test.js');
+    let blob = fs.readFileSync('test.js');
     let crypt = encrypt(blob, 'secret');
 
-console.log(crypt.toString('base64'));
-//    fs.writeFileSync('/tmp/test.js.enc', crypt);
+    fs.writeFileSync('/tmp/test.js.enc', crypt);
+    console.log('encrypted output to /tmp/test.js.enc');
+
 
-//    crypt = fs.readFileSync('/tmp/test.js.enc');
-//    let out = decrypt(crypt, 'secret');
-//    fs.writeFileSync('/tmp/test.js', out);
+    crypt = fs.readFileSync('/tmp/test.js.enc');
+    let out = decrypt(crypt, 'secret');
+    fs.writeFileSync('/tmp/test.js', out);
+    fs.writeFileSync('/tmp/test.js', out);
+    console.log('decrypted output to /tmp/test.js');
 }
 
 main();