Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
multiverseweb authored May 20, 2024
1 parent 5cf2a08 commit eacb0b9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
<meta charset="UTF-8">
<link rel="stylesheet" href="style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Encryptify</title>
<title>Encriptify</title>
</head>

<body>
<div class="container">
<p>Text Encoder/Decoder</p>
<textarea id="inputText" rows="4" placeholder="Enter text to encrypt/decrypt"></textarea>
<textarea id="cycles" placeholder="Key (optional)"></textarea>
<div class="buttons">
<button onclick="encryptText()">Encrypt</button>
<button onclick="decryptText()">Decrypt</button>
Expand Down
54 changes: 38 additions & 16 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
let key = 1; // Initialize key with a default value

function encryptText() {
const text = document.getElementById("inputText").value;
if (!text) {
alert("Please enter some text to encrypt.");
const cycles = parseInt(document.getElementById("cycles").value) || 1; // Use default value of 1 if cycles is not a valid number

if (!text || isNaN(cycles) || cycles <= 0) {
alert("Please enter some text and a valid number of cycles.");
return;
}

let encrypted = text;
for (let i = 0; i < cycles; i++) {
encrypted = encrypt(encrypted);
}

document.getElementById("inputText").value = encrypted;
}

function decryptText() {
const coded = document.getElementById("inputText").value;
const cycles = parseInt(document.getElementById("cycles").value) || 1; // Use default value of 1 if cycles is not a valid number

if (!coded || isNaN(cycles) || cycles <= 0) {
alert("Please enter some text and a valid number of cycles.");
return;
}

let decrypted = coded;
for (let i = 0; i < cycles; i++) {
decrypted = decrypt(decrypted);
}

document.getElementById("inputText").value = decrypted;
}

function encrypt(text) {
let halfLength = Math.floor(text.length / 2);
let parts = [];
parts.push(text.substring(0, halfLength));
parts.push(text.substring(halfLength).split("").reverse().join(""));

let key = Math.floor(Math.random() * 9) + 1; // Random number between 1 and 9
let temp1 = [];

parts.forEach(i => {
let temp2 = "";
for (let j of i) {
Expand All @@ -23,16 +51,10 @@ function encryptText() {
});

let encrypted = temp1[1] + String(key) + temp1[0];
document.getElementById("inputText").value = encrypted;
return encrypted;
}

function decryptText() {
const coded = document.getElementById("inputText").value;
if (!coded) {
alert("Please enter some text to decrypt.");
return;
}

function decrypt(coded) {
const keyIndex = Math.floor(coded.length / 2);
const key = parseInt(coded.charAt(keyIndex));

Expand All @@ -52,14 +74,14 @@ function decryptText() {
});

let decrypted = temp1[1] + temp1[0].split("").reverse().join("");
document.getElementById("inputText").value = decrypted;
return decrypted;
}

function copyText(){
function copyText() {
const copied = document.getElementById("inputText").value;
navigator.clipboard.writeText(copied);
}

function clearText(){
document.getElementById("inputText").value = "";
}
document.getElementById("cycles").value = "";
}
6 changes: 5 additions & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ textarea {
box-shadow: 0px 0px 10px black;
transition-duration: 0.2s;
}
#cycles{
max-height: 1rem;
min-height: 1rem;
}
button{
background-color: rgba(255, 255, 255, 0.204);
padding: 10px;
Expand All @@ -43,4 +47,4 @@ button:hover{
}
textarea:hover{
box-shadow: 0px 0px 0px black;
}
}

0 comments on commit eacb0b9

Please sign in to comment.