-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogic.js
More file actions
87 lines (70 loc) · 1.81 KB
/
logic.js
File metadata and controls
87 lines (70 loc) · 1.81 KB
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
//Initialize
function passCheck() {
if (!(document.getElementById('password').value == "")) {
return true;
} else {
alert("Please input a password!");
return false;
}
}
function encrypt() {
if (passCheck()) {
document.getElementById('result').value = Jcrypter.encrypt(document.getElementById('password').value, document.getElementById('cipher').value);
}
}
function decrypt() {
if (passCheck()) {
document.getElementById('result').value = Jcrypter.decrypt(document.getElementById('password').value, document.getElementById('cipher').value);
}
}
//JavasCrypter
Jcrypter = {};
function toAscii(s, bool) {
var r = [];
var chars = s.split("");
for (var i = 0; i < s.length; i++) {
r.push(chars[i].charCodeAt(0));
}
return r;
}
function fromAscii(c) {
var r = "";
for (var i = 0; i < c.length; i++) {
r += String.fromCharCode(c[i]);
}
return r;
}
function obf(str) {
var keyGen = new Math.seedrandom(str);
var seed = Math.round(keyGen() * 10000000000).toString().split("");
return seed;
}
//End of Utilities
Jcrypter.encrypt = function(pw, txt) {
var seed = obf(pw);
var txtAscii = toAscii(txt);
for (var x = 0, inc = 0; x < txt.length; x++, inc++) {
if (inc > seed.length - 1) {
inc = 0;
}
txtAscii[x] = parseInt(txtAscii[x]) + parseInt(seed[inc]);
}
var encTxt = fromAscii(txtAscii);
return encTxt;
}
Jcrypter.decrypt = function(pw, encTxt) {
var seed = obf(pw);
var txtAscii = toAscii(encTxt);
var rem = (encTxt.length - 1) % (seed.length);
if (rem == 0) {
rem = seed.length - 1;
}
for (var x = encTxt.length - 1, inc = rem; x >= 0; x--, inc--) {
if (inc < 0) {
inc = seed.length - 1;
}
txtAscii[x] = parseInt(txtAscii[x]) - parseInt(seed[inc]);
}
var decTxt = fromAscii(txtAscii);
return decTxt;
}