Spaces:
Running
Running
File size: 1,874 Bytes
edb8466 |
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 |
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;
}
|