-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypt.js
55 lines (48 loc) · 1.98 KB
/
crypt.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
var vizhener = {
ru : "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ".split(""),
en : "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""),
square : [],
genSqViz : function (lang) {
var l = this[lang], square = [];
for (var i = 0; i < l.length; i++) {
this.square[i] = l.slice(i).concat(l.slice(0, i));
}
},
encryption : function (lang, text, key) {
if (lang !== "ru" && lang !== "en") return false;
if (text.length !== key.length) key = this.genKey(text, key);
this.genSqViz(lang);
var s = "";
for (var i = 0; i < text.length; i++) {
s += this.square[this[lang].indexOf(text[i])][this[lang].indexOf(key[i])];
}
return s;
},
decryption : function (lang, key, cipher) {
if (lang !== "ru" && lang !== "en") return false;
if (cipher.length !== key.length) key = this.genKey(cipher, key);
this.genSqViz(lang);
var s = "";
for (var i = 0; i < cipher.length; i++) {
var row = this[lang].indexOf(key[i])
coll = this.square[row].indexOf(cipher[i]);
s += this[lang][coll];
}
return s;
},
genKey : function (text, key) {
for (let i = 0; i < Math.ceil(text.length/key.length)-1; i++) {
key = key.concat(key);
}
key = key.concat(key.slice(0, text.length - key.length));
console.log(key);
console.log(text);
return key;
}
};
//
// document.write (vizhener.encryption("en", "ATTACKATDAWN", "LEMONLEMONLE") + "<br>"); //(en) шифруем
// document.write (vizhener.decryption("en", "LEMONLEMONLE", "LXFOPVEFRNHR") + "<br>"); //(en) расшифровываем
//
// document.write (vizhener.encryption("ru", "ПОЖАРГОРИМ", "ЖОПАЖОПАЖО") + "<br>"); //(ru) шифруем
// document.write (vizhener.decryption("ru", "ЖОПАЖОПАЖО", "ЦЭЦАЧСЮРПЫ") + "<br>"); //(ru) расшифровываем