Created
October 11, 2011 04:58
-
-
Save MarcoPolo/1277332 to your computer and use it in GitHub Desktop.
vignere in 25 lines of js
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
function encrypt(mess, key){ | |
var i = (mess.length)/(key.length); | |
while (i>0){ | |
key +=key; | |
i--; | |
} | |
key = key.substring(0, mess.length); | |
console.log('message is',mess) | |
console.log('key is',key.length); | |
var result = '' | |
for (var i=0; i < key.length; i++){ | |
var column = key.charCodeAt(i); | |
var row = mess.charCodeAt(i)-97; //subtract 97 to just get the difference from a | |
var sum=row+column; | |
if (sum > 122){ | |
sum=((row+column - 123) + 97); | |
console.log('too big'); | |
} | |
result += String.fromCharCode(sum) | |
} | |
console.log(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment