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

View File

@ -61,7 +61,7 @@ the ciphertext.
export async function encryptFile(file: File) { export async function encryptFile(file: File) {
let keyMaterial = await getKeyMaterial(); let keyMaterial = await getKeyMaterial();
let key = await getKey(keyMaterial); let key = await getKey(keyMaterial);
iv = window.crypto.getRandomValues(new Uint8Array(12)); iv = getStringEncoding(password).slice(0,12);
const fileReader = new FileReader(); const fileReader = new FileReader();
let byteArray = await file.arrayBuffer(); let byteArray = await file.arrayBuffer();
@ -90,18 +90,17 @@ export async function decryptFileContent(base64Encoded: string) {
let keyMaterial = await getKeyMaterial(); let keyMaterial = await getKeyMaterial();
console.log(keyMaterial) console.log(keyMaterial)
let key = await getKey(keyMaterial); let key = await getKey(keyMaterial);
iv = getStringEncoding(password).slice(0,12);
//Remove anything form the Base64 String that isn't base64 //Remove anything form the Base64 String that isn't base64
const cleanString = base64Encoded.replace(/[^A-Za-z0-9+/=]/g, ''); const cleanString = base64Encoded.replace(/[^A-Za-z0-9+/=]/g, '');
// create Uint8 Array from base64 string // create Uint8 Array from base64 string
let encryptedString = atob(cleanString) let encryptedString = atob(cleanString)
console.log(encryptedString)
const encryptedContent = new Uint8Array(encryptedString.length) const encryptedContent = new Uint8Array(encryptedString.length)
for (let i = 0; i < encryptedString.length; i++) { for (let i = 0; i < encryptedString.length; i++) {
encryptedContent[i] = encryptedString.charCodeAt(i); // Populate the array with the decoded data encryptedContent[i] = encryptedString.charCodeAt(i); // Populate the array with the decoded data
} }
console.log(encryptedContent)
const encryptedContentBuffer: ArrayBuffer = encryptedContent.buffer; const encryptedContentBuffer: ArrayBuffer = encryptedContent.buffer;
try { try {
@ -113,7 +112,7 @@ export async function decryptFileContent(base64Encoded: string) {
key, key,
encryptedContentBuffer encryptedContentBuffer
); );
return decrypted; return new Uint8Array(decrypted);
} catch (e) { } catch (e) {
console.log(e) console.log(e)
errorMessage.set(`Can not decrypt file: ${e}`) errorMessage.set(`Can not decrypt file: ${e}`)