Skip to content

Commit

Permalink
files added
Browse files Browse the repository at this point in the history
  • Loading branch information
Vedang1510 authored Oct 13, 2023
1 parent 9865656 commit c7e7d6c
Show file tree
Hide file tree
Showing 14 changed files with 1,158 additions and 0 deletions.
56 changes: 56 additions & 0 deletions aes.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
*{
padding: 0px;
margin: 0px;
background-color: #F3FDE8;
}
.card{
border-radius: 8px;
height: 37rem;
width: 94%;
margin-top: 5rem;
margin-left: 3rem;
margin-right: 3rem;
box-shadow: 0px 0px 20px 0px #DAD4B5;
}
.textIp{
padding-top: 5rem;
padding-left: 4rem;
}
.text{
font-size: 1.5rem;
}
#textBar{
width: 25rem;
height: 1.6rem;
font-size: 1rem;
border: #DAD4B5 1px solid;
padding-left: 0.5rem;
margin-left: 1rem;
}
#encrypt-button, #decrypt-button, #encryptButton, #decryptButton{
height: 2.5rem;
width: 6rem;
border: none;
background-color: #DAD4B5;
border-radius: 2px;
}

#encryptedText, #decryptedText{
width: 25rem;
margin-left: 0.6rem;
height: 5rem;
margin-top: 1rem
}
#encryptButton, #decryptButton{
margin-bottom: 2rem;
}
.fileIp{
margin-left: 60%;
margin-top: -24rem;
}
#encryptedFile{
width: 30rem;
height: 12rem;
margin-top: 1rem;
margin-bottom: 2rem;
}
157 changes: 157 additions & 0 deletions aes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="aes.css">
<title>AES Program</title>
</head>
<body>
<div class="card">
<div class="textIp">
<!-- text input -->
<span class="text">Data to encrypt:</span>
<input id="textBar" type="text" placeholder="Enter text to encrypt"><br><br>
<button id="encrypt-button" onclick="encrypt()">Encrypt</button><br><br>

<!-- Output -->
<span class="text">Encrypted Text: </span>
<textarea id="encryptedText"></textarea><br><br>
<button id="decrypt-button" onclick="decrypt()">Decrypt</button><br><br>
<span class="text">Decrypted Text: </span>
<textarea id="decryptedText"></textarea>
</div>
<div class="fileIp">
<!-- File input -->
<input type="file" id="fileInput" />
<button type="button" id="encryptButton">Encrypt</button>
<button type="button" id="decryptButton">Decrypt</button>

<div class="text">Encrypted File</div>
<textarea id="encryptedFile"> </textarea>
<div class="text">Decrypted File</div>
<div id="decryptedFile"></div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
<script>
function encrypt() {
const pass = prompt ('Enter your password');
var encrypted = CryptoJS.AES.encrypt(
document.getElementById("textBar").value,
pass
);
document.getElementById("encryptedText").innerHTML = encrypted;
document.getElementById("decryptedText").innerHTML = "";
}
function decrypt() {
const pass = prompt ('Enter your password');
try{
var decrypted = CryptoJS.AES.decrypt(
document.getElementById("encryptedText").innerHTML,
pass
).toString(CryptoJS.enc.Utf8);
document.getElementById("decryptedText").innerHTML = decrypted;
document.getElementById("encryptedText").innerHTML = "";
if (document.getElementById("decryptedText").innerHTML == ''){
alert('Decryption failed. Check your password.');
}
}
catch(err){
alert('Decryption failed. Check your password.');
}
}
// Get the file input element
const fileInput = document.getElementById('fileInput');

// Get the encrypt and decrypt buttons
const encryptButton = document.getElementById('encryptButton');
const decryptButton = document.getElementById('decryptButton');

// Get the encrypted and decrypted file divs
const encryptedFileDiv = document.getElementById('encryptedFile');
const decryptedFileDiv = document.getElementById('decryptedFile');

// Create a function to encrypt a file
async function encryptFile(file, password) {
// Read the file as a Blob
const fileContent = await file.arrayBuffer();

// Convert the ArrayBuffer to a WordArray (CryptoJS format)
const fileData = CryptoJS.lib.WordArray.create(new Uint8Array(fileContent));

// Encrypt the file data
const encryptedData = CryptoJS.AES.encrypt(fileData, password);

// Get the base64-encoded ciphertext
const encryptedBase64 = encryptedData.toString();

// Return the encrypted base64 string
return encryptedBase64;
}

// Create a function to decrypt a file
function decryptFile(encryptedBase64, password) {
// Decrypt the base64-encoded ciphertext
const decrypted = CryptoJS.AES.decrypt(encryptedBase64, password);

// Convert the WordArray to a Uint8Array
const decryptedBytes = new Uint8Array(decrypted.words.length * 4);
for (let i = 0; i < decrypted.words.length; i++) {
decryptedBytes[i * 4] = (decrypted.words[i] >> 24) & 0xff;
decryptedBytes[i * 4 + 1] = (decrypted.words[i] >> 16) & 0xff;
decryptedBytes[i * 4 + 2] = (decrypted.words[i] >> 8) & 0xff;
decryptedBytes[i * 4 + 3] = decrypted.words[i] & 0xff;
}

// Convert the Uint8Array to a Blob
const decryptedBlob = new Blob([decryptedBytes]);

// Return the decrypted Blob
return decryptedBlob;
}

// Add an event listener to the encrypt button
encryptButton.addEventListener('click', async () => {
// Get the file from the file input element
const file = fileInput.files[0];

// Get the password from the user
const password = prompt('Enter a password to encrypt the file with:');

if (password) {
// Encrypt the file
const encryptedBase64 = await encryptFile(file, password);

// Display the encrypted file in the encrypted file div
encryptedFileDiv.textContent = encryptedBase64;
}
});

// Add an event listener to the decrypt button
decryptButton.addEventListener('click', async () => {
// Get the encrypted file from the encrypted file div
const encryptedBase64 = encryptedFileDiv.textContent;

// Get the password from the user
const password = prompt('Enter the password to decrypt the file with:');

if (password) {
// Decrypt the file
const decryptedBlob = decryptFile(encryptedBase64, password);

// Create a download link for the decrypted file
const downloadLink = document.createElement('a');
downloadLink.href = URL.createObjectURL(decryptedBlob);
downloadLink.download = 'decrypted_file';
downloadLink.textContent = 'Download Decrypted File';

// Display the download link in the decrypted file div
decryptedFileDiv.innerHTML = '';
decryptedFileDiv.appendChild(downloadLink);
}
});
</script>
</body>
</html>
Binary file added arrow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions des.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.card{
border-radius: 8px;
height: 37rem;
width: 94%;
margin-left: 3rem;
margin-right: 3rem;
box-shadow: 0px 0px 20px 0px #DAD4B5;
}
button{
height: 2.5rem;
width: 6rem;
border: none;
background-color: #DAD4B5;
border-radius: 2px;
}
button:hover{
cursor: pointer;
}
#textInput{
width: 28rem;
margin-top: 6rem;
margin-left: 5rem;
}
#output{
margin-left: 5rem;
font-size: 1.3rem;
}
#encryptionKey{
margin-left: 5rem;
margin-top: 1rem;
}
.textInput button{
margin-left: 3rem;
margin-top: 2rem;
}
.fileIp{
margin-left: 60%;
margin-top: -15rem;
}
p{
font-size: 1.5rem;
margin-left: 5rem;
}
97 changes: 97 additions & 0 deletions des.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,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>
Binary file added encrypted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c7e7d6c

Please sign in to comment.