-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
135 lines (117 loc) · 4.17 KB
/
script.js
File metadata and controls
135 lines (117 loc) · 4.17 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
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
130
131
132
133
134
const chars = " " + "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" + "0123456789" + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
let key = [];
// Load or generate key
function loadKey() {
const storedKey = localStorage.getItem("cipherKey");
if (storedKey) {
key = JSON.parse(storedKey);
document.getElementById("currentKey").innerText = "Custom Key (Loaded)";
} else {
generateKey();
}
}
// Generate new key
function generateKey() {
key = chars.split("");
key.sort(() => Math.random() - 0.5);
localStorage.setItem("cipherKey", JSON.stringify(key));
document.getElementById("currentKey").innerText = "Default Key";
}
// Upload Cipher Key
function uploadCipherKey() {
const file = document.getElementById("uploadKey").files[0];
if (file) {
const reader = new FileReader();
reader.onload = function(event) {
try {
key = JSON.parse(event.target.result);
localStorage.setItem("cipherKey", JSON.stringify(key));
document.getElementById("currentKey").innerText = "Custom Key (Uploaded)";
alert("Cipher Key Uploaded Successfully!");
} catch (error) {
alert("Invalid Cipher Key file! Please upload a valid key.");
}
};
reader.readAsText(file);
}
}
// Download Cipher Key
function downloadCipherKey() {
let blob = new Blob([JSON.stringify(key)], { type: "application/json" });
let link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = "cipher_key.json";
link.click();
}
// Reset Cipher Key with Confirmation
function resetCipherKey() {
if (confirm("⚠️ Are you sure? This will reset the encryption key and old messages can't be decrypted!")) {
generateKey();
alert("Cipher Key has been reset successfully & New encryption key generated!!");
}
}
// Real-time Encrypt
function realTimeEncrypt() {
let plainText = document.getElementById("plainText").value;
document.getElementById("cipherText").value = [...plainText].map(letter => key[chars.indexOf(letter)] || letter).join("");
}
// Real-time Decrypt
function realTimeDecrypt() {
let cipherText = document.getElementById("decryptInput").value;
document.getElementById("decryptedText").value = [...cipherText].map(letter => chars[key.indexOf(letter)] || letter).join("");
}
// Reset Fields
function resetFields() {
document.getElementById("plainText").value = "";
document.getElementById("cipherText").value = "";
document.getElementById("decryptInput").value = "";
document.getElementById("decryptedText").value = "";
}
// Copy to Clipboard
function copyToClipboard(id) {
let text = document.getElementById(id);
text.select();
document.execCommand("copy");
alert("Copied to clipboard!");
}
// Paste from Clipboard
function pasteText(id) {
navigator.clipboard.readText()
.then(text => {
document.getElementById(id).value = text;
})
.catch(err => {
alert("Failed to paste! Clipboard permission required.");
});
}
// Function to download Encrypted/Decrypted text as a file
function downloadText(id, fileName) {
let text = document.getElementById(id).value;
if (text.trim() === "") {
alert("Nothing to download! Please enter text first.");
return;
}
let blob = new Blob([text], { type: "text/plain" });
let link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = fileName;
link.click();
}
// Get IPv4 Address
fetch('https://api.ipify.org?format=json')
.then(response => response.json())
.then(data => document.getElementById("user-ip").innerText = "Your IP is: " + data.ip);
loadKey();
// Function to update and display the visit count
function updatePageVisits() {
let visits = localStorage.getItem("pageVisits");
if (visits === null) {
visits = 1;
} else {
visits = parseInt(visits) + 1;
}
localStorage.setItem("pageVisits", visits);
document.getElementById("visit-counter").innerText = "Page Visits: " + visits;
}
// Run the function when the page loads
updatePageVisits();