Skip to content

Commit

Permalink
add four square cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
eliely committed Oct 12, 2022
1 parent df3f050 commit 2c7577f
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cryptography/FourSquare_cipher/JavaScript/FourSquare_cipher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function encrypt(messageInput, keyword) {
var alphabet = 'abcdefghijklmnopqrstuvwxyz';
var messageOutput = "";

var pos = 0;
while (pos < messageInput.length) {
var m1 = messageInput[pos];
var m2 = '';

if (pos + 1 < messageInput.length) {
m2 = messageInput[pos + 1];
pos += 2;
} else {
m2 = 'Q' // some dummy letter
pos += 1;
}

var idx1 = alphabet.indexOf(m1); // upper-left table
var idx2 = alphabet.indexOf(m2); // lower-right table
var c1 = keyword[0][(5 * Math.floor(idx1 / 5)) + idx2 % 5];
var c2 = keyword[1][(5 * Math.floor(idx2 / 5)) + idx1 % 5];

messageOutput = messageOutput.concat(c1);
messageOutput = messageOutput.concat(c2);
}

return messageOutput;
}

0 comments on commit 2c7577f

Please sign in to comment.