// Function to convert text to binary art function convertTextToBinaryArt(text) { const binaryArt = []; for (let i = 0; i < text.length; i++) { const char = text.charAt(i); const charCode = char.charCodeAt(0); let binary = charCode.toString(2).padStart(8, '0'); binary = binary.replace(/0/g, '⬛'); // Replace '0' with black square symbol binary = binary.replace(/1/g, '⬜'); // Replace '1' with white square symbol binaryArt.push(binary); } return binaryArt.join('\n'); } // Function to convert binary art back to text function convertBinaryArtToText(binaryArt) { const lines = binaryArt.split('\n'); let text = ''; for (let i = 0; i < lines.length; i++) { const line = lines[i]; const binaryChars = line.split(' '); for (let j = 0; j < binaryChars.length; j++) { const binaryChar = binaryChars[j]; const binaryCode = binaryChar.replace(/⬛/g, '0').replace(/⬜/g, '1'); const charCode = parseInt(binaryCode, 2); const char = String.fromCharCode(charCode); text += char; } } return text; } // Function to handle form submission function handleSubmit(event) { event.preventDefault(); const textInput = document.getElementById('text-input'); const binaryArtOutput = document.getElementById('binary-art-output'); const encryptionKeyInput = document.getElementById('encryption-key-input'); const text = textInput.value; const encryptionKey = parseInt(encryptionKeyInput.value); const encryptedText = encryptText(text, encryptionKey); const binaryArt = convertTextToBinaryArt(encryptedText); binaryArtOutput.value = binaryArt; } // Function to encrypt the text using the Caesar cipher function encryptText(text, encryptionKey) { let encryptedText = ''; for (let i = 0; i < text.length; i++) { const char = text.charAt(i); const charCode = char.charCodeAt(0); const encryptedCharCode = (charCode + encryptionKey) % 256; encryptedText += String.fromCharCode(encryptedCharCode); } return encryptedText; } // Function to decrypt the text using the Caesar cipher function decryptText(text, encryptionKey) { let decryptedText = ''; for (let i = 0; i < text.length; i++) { const char = text.charAt(i); const charCode = char.charCodeAt(0); const decryptedCharCode = (charCode - encryptionKey + 256) % 256; decryptedText += String.fromCharCode(decryptedCharCode); } return decryptedText; } // Function to handle decryption form submission function handleDecrypt(event) { event.preventDefault(); const binaryArtInput = document.getElementById('binary-art-input'); const decryptedTextOutput = document.getElementById('decrypted-text-output'); const encryptionKeyInput = document.getElementById('decryption-key-input'); const binaryArt = binaryArtInput.value; const encryptionKey = parseInt(encryptionKeyInput.value); const decryptedBinaryArt = convertBinaryArtToText(binaryArt); const decryptedText = decryptText(decryptedBinaryArt, encryptionKey); decryptedTextOutput.value = decryptedText; }