-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbitflip_dec.js
50 lines (40 loc) · 1.24 KB
/
bitflip_dec.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
module.exports = bitflip_dec;
function bitflip_dec(text, key) {
// Initialize variables
let encrypted = "";
let decrypted = "";
let flip = false;
// Loop through each character in the text
for (let i = 0; i < text.length; i++) {
// Get the ASCII value of the character
let charCode = text.charCodeAt(i);
// Flip the bits according to the key
if (key[i % key.length] === "1") {
flip = !flip;
}
// Encrypt the character by flipping the bits
if (flip) {
encrypted += String.fromCharCode(charCode ^ 1);
} else {
encrypted += text[i];
}
}
// Reset the flip variable
flip = false;
// Loop through each character in the encrypted text
for (let i = 0; i < encrypted.length; i++) {
// Get the ASCII value of the character
let charCode = encrypted.charCodeAt(i);
// Flip the bits according to the key
if (key[i % key.length] === "1") {
flip = !flip;
}
// Decrypt the character by flipping the bits
if (flip) {
decrypted += String.fromCharCode(charCode ^ 1);
} else {
decrypted += encrypted[i];
}
}
return decrypted
};