-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptlet.js
48 lines (44 loc) · 1.04 KB
/
cryptlet.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
function cryptlet() {
if( document.activeElement && document.activeElement.type != undefined) {
cryptlet_encrypt();
}
else {
cryptlet_decrypt();
}
}
function cryptlet_encrypt()
{
var ae = document.activeElement ;
if( ae.type == 'textarea' || ae.type == 'text')
{
var s=ae.selectionStart,e=ae.selectionEnd;
if( e>0 && e>s )
{
console.log("SEL: "+ s+", "+e);
text = ae.value.substring(s,e);
pass = prompt("To encrypt, enter password :");
code = sjcl.encrypt(pass, text);
ae.value = ae.value.substring(0,s) + code + ae.value.substring(e);
}
}
}
function cryptlet_decrypt()
{
console.log('decrypt');
var code = '';
if (window.getSelection) {
code = window.getSelection().toString();
} else if (document.getSelection) {
code = document.getSelection().toString();
} else if (document.selection) {
code = document.selection.createRange().text;
}
console.log('code: '+code.length);
if( code.length>0 )
{
pass = prompt("To decrypt, enter password :");
clear = sjcl.decrypt(pass, code);
alert(clear);
}
}
cryptlet();