Skip to content

Instantly share code, notes, and snippets.

@un4ckn0wl3z
Created July 27, 2024 13:08
Show Gist options
  • Save un4ckn0wl3z/55c811e61190ff618aa4fdc05a5bbcc5 to your computer and use it in GitHub Desktop.
Save un4ckn0wl3z/55c811e61190ff618aa4fdc05a5bbcc5 to your computer and use it in GitHub Desktop.
xorDecrypt.js
function xorDecrypt(cipherText, hexKey) {
// Convert the hexadecimal key to a byte array
let key = hexKey.match(/.{1,2}/g).map(byte => parseInt(byte, 16));
// Decode the base64 encoded cipher text to a byte array
let cipherBytes = atob(cipherText).split('').map(char => char.charCodeAt(0));
// XOR decryption
let plainBytes = cipherBytes.map((byte, index) => byte ^ key[index % key.length]);
// Convert the byte array to a string
let plainText = String.fromCharCode.apply(null, plainBytes);
return plainText;
}
// Example usage
let cipherText = 'U29tZUVuY3J5cHRlZERhdGE='; // base64 encoded XOR encrypted text
let hexKey = '1a2b3c4d'; // hexadecimal key
let decryptedText = xorDecrypt(cipherText, hexKey);
console.log(decryptedText);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment