-
Notifications
You must be signed in to change notification settings - Fork 54
/
Vigenere.js
72 lines (60 loc) · 1.81 KB
/
Vigenere.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
/*
* Vigenere Cipher
*
* @author lellansin <lellansin@gmail.com>
* @website http://www.lellansin.com/tutorials/ciphers
*/
(function() {
var Vigenere = {};
var TABLE_WIDTH = 26;
var ASCII = {
A: 'A'.charCodeAt()
};
/*
* 生成密表
*/
Vigenere.initTable = function() {
var table = [];
for (var i = 0; i < TABLE_WIDTH; i++) {
var line = [];
for (var j = 0; j < TABLE_WIDTH; j++) {
line[j] = String.fromCharCode(ASCII.A + (i + j) % TABLE_WIDTH);
}
table.push(line);
}
return table;
};
/*
* 加密
*/
Vigenere.encrypt = function(key, words) {
var table = Vigenere.initTable();
var count = 0;
key = key.toUpperCase();
return words.toUpperCase().replace(/[\W]*(\w)[\W]*/g, function(text, ch) {
return table[key[count++ % key.length].charCodeAt() - ASCII.A][ch.charCodeAt() - ASCII.A];
});
};
/*
* 解密
*/
Vigenere.decrypt = function(key, text) {
var count = 0;
key = key.toUpperCase();
return text.toUpperCase().replace(/[\W]*(\w)[\W]*/g, function(match, ch) {
var offset = ch.charCodeAt() - key[count++ % key.length].charCodeAt();
offset >= 0 ? null : offset += TABLE_WIDTH;
return String.fromCharCode(ASCII.A + offset);
});
};
/* -------------------- 测试 -------------------- */
// 本例推算见《密码学基础》(西安电子科技大学出版社) 第5页
var text = "block cipher design principles";
var key = "computer";
var ciphertext;
// 加密
var ciphertext = Vigenere.encrypt(key, text);
console.log(ciphertext);
// 解密
console.log(Vigenere.decrypt(key, ciphertext));
}());