Encryption-Demo / script.js
Hev832's picture
Create script.js
edb8466 verified
function caesarCipher(str, shift) {
return str.split('').map(char => {
if (char.match(/[a-z]/i)) {
let code = char.charCodeAt();
if ((code >= 65) && (code <= 90))
return String.fromCharCode(((code - 65 + shift) % 26) + 65);
else if ((code >= 97) && (code <= 122))
return String.fromCharCode(((code - 97 + shift) % 26) + 97);
}
return char;
}).join('');
}
function vigenereCipher(text, key) {
let output = '';
key = key.toUpperCase();
for (let i = 0, j = 0; i < text.length; i++) {
let c = text.charAt(i);
if (c.match(/[a-z]/i)) {
let code = text.charCodeAt(i);
if ((code >= 65) && (code <= 90))
c = String.fromCharCode(((code - 65 + key.charCodeAt(j % key.length) - 65) % 26) + 65);
else if ((code >= 97) && (code <= 122))
c = String.fromCharCode(((code - 97 + key.charCodeAt(j % key.length) - 65) % 26) + 97);
j++;
}
output += c;
}
return output;
}
function encryptText() {
const inputText = document.getElementById('inputText').value;
const method = document.getElementById('encryptionMethod').value;
let encryptedText = '';
if (method === 'caesar') {
const shift = 3; // You can customize the shift value
encryptedText = caesarCipher(inputText, shift);
} else if (method === 'vigenere') {
const key = 'KEY'; // You can customize the key
encryptedText = vigenereCipher(inputText, key);
} else if (method === 'aes') {
// AES encryption will require more complex setup, usually handled server-side
encryptedText = 'AES Encryption is more complex and is usually done on the server-side.';
}
document.getElementById('outputText').value = encryptedText;
}