-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencryptor.js
88 lines (76 loc) · 2.36 KB
/
encryptor.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
function generateKey(length) {
var result = '';
var characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
function randShift() {
return Math.floor(Math.random() * 52);
}
function reverseString(s){
return s.split("").reverse().join("");
}
function getAsciiValue(text) {
let list = [];
for (let i = 0; i < text.length; i++) {
const num = text.charCodeAt(i);
list.push(num);
}
return list;
}
function encrypt(text) {
const inputLen = text.length;
const input = text;
const key = generateKey(inputLen);
let inputAscii = getAsciiValue(input);
let keyAscii = getAsciiValue(key);
const shift = randShift();
let key2 = '';
for (let i = 0; i < inputLen; i++) {
const num = ((inputAscii[i] + keyAscii[i]) % 128) + 256;
let char = String.fromCharCode(num);
key2 += char;
}
let key2Ascii = getAsciiValue(key2);
let almostEncr = '';
for (let i = 0; i < inputLen; i++) {
const num = (inputAscii[i] + key2Ascii[i]) + 50;
let char = String.fromCharCode(num);
almostEncr += char;
}
let encrypted = '';
for (let i = 0; i < almostEncr.length; i++) {
const num = almostEncr.charCodeAt(i) + shift;
const char = String.fromCharCode(num);
encrypted += char;
}
encrypted = reverseString(encrypted);
const encryptionKey = key2;
return [shift, input, encryptionKey, encrypted];
}
function decrypt(text, userKey, userShift) {
let input = text;
const inputLen = input.length;
const shift = userShift;
const key = userKey;
input = reverseString(input);
const inputAscii = getAsciiValue(input);
const keyAscii = getAsciiValue(key);
let almostDecr = '';
for (let i = 0; i < inputLen; i++) {
const num = (inputAscii[i] - keyAscii[i]) - 50;
const char = String.fromCharCode(num);
almostDecr += char;
}
let decrypted = '';
for (let i = 0; i < inputLen; i++) {
const num = almostDecr.charCodeAt(i) - shift;
const char = String.fromCharCode(num);
decrypted += char;
}
return decrypted;
}
module.exports = {encrypt, decrypt};