From fddfeb652572d0cd389fa4b8e4634e3292754af5 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Wed, 9 Jul 2025 01:07:18 -0400 Subject: [PATCH] Improve AES encryption with dynamic salt handling Updated `encryptAES` to use a randomly generated salt and modified the return value to include the salt, IV, and encrypted content in a single output. Adjusted `decryptAES` to extract the salt and IV from the input, ensuring the decryption key is derived using the same salt used during encryption. Enhanced decryption process to utilize the extracted IV for AES-GCM operations. --- SharpbinV2.Server/wwwroot/js/index.js | 15 +++++++++++---- SharpbinV2.Server/wwwroot/js/paste.js | 14 +++++++++----- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/SharpbinV2.Server/wwwroot/js/index.js b/SharpbinV2.Server/wwwroot/js/index.js index f9a1c3f..b2db075 100644 --- a/SharpbinV2.Server/wwwroot/js/index.js +++ b/SharpbinV2.Server/wwwroot/js/index.js @@ -69,6 +69,9 @@ function getSiteInfo() { async function encryptAES(content, password) { const enc = new TextEncoder(); + + const salt = crypto.getRandomValues(new Uint8Array(16)); + const keyMaterial = await window.crypto.subtle.importKey( "raw", enc.encode(password), @@ -79,7 +82,7 @@ async function encryptAES(content, password) { const key = await window.crypto.subtle.deriveKey( { name: "PBKDF2", - salt: enc.encode("salt"), + salt: salt, iterations: 100000, hash: "SHA-256" }, @@ -97,7 +100,11 @@ async function encryptAES(content, password) { key, enc.encode(content) ); - const encryptedArray = new Uint8Array(encryptedContent); - const encryptedString = btoa(String.fromCharCode(...iv) + String.fromCharCode(...encryptedArray)); - return encryptedString; + + const combined = new Uint8Array(salt.length + iv.length + encryptedContent.byteLength); + combined.set(salt, 0); + combined.set(iv, salt.length); + combined.set(new Uint8Array(encryptedContent), salt.length + iv.length); + + return btoa(String.fromCharCode.apply(null, combined)); } diff --git a/SharpbinV2.Server/wwwroot/js/paste.js b/SharpbinV2.Server/wwwroot/js/paste.js index affb162..37d9f69 100644 --- a/SharpbinV2.Server/wwwroot/js/paste.js +++ b/SharpbinV2.Server/wwwroot/js/paste.js @@ -184,6 +184,12 @@ function addContent(content, syntax) { async function decryptAES(content, password) { const enc = new TextEncoder(); + const rawData = atob(content); + const rawDataArray = Uint8Array.from(rawData, c => c.charCodeAt(0)); + const salt = rawDataArray.slice(0, 16); + const iv = rawDataArray.slice(16, 28); + const ciphertext = rawDataArray.slice(28); + const keyMaterial = await window.crypto.subtle.importKey( "raw", enc.encode(password), @@ -194,7 +200,7 @@ async function decryptAES(content, password) { const key = await window.crypto.subtle.deriveKey( { name: "PBKDF2", - salt: enc.encode("salt"), + salt: salt, iterations: 100000, hash: "SHA-256" }, @@ -203,10 +209,8 @@ async function decryptAES(content, password) { false, ["decrypt"] ); - const rawData = atob(content); - const rawDataArray = Uint8Array.from(rawData, c => c.charCodeAt(0)); - const iv = rawDataArray.slice(0, 12); - const ciphertext = rawDataArray.slice(12); + + const decrypted = await window.crypto.subtle.decrypt( { name: "AES-GCM",