Last active
September 6, 2017 02:25
-
-
Save moo3/4ef9c29e2ddb0bd0d40f to your computer and use it in GitHub Desktop.
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
var refText, encodedMessage, decodedMessage; | |
function decodeMessage(ref, key) { | |
var keyArr = key.split(''); | |
var keyArrCopy = [].concat(keyArr); | |
var dict = []; | |
var result = ''; | |
while(keyArr.length > 0) { | |
var k = keyArr.splice(0, keyArr.shift()).join(''); | |
var c = ref.charAt( Number(k) ); | |
result += c; | |
} | |
return result; | |
} | |
function encodeMessage(ref, message) { | |
var c, r, t, refMap = {}, key = []; | |
ref.toLowerCase().split('').map( function(char, i) { | |
refMap[char] && refMap[char].push(i) || (refMap[char] = [i]); | |
}); | |
message.toLowerCase().split('').map( function(char, i) { | |
c = refMap[char] || []; | |
r = getRandomInt(0,c.length-1); | |
t = (c[ r ].toString().length).toString() + c[ r ].toString(); | |
key.push( t ); | |
}); | |
return key.join(''); | |
} | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} | |
refText = 'A quick movement of the enemy will jeopardize six gunboats.'; | |
encodedMessage = encodeMessage(refText, 'encoded message here'); | |
decodedMessage = decodeMessage(refText, encodedMessage); | |
console.log('Encoded Key: ', encodedMessage); | |
console.log('Decoded Message: ', decodedMessage); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment