Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Vigenere Cipher #387

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions VigenereCipher/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
let letters = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];

let table = [letters]

let plaintext = "ATTACKATDAWN";
let key = "LEMON";
let repeatKey = "";
let solution = "";

let remainder = plaintext.length % key.length;
let round = Math.round(plaintext.length / key.length);

for(let i = 0; i < round; i++){
repeatKey += key;
}

for(let i = 0; i < remainder; i++){
repeatKey += key[i];
}


//Set up tables
function getLetterSet(set, index){
let first = set[0];
let newSet = [];
for(let i = 1; i <= 25; i++){
newSet.push(set[i])
}
newSet.push(first);
table.push(newSet);
if(index <= 23){
getLetterSet(newSet, index + 1);
}else{
return table;
}
}

let newTable = getLetterSet(letters, 0);


//Solution
for(let i = 0; i < plaintext.length; i++){
let plainLetter = plaintext[i];
let keyLetter = repeatKey[i];
let rowNum = letters.indexOf(keyLetter);
let colNum = letters.indexOf(plainLetter);
let cipher = table[rowNum][colNum];
solution += cipher;
}

console.log(solution)