-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayfair.js
148 lines (130 loc) · 4.41 KB
/
playfair.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
function updateMatrixKey() {
var a = "abc";
var alph = "ABCDEFGHIKLMNOPQRSTUVWXYZ";
var key = document.getElementById("key").value.toUpperCase().replace(/[^A-Z_]+/g, "");
key = key.replace('J', 'I');
document.getElementById("key").value = key;
key = removeDuplicate(key + alph);
for (let i=0; i < 25; i++) {
document.getElementById(getRow(i).toString() + getCol(i).toString()).innerHTML = key.charAt(i);
}
}
function crypt(message, matrixKey, isDecrypt = false) {
var text = message.replace("J", "I");
text = text.replace("j", "i");
var result = "";
var map = [];
var k = 1; //1 => encrypt, -1 => decrypt
if (isDecrypt == true) {
k = 4;
}
for (let i=0; i < text.length; i++) {
let codeTxt = text.charCodeAt(i);
if ((codeTxt >= 65 && codeTxt <= 90) || (codeTxt >= 97 && codeTxt <= 122)) {
map.push(i);
}
}
if ((map.length % 2) == 1) {
text += "X";
map.push(text.length - 1);
}
for (let i=0; i < map.length; i+=2) {
let txtA = text.charAt(map[i]);
let codeTxtA = txtA.charCodeAt();
let txtB = text.charAt(map[i+1]);
let codeTxtB = txtB.charCodeAt();
if (txtA == txtB) {
txtB = "X";
}
let posA = matrixKey.indexOf(txtA.toUpperCase());
let posB = matrixKey.indexOf(txtB.toUpperCase());
let chrCphA = "";
let chrChpB = "";
if(getRow(posA) == getRow(posB)) {
chrCphA = matrixKey.charAt((getRow(posA) * 5) + ((getCol(posA) + k) % 5));
chrCphB = matrixKey.charAt((getRow(posB) * 5) + ((getCol(posB) + k) % 5));
} else if(getCol(posA) == getCol(posB)) {
chrCphA = matrixKey.charAt((((getRow(posA) + k) % 5) * 5) + (getCol(posA) % 5));
chrCphB = matrixKey.charAt((((getRow(posB) + k) % 5) * 5) + (getCol(posB) % 5));
} else {
chrCphA = matrixKey.charAt((getRow(posA) * 5) + getCol(posB));
chrCphB = matrixKey.charAt((getRow(posB) * 5) + getCol(posA));
}
if(isLowerCase(codeTxtA)) {
chrCphA = chrCphA.toLowerCase();
}
if(isLowerCase(codeTxtB)) {
chrCphB = chrCphB.toLowerCase();
}
text = text.setCharAt(map[i], chrCphA);
text = text.setCharAt(map[i+1], chrCphB);
}
return text;
}
function btnEncrypt() {
var plainText = document.getElementById("plainText").value;
if (plainText.length == 0) {
document.getElementById("errorMsg").innerHTML = 'Chưa nhập Plain text kìa! :D';
return;
}
var matrixKey = getMatrixKey();
if (matrixKey.length == 0) {
document.getElementById("errorMsg").innerHTML = 'Khóa K không được để trống';
return;
}
var cipherText = crypt(plainText, matrixKey);
document.getElementById("cipherText").value = cipherText;
document.getElementById("errorMsg").innerHTML = "";
}
function btnDecrypt() {
var cipherText = document.getElementById("cipherText").value;
if (cipherText.length == 0) {
document.getElementById("errorMsg").innerHTML = 'Chưa nhập Cipher text kìa!';
return;
}
document.getElementById("cipherText").value = cipherText;
var matrixKey = getMatrixKey();
if (matrixKey.length == 0) {
document.getElementById("errorMsg").innerHTML = 'Khóa K không được để trống nhé!';
return;
}
var plainText = crypt(cipherText, matrixKey, true);
document.getElementById("plainText").value = plainText;
document.getElementById("errorMsg").innerHTML = "";
}
function getCol(i) {
return i % 5;
}
function getRow(i) {
return parseInt(i/5);
}
function getMatrixKey() {
var result = "";
for (let i=0; i < 5; i++) {
for (let j=0; j < 5; j++) {
result += document.getElementById(i.toString() + j.toString()).innerHTML;
}
}
return result;
}
function removeDuplicate(text) {
var result = "";
for (var i = 0; i < text.length; i++) {
if (result.indexOf(text.charAt(i)) == -1) {
result += text.charAt(i);
}
}
return result;
}
function isLowerCase(code) {
if (code >= 97 && code <= 122) {
return true;
}
return false;
}
String.prototype.setCharAt = function (index, chr) {
if(index < this.length) {
return this.substr(0, index) + chr + this.substr(index + 1);
}
return this;
}