forked from ZoranPandovski/al-go-rithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ZoranPandovski#3328 from eliely/four-square-cipher
Add four square cipher in JavaScript
- Loading branch information
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
cryptography/FourSquare_cipher/JavaScript/FourSquare_cipher.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
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; | ||
} |