Skip to content

Instantly share code, notes, and snippets.

@ebrandel
Last active October 5, 2016 02:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebrandel/097adc791b5d1d6ce863da99263fb328 to your computer and use it in GitHub Desktop.
Save ebrandel/097adc791b5d1d6ce863da99263fb328 to your computer and use it in GitHub Desktop.
OTP
// JavasScript solution to the NSA's OTP puzzle
// https://www.facebook.com/NSACareers/photos/a.10150165394744358.374663.38534064357/10155202632259358/?type=3&theater
var cipherText = "6097703920902805098792458100127006308920278750110017283152904512008635073921961285410397244195102032905201942802717080593227";
var key = "6981642705701301086201207791115091207421138236919216132358913111926129022415841781360483274671901231854407951401635567442416";
var message = "";
// offset this so that letters.charAt(1) returns A and letters.charAt(26) returns Z
var letters = " abcdefghijklmnopqrstuvwxyz";
// Doing the second step here: https://en.wikipedia.org/wiki/One-time_pad#Example
// Stepping through the cipherText and the key one character at a time
// If the cipherText character (c) is less than the key character (k), add 10
for (var i = 0; i<cipherText.length; i++) {
var c = Number(cipherText.charAt(i));
var k = Number(key.charAt(i));
if (c < k) {
c += 10;
}
var p = c - k;
message += p.toString();
}
// message now contains a string of numbers. To convert these to letters, first split them into
// two digit chunks.
var plainTextArray = message.match(/.{1,2}/g);
var answer = "";
// Now loop through each chunk, convert it to a number, and then add the coresponding letter value
// to the answer
plainTextArray.forEach(function(element, index, array) {
// Each two digit number is a letter. So "01" is a, "26" is z. For values > 26
// go around to the start of the alphabet
var letterIndex = Number(element) % 26;
answer += letters[letterIndex];
})
// output the answer
console.log(answer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment