Encryption and Decryption now works including up- and download

main
Johannes Bülow 2023-01-04 19:18:06 +01:00
parent dfcacf7bbf
commit 32913d8695
2 changed files with 20 additions and 10 deletions

View File

@ -1,9 +1,10 @@
<script lang="ts">
import { onMount } from 'svelte';
import { escape } from 'svelte/internal';
import { escape } from 'svelte/internal';
import { decryptFileContent } from './cryptlib';
let filename: string = "";
let originalFilename: string;
let originalFilename: string = "Decrypted.txt";
let outputFile: Blob;
function removeCrpytFileEnding() {
if (filename.endsWith(".crypt")) {
@ -39,24 +40,34 @@
event.preventDefault();
filename = file.name;
console.log("Uploaded:", filename)
removeCrpytFileEnding()
if (!file) {
return;
}
readFileContent(file).then((fileContent) => {
let outputFile: Blob;
decryptFileContent(fileContent).then((outputArray) => {
console.log(outputArray)
console.log("Output Array:", outputArray);
outputFile = new Blob([outputArray], { type: 'application/octet-stream'});
});
console.log("Blob", outputFile)
if (outputFile !instanceof Blob) {
console.log("Is not a blob")
}
if (outputFile instanceof Blob) {
console.log("Is a blob")
}
console.log("out side DecryptFileContent", outputFile)
let url = URL.createObjectURL(outputFile);
console.log(url)
let a = document.createElement("a");
a.href = url;
a.download = originalFilename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
});
}); //end of decryptFileContent
}); //end of readFileContent
}
</script>

View File

@ -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}`)