Skip to content

Commit

Permalink
moving to the crypto library from openssl
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunal Nagar committed Apr 8, 2019
1 parent a8094b2 commit fa68b12
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 34 deletions.
39 changes: 39 additions & 0 deletions encrypt_decrypt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var fs = require("fs");
var crypto = require("crypto");
var log = require("electron-log");

module.exports = class EncryptDecrypt {

constructor(finalFile, filePath, passphrase) {
this.finalFile = finalFile;
this.filePath = filePath;
this.passphrase = passphrase;
}

encrypt(data) {
try {
var cipher = crypto.createCipher('aes-256-cbc', this.passphrase);
data.pipe(cipher).pipe(fs.createWriteStream(this.finalFile));
return {
code: 200,
message: "Encrypted"
};
} catch(e) {
throw new Error(e.message);
}
}

decrypt(data) {
try {
var decipher = crypto.createDecipher("aes-256-cbc", this.passphrase);
data.pipe(decipher).pipe(fs.createWriteStream(this.finalFile));
return {
code: 200,
message: "Decrypted"
};
} catch (exception) {
throw new Error(exception.message);
}
}

}
39 changes: 23 additions & 16 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var path = require("path")
var fs = require("fs");
var http = require("http");
var exec = require("child_process").exec
var crypto = require("crypto");
var EncryptDecryptHelper = require("./encrypt_decrypt");
var child;

function createWindow() {
Expand Down Expand Up @@ -52,38 +54,43 @@ ipcMain.on("event:log", function(e, arg) {
ipcMain.on("action:encrypt_decrypt", function (e, arg) {
log.info("encrypting or decrypting...");
log.debug(arg);
var nameWithExt = "";
var key = arg.passphrase;
var cipher;
var input;
var output;
var popupFileName;
if(arg.action === "encrypt") {
nameWithExt = arg.filePath + ".enc";
popupFileName = arg.filePath + ".enc";
} else if(arg.action === "decrypt") {
nameWithExt = arg.filePath.replace(/\.[^/.]+$/, "")
popupFileName = arg.filePath.replace(".enc", "");
}
var edHelper;
var result;
dialog.showSaveDialog({
defaultPath: nameWithExt
defaultPath: popupFileName
}, function (filename) {
if(typeof filename !== "undefined") {
edHelper = new EncryptDecryptHelper(filename, arg.filePath, arg.passphrase)
if(arg.action === "encrypt") {
log.info("Encrypting " + filename + " with password <redacted>");
e.sender.send("notice-status", "Encrypting...")
sCommand = "openssl enc -aes-256-cbc -salt -in " + arg.filePath + " -out " + filename + " -k " + arg.passphrase
result = edHelper.encrypt(fs.createReadStream(arg.filePath));
if(result && result.code === 200) {
log.info("File successfully encrypted!");
e.sender.send("notice-status", "Done! File has been saved to: " + filename)
}
} else if(arg.action === "decrypt") {
log.info("Decrypting " + filename + " with password <redacted>");
e.sender.send("notice-status", "Decrypting...")
sCommand = "openssl enc -d -aes-256-cbc -in " + arg.filePath + " -out " + filename + " -k " + arg.passphrase
}
child = exec(sCommand, function (err, stdout, stderr) {
if (err !== null) {
log.error("Something went wrong while encrypting/decrypting...");
log.error(err);
e.sender.send("notice-status", "Something went wrong.");
} else {
log.info("File successfully: " + arg.action);
result = edHelper.decrypt(fs.createReadStream(arg.filePath));
if(result && result.code === 200) {
log.info("File successfully decrypted!");
e.sender.send("notice-status", "Done! File has been saved to: " + filename)
}
});
}
} else {
log.warn("Destination file location not selected");
e.sender.send("notice-status", "Oops. Destination file location not selected. Press the Reset button and try again!")
e.sender.send("notice-status", "Oops. Destination file location not selected. Please try again!")
}
});
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"time-grunt": "^2.0.0"
},
"dependencies": {
"crypto": "^1.0.1",
"electron-log": "^3.0.5",
"jquery": "^3.3.1"
}
Expand Down
44 changes: 26 additions & 18 deletions renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,35 @@ $(function () {
});

$btnEncrypt.on('click', function() {
ipcRenderer.send('event:log', {
action: 'click',
element: 'choice_encrypt'
});
ipcRenderer.send('action:encrypt_decrypt', {
action: 'encrypt',
filePath: filePath,
passphrase: $fieldPassphrase.val()
});
if($fieldPassphrase.val()) {
ipcRenderer.send('event:log', {
action: 'click',
element: 'choice_encrypt'
});
ipcRenderer.send('action:encrypt_decrypt', {
action: 'encrypt',
filePath: filePath,
passphrase: $fieldPassphrase.val()
});
} else {
$fieldNotice.html("Passphrase is required");
}
});

$btnDecrypt.on('click', function() {
ipcRenderer.send('event:log', {
action: 'click',
element: 'choice_decrypt'
});
ipcRenderer.send('action:encrypt_decrypt', {
action: 'decrypt',
filePath: filePath,
passphrase: $fieldPassphrase.val()
});
if($fieldPassphrase.val()) {
ipcRenderer.send('event:log', {
action: 'click',
element: 'choice_decrypt'
});
ipcRenderer.send('action:encrypt_decrypt', {
action: 'decrypt',
filePath: filePath,
passphrase: $fieldPassphrase.val()
});
} else {
$fieldNotice.html("Passphrase is required");
}
});

$areaDrag.on('dragover', function (e) {
Expand Down

0 comments on commit fa68b12

Please sign in to comment.