-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyd.ts
35 lines (30 loc) · 1013 Bytes
/
cyd.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function escitalaEncrypt(message: string, turns: number): string {
const columnCount = turns;
const rowCount = Math.ceil(message.length / columnCount);
const matrix: string[][] = new Array(rowCount).fill('').map(_ => new Array(columnCount).fill(''));
let k = 0;
for (let i = 0; i < rowCount; i++) {
for (let j = 0; j < columnCount; j++) {
if (k < message.length) {
matrix[i][j] = message[k];
k++;
}
}
}
return matrix.flat().join('');
}
function escitalaDecrypt(encryptedMessage: string, turns: number): string {
const columnCount = turns;
const rowCount = Math.ceil(encryptedMessage.length / columnCount);
const matrix: string[][] = new Array(rowCount).fill('').map(_ => new Array(columnCount).fill(''));
let k = 0;
for (let j = 0; j < columnCount; j++) {
for (let i = 0; i < rowCount; i++) {
if (k < encryptedMessage.length) {
matrix[i][j] = encryptedMessage[k];
k++;
}
}
}
return matrix.flat().join('');
}