Skip to content

Commit

Permalink
minor changes and bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mohitpanthri committed Jun 2, 2024
1 parent 0411aee commit 6eb56e5
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 36 deletions.
2 changes: 2 additions & 0 deletions src/scripts/affine.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Function to perform Affine Cipher encryption
function affineEncrypt(text, key1, key2) {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
text = text.toUpperCase().replace(/\s/g, "");
const textLength = text.length;
let encryptedText = "";

Expand All @@ -27,6 +28,7 @@ function affineEncrypt(text, key1, key2) {
// Function to perform Affine Cipher decryption
function affineDecrypt(text, key1, key2) {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
text = text.toUpperCase().replace(/\s/g, "");
const textLength = text.length;
const modInverseA = modInverse(key1, 26); // Calculate the modular inverse of 'a'

Expand Down
1 change: 1 addition & 0 deletions src/scripts/caesar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Function to perform caesar encryption
export const caesarEncrypt = (text, shift) => {
text = text.toUpperCase().replace(/\s/g, "");
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const modValue = alphabet.length;
const encryptedChars = [];
Expand Down
6 changes: 4 additions & 2 deletions src/scripts/playfair.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ function modifyText(plaintext) {

// Function to perform Playfair encryption
function playfairEncrypt(text, keyword) {
keyword = keyword.toUpperCase().replace(/\s/g, "");
const keySquare = generateKeySquare(keyword);
let encryptedText = "";
let pos1, pos2, row1, row2, col1, col2;

// Replacing any J in the text with I
let replaceJ = text.replace(/J/g, "I");
let replaceJ = text.toUpperCase().replace(/\s/g, "").replace(/J/g, "I");
text = modifyText(replaceJ);

for (let i = 0; i < text.length; i += 2) {
Expand Down Expand Up @@ -84,12 +85,13 @@ function playfairEncrypt(text, keyword) {

// Function to perform Playfair decryption
function playfairDecrypt(text, keyword) {
keyword = keyword.toUpperCase().replace(/\s/g, "");
const keySquare = generateKeySquare(keyword);
let decryptedText = "";
let pos1, pos2, row1, row2, col1, col2;

// Replacing any J in the text with I
text = text.replace(/J/g, "I");
text = text.toUpperCase().replace(/\s/g, "").replace(/J/g, "I");

for (let i = 0; i < text.length; i += 2) {
pos1 = keySquare.indexOf(text[i]);
Expand Down
57 changes: 30 additions & 27 deletions src/scripts/railfence.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
// Function to perform railfence Cipher encryption
function railFenceEncrypt(text, rails) {
if (rails < 2) {
throw new Error("Number of rails must be at least 2.");
text = text.toUpperCase().replace(/\s/g, "");
let i = 0,
j = 0,
l,
counter = 0;
let flag = "down";

l = text.length;
let railMatrix = [];
for (i = 0; i < rails; i++) {
railMatrix[i] = [];
for (j = 0; j < l; j++) railMatrix[i][j] = " ";
}

const textLength = text.length;
const railMatrix = new Array(rails)
.fill("")
.map(() => new Array(textLength).fill(""));
let currentRail = 0;
let direction = 1;

for (let i = 0; i < textLength; i++) {
railMatrix[currentRail][i] = text[i];

if (currentRail === 0) {
direction = 1;
} else if (currentRail === rails - 1) {
direction = -1;
}

currentRail += direction;
i = 0;
j = 0;
while (counter < l) {
railMatrix[i][j] = text[counter];
counter++;
j++;

if (i == rails - 1) flag = "up";
if (i == 0) flag = "down";
if (flag == "down") i++;
if (flag == "up") i--;
}

return railMatrix.flat().join("");
table(railMatrix, rails, l);
return railMatrix.flat().join("").replace(/ /g, "");
}

// Function to perform railfence Cipher decryption
function railFenceDecrypt(text, rails) {
if (rails < 2) {
throw new Error("Number of rails must be at least 2.");
}

text = text.toUpperCase().replace(/\s/g, "");
const textLength = text.length;
const railMatrix = new Array(rails)
.fill("")
Expand Down Expand Up @@ -73,7 +75,8 @@ function railFenceDecrypt(text, rails) {

currentRail += direction;
}

console.log(railMatrix);
table(railMatrix, rails, textLength);
return decryptedText;
}

Expand Down
30 changes: 29 additions & 1 deletion src/scripts/transposition.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
function transpositionEncrypt(plainText, key) {
let cipherText = "";

plainText = plainText.toUpperCase().replace(/\s/g, "");
key = removeRepeatedCharacters(key).toUpperCase().replace(/\s/g, "");

// Track key indices for column arrangement
let keyIndex = 0;

Expand All @@ -28,7 +31,7 @@ function transpositionEncrypt(plainText, key) {
row.push(textArray[currentIndex]);
currentIndex++;
} else {
row.push(""); // Replace empty spaces with empty string
row.push("X"); // Replace empty spaces with empty string
}
}
matrix.push(row);
Expand All @@ -50,6 +53,9 @@ function transpositionEncrypt(plainText, key) {
function transpositionDecrypt(cipherText, key) {
let decryptedMessage = ""; // Initialize variable to store the decrypted message

cipherText = cipherText.toUpperCase().replace(/\s/g, "");
key = removeRepeatedCharacters(key).toUpperCase().replace(/\s/g, "");

// Track key indices for column arrangement
let keyIndex = 0;

Expand Down Expand Up @@ -99,6 +105,28 @@ function transpositionDecrypt(cipherText, key) {
return decryptedMessage;
}

// Function to remove any duplicates from the key
function removeRepeatedCharacters(inputString) {
// Create a Set to store unique characters
const uniqueChars = new Set();

// Use Array.from to convert the string into an array of characters
const charsArray = Array.from(inputString);

// Filter out repeated characters by adding them to the Set
const filteredChars = charsArray.filter((char) => {
if (!uniqueChars.has(char)) {
uniqueChars.add(char);
return true;
}
return false;
});

// Join the filtered characters back into a string
const resultString = filteredChars.join("");

return resultString;
}

// Exporting the functions
export { transpositionEncrypt, transpositionDecrypt };
19 changes: 13 additions & 6 deletions src/scripts/vigenere.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
function vigenereEncrypt(text, keyword) {
// Function to perform vigenere Cipher encryption
function vigenereEncrypt(plainText, keyword) {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const textLength = text.length;
plainText = plainText.toUpperCase().replace(/\s/g, "");
keyword = keyword.toUpperCase().replace(/\s/g, "");
const textLength = plainText.length;
const keywordLength = keyword.length;
let encryptedText = "";

for (let i = 0; i < textLength; i++) {
const char = text[i];
const char = plainText[i];
if (char === " ") {
encryptedText += " ";
continue;
Expand All @@ -28,14 +31,18 @@ function vigenereEncrypt(text, keyword) {
return encryptedText;
}

function vigenereDecrypt(text, keyword) {
// Function to perform vigenere Cipher decryption
function vigenereDecrypt(cipherText, keyword) {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const textLength = text.length;
cipherText = cipherText.toUpperCase().replace(/\s/g, "");
keyword = keyword.toUpperCase().replace(/\s/g, "");
const textLength = cipherText.length;
const keywordLength = keyword.length;
let decryptedText = "";
// let i = 0;

for (let i = 0; i < textLength; i++) {
const char = text[i];
const char = cipherText[i];
if (char === " ") {
decryptedText += " ";
continue;
Expand Down

0 comments on commit 6eb56e5

Please sign in to comment.