Skip to content

Instantly share code, notes, and snippets.

@chris--young
Created March 25, 2015 23:50
Show Gist options
  • Save chris--young/4d3027eb00751ee18abf to your computer and use it in GitHub Desktop.
Save chris--young/4d3027eb00751ee18abf to your computer and use it in GitHub Desktop.
Mob Crypto Challenge
function decrypt(cipherText, key) {
var keyInt = key.codePointAt(0),
plainText = '';
for (var char = 0; char < cipherText.length; char += 2)
plainText += String.fromCharCode(parseInt(cipherText.substr(char, 2), 16) ^ keyInt);
return plainText;
}
function getKey(cipherText) {
var commonChars = 'eotha sinrd luymw fgcbp kvjqxz',
mostLikely = 0,
key = '';
for (var possibleKey = 0; possibleKey <= 127; possibleKey++) {
var possibleMatch = '',
chanceOfMatch = 0;
for (var plainChar = 0; plainChar <= cipherText.length; plainChar += 2)
possibleMatch += String.fromCharCode(parseInt(cipherText.substr(plainChar, 2), 16) ^ possibleKey);
for (var possibleChar = 0; possibleChar < possibleMatch.length; possibleChar++)
for (var commonChar = 0; commonChar < commonChars.length; commonChar++)
if (possibleMatch[possibleChar].toLowerCase() == commonChars[commonChar]) {
chanceOfMatch++;
break;
}
if (chanceOfMatch > mostLikely) {
key = String.fromCharCode(possibleKey);
mostLikely = chanceOfMatch;
}
}
return key;
}
var cipherText = '6B4A52055C4A500257400548444E4C4B4205484A474C49400548445151405704',
key = getKey(cipherText);
console.log('key:', key);
console.log('decrypt:', decrypt(cipherText, key));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment