Created
November 13, 2024 18:57
-
-
Save adrianhorning08/c4780e377ec07e5c6b3e9c3d11a24204 to your computer and use it in GitHub Desktop.
Decrypt view stats response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Async function to handle JSON or encrypted response | |
let decryptResponse = async (response) => { | |
let result; | |
// These are Base64 encoded arrays used as the key and IV for AES-GCM decryption | |
let base64IV = | |
"Wzk3LCAxMDksIC0xMDAsIC05MCwgMTIyLCAtMTI0LCAxMSwgLTY5LCAtNDIsIDExNSwgLTU4LCAtNjcsIDQzLCAtNzUsIDMxLCA3NF0="; | |
let base64Key = | |
"Wy0zLCAtMTEyLCAxNSwgLTEyNCwgLTcxLCAzMywgLTg0LCAxMDksIDU3LCAtMTI3LCAxMDcsIC00NiwgMTIyLCA0OCwgODIsIC0xMjYsIDQ3LCA3NiwgLTEyNywgNjUsIDc1LCAxMTMsIC0xMjEsIDg5LCAtNzEsIDUwLCAtODMsIDg2LCA5MiwgLTQ2LCA0OSwgNTZd"; | |
try { | |
let contentType; | |
// Check if the response is JSON based on the content-type header | |
if ( | |
response.headers && | |
(contentType = response.headers.get("content-type")) && | |
contentType.includes("application/json") | |
) { | |
// If JSON, parse the response normally | |
result = await response.json(); | |
} else { | |
// If not JSON, proceed with decryption | |
// Decode Base64 key and IV, then parse as Uint8Arrays | |
let ivArray = new Uint8Array(JSON.parse(atob(base64IV))); | |
let keyArray = new Uint8Array(JSON.parse(atob(base64Key))); | |
// Read the response as a binary array | |
let encryptedData = new Uint8Array(await response.arrayBuffer()); | |
// Import the AES key for decryption | |
let cryptoKey = await crypto.subtle.importKey( | |
"raw", | |
keyArray, | |
{ name: "AES-GCM" }, | |
false, | |
["decrypt"] | |
); | |
// Decrypt the data using AES-GCM with the imported key and IV | |
let decryptedData = await crypto.subtle.decrypt( | |
{ name: "AES-GCM", iv: ivArray }, | |
cryptoKey, | |
encryptedData | |
); | |
// Decode the decrypted data to a UTF-8 string | |
let decodedText = new TextDecoder("utf-8").decode(decryptedData); | |
// Parse the decrypted string as JSON | |
result = JSON.parse(decodedText); | |
} | |
} catch (error) { | |
// In case of any error during decryption, try the decryption process again | |
let ivArray = new Uint8Array(JSON.parse(atob(base64IV))); | |
let keyArray = new Uint8Array(JSON.parse(atob(base64Key))); | |
let encryptedData = new Uint8Array(await response.arrayBuffer()); | |
let cryptoKey = await crypto.subtle.importKey( | |
"raw", | |
keyArray, | |
{ name: "AES-GCM" }, | |
false, | |
["decrypt"] | |
); | |
let decryptedData = await crypto.subtle.decrypt( | |
{ name: "AES-GCM", iv: ivArray }, | |
cryptoKey, | |
encryptedData | |
); | |
result = JSON.parse(new TextDecoder("utf-8").decode(decryptedData)); | |
} finally { | |
// Return the final result, whether decrypted or parsed JSON | |
return result; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment