Skip to content

Commit 24d4537

Browse files
committed
add the transposition and double transposition encryption
1 parent 5427ec6 commit 24d4537

File tree

4 files changed

+142
-13
lines changed

4 files changed

+142
-13
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 66 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

caesarCipher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const caesarCipher = (mainString, num, cb) => {
3232
cb(newString);
3333
};
3434

35-
caesarCipher('Zoo Keeper', 2, (result) => {
35+
caesarCipher('Zoo Keeper', -1, (result) => {
3636
console.log('Result is: ' + result);
3737
});
3838

transposition.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
let transposition = (myString, column, cb) => {
2+
3+
createMatrix(myString, column, (err, matrix, row) => {
4+
let transposedMessage = '';
5+
if (err) {
6+
7+
} else {
8+
for (let j=0; j<column; j++) {
9+
for (let i=0; i<row; i++) {
10+
if (matrix[i][j] == null) {
11+
12+
} else {
13+
transposedMessage += matrix[i][j];
14+
}
15+
}
16+
}
17+
18+
return cb(null, transposedMessage);
19+
}
20+
});
21+
};
22+
23+
let createMatrix = (myString, column, cb) => {
24+
myString = myString.split('');
25+
let row,
26+
length = myString.length,
27+
tempRowF = length/column,
28+
tempRowR = parseInt(length/column),
29+
matrix = new Array();
30+
31+
if (tempRowR == tempRowF) {
32+
row = tempRowR;
33+
} else {
34+
row = tempRowR + 1;
35+
}
36+
37+
let index = 0;
38+
39+
for (let i=0; i<row; i++) {
40+
matrix[i] = new Array();
41+
}
42+
43+
for (let i=0; i<row; i++) {
44+
for (let j=0; j<column; j++) {
45+
if (myString[index] == ' ') {
46+
matrix[i][j] = '-';
47+
} else if (index >= length) {
48+
matrix[i][j] = null;
49+
} else {
50+
matrix[i][j] = myString[index];
51+
}
52+
index ++;
53+
}
54+
}
55+
56+
return cb(null, matrix, row);
57+
};
58+
59+
transposition('ABCDEF', 3, (err, encryptMessage) => {
60+
if (err) {
61+
62+
} else {
63+
console.log(encryptMessage);
64+
transposition(encryptMessage, 3, (err, decryptMessage) => {
65+
console.log(decryptMessage);
66+
})
67+
}
68+
});
69+

0 commit comments

Comments
 (0)