-
Notifications
You must be signed in to change notification settings - Fork 101
/
blog-encrypt.js
129 lines (83 loc) · 2.63 KB
/
blog-encrypt.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
'use strict';
function decryptAES () {
const pass = String(document.getElementById('pass').value);
try {
var decryptionError = String(document.getElementById('decryptionError').innerHTML);
var noContentError = String(document.getElementById('noContentError').innerHTML);
} catch (e) {
decryptionError = 'Incorrect Password!';
noContentError = 'No content to display!';
}
try {
let content = CryptoJS.AES.decrypt(document.getElementById('encrypt-blog').innerHTML.trim(), pass);
content = content.toString(CryptoJS.enc.Utf8);
content = decodeBase64(content);
content = unescape(content);
if (content === '') {
throw new Error(noContentError); // ???
} else {
document.getElementById('encrypt-blog').style.display = 'inline';
document.getElementById('encrypt-blog').innerHTML = '';
// Use jquery to load some js code
try {
$('#encrypt-blog').html(content);
} catch(e) {
const errorInfo = '<p>'
+ 'Some errors occurred, check the original file please.'
+ 'Detailed exception are shown in console.'
+ '</p>';
console.error(e);
$('#encrypt-blog').html(errorInfo);
}
document.getElementById('hbe-security').style.display = 'none';
if (document.getElementById('toc-div')) {
document.getElementById('toc-div').style.display = 'inline';
}
}
// Call MathJax to render
if(typeof MathJax !== 'undefined') {
try {
MathJax.Hub.Queue(
['resetEquationNumbers', MathJax.InputJax.TeX, ],
['PreProcess', MathJax.Hub, ],
['Reprocess', MathJax.Hub, ]
);
} catch (e) {
console.log('Can\'t render with MathJax');
}
}
} catch (e) {
alert(decryptionError);
console.log(e);
}
}
function htmlDecode (str) {
let s = '';
if (str.length == 0) {
return '';
}
s = str.replace(/>/g, '&');
s = s.replace(/</g, '<');
s = s.replace(/>/g, '>');
s = s.replace(/ /g, ' '); // ??? why not ' '
s = s.replace(/'/g, '\'');
s = s.replace(/"/g, '"');
s = s.replace(/<br>/g, '\n');
return s;
}
function decodeBase64 (content) {
content = CryptoJS.enc.Base64.parse(content);
content = CryptoJS.enc.Utf8.stringify(content);
return content;
}
// Since you decided to use jQuery.
$(document).ready(
function () {
console.log('Registering Enter for decrypt.');
document.getElementById('pass').onkeypress = function (keyPressEvent) {
if (keyPressEvent.keyCode === 13) {
decryptAES();
}
};
}
);