-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtranspo.js
92 lines (83 loc) · 2.1 KB
/
transpo.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// This function create a numerique key ordered by given keyword letters position
function createKeyTab(keyText)
{
var tab=new Array(keyText.length);
var pos,i,j;
var alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
pos=0;
for (j=0;j<26;j++)
{
for (i=0;i<keyText.length;i++)
{
if (keyText.charAt(i)==alphabet.charAt(j))
{
tab[pos]=i;
pos++;
}
}
}
//console.log(tab);
return tab;
}
function transpoTab(text , key, op=0){
let nb, i, lastLine, output, columns, border;
text=fiveTimes(text);
const textLength=text.length, keyLength = key.length;
nb=Math.floor(textLength/keyLength);
lastLine=textLength-nb*keyLength;
const keyTab= createKeyTab(key);
columns=new Array(keyLength);
if(op===0){
for (i=0;i<keyLength;i++) {
if (i<lastLine) border=nb+1; else border=nb;
columns[i]="";
for (j=0;j<border;j++)
{
columns[i]+=text.charAt(j*keyLength+i);
}
}
// On releve par les columns...
output="";
for (i=0; i<keyLength; i++){
output+=columns[keyTab[i]];
}
}
else if(op===1){
columns=new Array(keyLength);
current=0;
for (i=0;i<keyLength;i++)
{
if (keyTab[i]<lastLine) border=nb+1; else border=nb;
columns[keyTab[i]]=text.substring(current,current+border);
// console.log(columns)
current+=border;
}
output="";
for (j=0;j<nb;j++){
for (i=0;i<keyLength;i++) output+=columns[i].charAt(j);
//console.log(output)
}
for (i=0;i<lastLine;i++) output+=columns[i].charAt(nb);
}
//console.log(output);
return output;
}
function fiveTimes(plain){
let ret="", pad= 5 - plain.length % 5;
if( pad !== 5){
plain+= randomCharGenerator(pad);
}
return plain;
}
function randomCharGenerator(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
module.exports = {
transpoTab
};