-
Notifications
You must be signed in to change notification settings - Fork 0
/
des.html
97 lines (92 loc) · 3.92 KB
/
des.html
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
<!DOCTYPE html>
<html>
<head>
<title>DES File Encryption and Decryption</title>
<script src="sjcl.js"></script>
<link rel="stylesheet" href="des.css">
<script src="https://crypto.stanford.edu/sjcl/sjcl.js"></script>
</head>
<body>
<center><h1>DES File Encryption and Decryption</h1></center>
<div class="card">
<div class="textInput">
<textarea id="textInput" rows="5" cols="50" placeholder="Enter your text here..."></textarea><br>
<input type="text" id="encryptionKey" placeholder="Enter the encryption key..." /><br>
<button onclick="encryptText()">Encrypt</button>
<button onclick="decryptText()">Decrypt</button><br>
<p>Result</p>
<div id="output"></div>
</div>
<div class="fileIp">
<input type="file" id="fileInput" accept=".txt" />
<button onclick="encryptFile()">Encrypt File</button>
<button onclick="decryptFile()">Decrypt File</button><br>
<a id="downloadLink" style="display: none;" download="decrypted.txt">Download Decrypted File</a>
</div>
</div>
</body>
<script>
function encryptFile() {
const fileInput = document.getElementById("fileInput");
const file = fileInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (event) {
const fileContent = event.target.result;
const encryptedData = sjcl.encrypt("YOUR_ENCRYPTION_KEY", fileContent);
saveFile(encryptedData, "encrypted.txt");
};
reader.readAsText(file);
}
}
function decryptFile() {
const fileInput = document.getElementById("fileInput");
const file = fileInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (event) {
const fileContent = event.target.result;
try {
const decryptedData = sjcl.decrypt("YOUR_ENCRYPTION_KEY", fileContent);
saveFile(decryptedData, "decrypted.txt");
} catch (error) {
alert("Decryption failed. Please make sure you provide the correct encryption key.");
}
};
reader.readAsText(file);
}
}
function saveFile(data, fileName) {
const blob = new Blob([data], { type: "text/plain" });
const url = window.URL.createObjectURL(blob);
const downloadLink = document.getElementById("downloadLink");
downloadLink.href = url;
downloadLink.style.display = "block";
downloadLink.download = fileName;
}
function encryptText() {
const textInput = document.getElementById("textInput").value;
const encryptionKey = document.getElementById("encryptionKey").value;
if (textInput && encryptionKey) {
const encryptedText = sjcl.encrypt(encryptionKey, textInput);
document.getElementById("output").innerText = `Encrypted Text: ${encryptedText}`;
} else {
alert("Please enter text and an encryption key.");
}
}
function decryptText() {
const encryptedText = document.getElementById("output").innerText;
if (encryptedText) {
const encryptionKey = document.getElementById("encryptionKey").value;
try {
const decryptedText = sjcl.decrypt(encryptionKey, encryptedText.split("Encrypted Text: ")[1]);
document.getElementById("output").innerText = `Decrypted Text: ${decryptedText}`;
} catch (error) {
alert("Decryption failed. Please make sure you provide the correct encryption key.");
}
} else {
alert("No encrypted text to decrypt.");
}
}
</script>
</html>