From 32913d8695cc7ef9a70a78658f7d40aea854678c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20B=C3=BClow?= Date: Wed, 4 Jan 2023 19:18:06 +0100 Subject: [PATCH] Encryption and Decryption now works including up- and download --- src/lib/DecryptButton.svelte | 23 +++++++++++++++++------ src/lib/cryptlib.ts | 7 +++---- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/lib/DecryptButton.svelte b/src/lib/DecryptButton.svelte index 73d7775..a5816dd 100644 --- a/src/lib/DecryptButton.svelte +++ b/src/lib/DecryptButton.svelte @@ -1,9 +1,10 @@ diff --git a/src/lib/cryptlib.ts b/src/lib/cryptlib.ts index 3dd8a89..3688ac8 100644 --- a/src/lib/cryptlib.ts +++ b/src/lib/cryptlib.ts @@ -61,7 +61,7 @@ the ciphertext. export async function encryptFile(file: File) { let keyMaterial = await getKeyMaterial(); let key = await getKey(keyMaterial); - iv = window.crypto.getRandomValues(new Uint8Array(12)); + iv = getStringEncoding(password).slice(0,12); const fileReader = new FileReader(); let byteArray = await file.arrayBuffer(); @@ -90,18 +90,17 @@ export async function decryptFileContent(base64Encoded: string) { let keyMaterial = await getKeyMaterial(); console.log(keyMaterial) let key = await getKey(keyMaterial); + iv = getStringEncoding(password).slice(0,12); //Remove anything form the Base64 String that isn't base64 const cleanString = base64Encoded.replace(/[^A-Za-z0-9+/=]/g, ''); // create Uint8 Array from base64 string let encryptedString = atob(cleanString) - console.log(encryptedString) const encryptedContent = new Uint8Array(encryptedString.length) for (let i = 0; i < encryptedString.length; i++) { encryptedContent[i] = encryptedString.charCodeAt(i); // Populate the array with the decoded data } - console.log(encryptedContent) const encryptedContentBuffer: ArrayBuffer = encryptedContent.buffer; try { @@ -113,7 +112,7 @@ export async function decryptFileContent(base64Encoded: string) { key, encryptedContentBuffer ); - return decrypted; + return new Uint8Array(decrypted); } catch (e) { console.log(e) errorMessage.set(`Can not decrypt file: ${e}`)