aboutsummaryrefslogtreecommitdiffhomepage
path: root/File-decryption
diff options
context:
space:
mode:
authorThomas Van Iseghem <[email protected]>2023-04-26 21:35:16 +0200
committerThomas Van Iseghem <[email protected]>2023-04-26 21:35:16 +0200
commit472c41c0084b3dec70bf32c91b9d819c846ce056 (patch)
treea8f337d454ead6a82bf59b6a978afae03a908846 /File-decryption
parent0aab7ac443f22f2539cb90e70c2137fe013185e3 (diff)
downloadOpenCortex-capture-sharing.tar.gz
OpenCortex-capture-sharing.zip
Refactored and added encrypt functioncapture-sharing
This can be refactored even more but will do that later. This is a good setup to allow for a PoC capture sharing service.
Diffstat (limited to 'File-decryption')
-rw-r--r--File-decryption/webapp/assets/js/file-processing.js74
-rw-r--r--File-decryption/webapp/assets/js/main.js20
2 files changed, 68 insertions, 26 deletions
diff --git a/File-decryption/webapp/assets/js/file-processing.js b/File-decryption/webapp/assets/js/file-processing.js
index 78e8b1e..bc64385 100644
--- a/File-decryption/webapp/assets/js/file-processing.js
+++ b/File-decryption/webapp/assets/js/file-processing.js
@@ -63,33 +63,61 @@ function deriveKeyAndIV(password, iterations = 10) {
return { key: key, iv: iv };
}
-function processFileInput(e) {
- const fileName = e.target.fileName;
-
- const serial = document.getElementById('serial-input').value;
- let main_key = null;
- if (serial.length > 0) {
- // local decryption, use master key + serial
- main_key = new Uint8Array([...MASTER_KEY, ...new TextEncoder("utf-8").encode(serial)]);
- } else {
- // global decryption, use master key only
- main_key = MASTER_KEY;
- }
+async function decryptFile(fileContents, key) {
+ // derive key and iv with our EVP_BytesToKey port
+ const derived = deriveKeyAndIV(key);
+
+ // import the raw key
+ const cryptoKey = await window.crypto.subtle.importKey(
+ "raw",
+ derived.key,
+ "AES-CTR",
+ true,
+ ["encrypt", "decrypt"]
+ );
+
+ console.log("cryptoKey", cryptoKey)
+
+ // decrypt the file contents using aes-128-ctr
+ const decryptedData = await window.crypto.subtle.decrypt(
+ { name: "AES-CTR", counter: derived.iv, length: 128 },
+ cryptoKey,
+ fileContents
+ );
+
+ // create a new Blob with the decrypted data
+ const decryptedBlob = new Blob([decryptedData], { type: "application/octet-stream" });
+
+ return decryptedBlob;
+ }
+
+async function encryptFile(fileContents, key) {
// derive key and iv with our EVP_BytesToKey port
- const derived = deriveKeyAndIV(main_key);
- // encrypted file contents
- const ciphertext = e.target.result;
+ const derived = deriveKeyAndIV(key);
+
// import the raw key
- window.crypto.subtle.importKey("raw", derived.key, "AES-CTR", true, ["encrypt", "decrypt"]).then(function (key) {
- // decrypt using aes-128-ctr
- window.crypto.subtle.decrypt({ name: "AES-CTR", counter: derived.iv, length: 128 }, key, ciphertext).then(function (cleartext) {
- var blob = new Blob([cleartext], { type: "application/octet-stream" });
- decryptedBlobUrl = window.URL.createObjectURL(blob);
- currentClearText = cleartext;
- });
- });
+ const cryptoKey = await window.crypto.subtle.importKey(
+ "raw",
+ derived.key,
+ "AES-CTR",
+ true,
+ ["encrypt", "decrypt"]
+ );
+
+ // encrypt the file contents using aes-128-ctr
+ const encryptedData = await window.crypto.subtle.encrypt(
+ { name: "AES-CTR", counter: derived.iv, length: 128 },
+ cryptoKey,
+ fileContents
+ );
+
+ // create a new Blob with the encrypted data
+ const encryptedBlob = new Blob([encryptedData], { type: "application/octet-stream" });
+
+ return encryptedBlob;
}
+
function processFileDecode(clearText) {
const protobufType = document.getElementById('protobuf-list').value;
diff --git a/File-decryption/webapp/assets/js/main.js b/File-decryption/webapp/assets/js/main.js
index e208e91..6d7c71f 100644
--- a/File-decryption/webapp/assets/js/main.js
+++ b/File-decryption/webapp/assets/js/main.js
@@ -32,11 +32,25 @@ document.addEventListener("DOMContentLoaded", function () {
fileInput.onchange = () => {
const reader = new FileReader()
- reader.addEventListener('load', (e) => {
- console.log(e);
+ reader.addEventListener('load', async (e) => {
currentFileName = e.target.fileName;
- processFileInput(e);
+ const serial = document.getElementById('serial-input').value;
+
+ let main_key = null;
+ if (serial.length > 0) {
+ // local decryption, use master key + serial
+ main_key = new Uint8Array([...MASTER_KEY, ...new TextEncoder("utf-8").encode(serial)]);
+ } else {
+ // global decryption, use master key only
+ main_key = MASTER_KEY;
+ }
+
+ console.log("Decrypting file: " + currentFileName + " with key: " + main_key + " and serial: " + serial);
+ let blob = await decrypt(e.target.result, main_key);
+ console.log(blob);
+ decryptedBlobUrl = window.URL.createObjectURL(blob);
+
// If the filename ends with .cns or pb, we can assume it's a protobuf file
if(currentFileName.endsWith('.cns') || currentFileName.endsWith('.pb')){
// Enable live decoding