Skip to content

Commit

Permalink
Merge pull request ZoranPandovski#3328 from eliely/four-square-cipher
Browse files Browse the repository at this point in the history
Add four square cipher in JavaScript
  • Loading branch information
ZoranPandovski authored Oct 13, 2022
2 parents df3f050 + 2c7577f commit e76d224
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 e76d224

Please sign in to comment.