Skip to content

Instantly share code, notes, and snippets.

@hayleyxyz
Created April 12, 2025 12:56
Show Gist options
  • Save hayleyxyz/38aa701b7aa5de2367171eb9d6f83a6a to your computer and use it in GitHub Desktop.
Save hayleyxyz/38aa701b7aa5de2367171eb9d6f83a6a to your computer and use it in GitHub Desktop.
const fs = require('fs');
if (process.argv.length != 3) {
console.log('Usage: node binary_rank <filename>');
process.exit(1);
}
const filename = process.argv[2];
const isValidAscii = (b) => {
return b >= 32 && b <= 126;
}
const decrypt = (data) => {
for (let key = 0; key < 256; key++) {
let isValid = true;
let lastIndex = 0;
for (let i = 0x4C; i < data.length; i++) {
const decrypted = data.charCodeAt(i) ^ key;
if (!isValidAscii(decrypted)) {
isValid = false;
lastIndex = i;
console.log(`Invalid character: ${decrypted} at index ${i}`);
break;
}
}
console.log(`Key: ${key}, Last index: ${lastIndex}`);
if (isValid) {
console.log(`Key: ${key}`);
break;
}
}
}
fs.readFile(filename, 'binary', (err, data) => {
if (err) {
console.log(`Error reading file: ${err}`);
process.exit(1);
}
decrypt(data);
const ranks = new Map();
for (let i = 0; i < data.length; i++) {
const b = data[i];
if (ranks.has(b)) {
ranks.set(b, ranks.get(b) + 1);
} else {
ranks.set(b, 1);
}
}
const sortedRanks = Array.from(ranks.entries()).sort((a, b) => b[1] - a[1]);
for (let [byte, count] of sortedRanks) {
let hex = byte.charCodeAt(0).toString(16);
console.log(`${hex}: ${count}`);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment