<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Password Decipher</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            background-color: #333333;
        }
        .container {
            text-align: center;
            background: #ffffff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        input {
            padding: 10px;
            font-size: 16px;
            margin-bottom: 10px;
        }
        button {
            padding: 10px 15px;
            font-size: 16px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        .message {
            margin-top: 15px;
            font-size: 18px;
            white-space: pre-line;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Password Decipher</h1>
        <form onsubmit="event.preventDefault(); checkPassword();">
            <input type="password" id="passwordInput" placeholder="Enter password">
            <br>
            <button type="submit">Submit</button>
        </form>
        <div class="message" id="message"></div>
    </div>

    <script>
        // Helper function to convert ArrayBuffer to Base64
        function arrayBufferToBase64(buffer) {
            const binary = String.fromCharCode.apply(null, new Uint8Array(buffer));
            return window.btoa(binary);
        }

        // Encrypt the success message using AES-GCM
        async function encryptMessage(message) {
            const encoder = new TextEncoder();
            const data = encoder.encode(message);

            // Generate a key for AES encryption (AES-GCM)
            const key = await window.crypto.subtle.generateKey(
                {
                    name: "AES-GCM",
                    length: 256,
                },
                true,
                ["encrypt", "decrypt"]
            );

            // Generate a random initialization vector (IV)
            const iv = window.crypto.getRandomValues(new Uint8Array(12));

            // Encrypt the message using AES-GCM
            const encryptedData = await window.crypto.subtle.encrypt(
                {
                    name: "AES-GCM",
                    iv: iv,
                },
                key,
                data
            );

            return { encryptedMessage: arrayBufferToBase64(encryptedData), key, iv };
        }

        // Decrypt the success message using AES-GCM
        async function decryptMessage(encryptedMessage, key, iv) {
            const decoder = new TextDecoder();
            const encryptedData = new Uint8Array(atob(encryptedMessage).split("").map(c => c.charCodeAt(0)));

            const decryptedData = await window.crypto.subtle.decrypt(
                {
                    name: "AES-GCM",
                    iv: iv,
                },
                key,
                encryptedData
            );

            return decoder.decode(decryptedData);
        }

        async function checkPassword() {
            const password = document.getElementById('passwordInput').value;
            const message = document.getElementById('message');

            const correctPassword = "Pa55word";  // The correct password
            const successMessage = "20-56-74\n53745279"; // Success message (to be encrypted)
            const failureMessage = "Incorrect password. Please try again.";

            if (password === correctPassword) {
                // Encrypt the success message
                const { encryptedMessage, key, iv } = await encryptMessage(successMessage);

                // Store the key and IV in sessionStorage (for simplicity)
                sessionStorage.setItem('encryptionKey', JSON.stringify({ key, iv }));

                message.style.color = "green";
                message.textContent = "Encrypted message: " + encryptedMessage;
            } else {
                message.style.color = "red";
                message.textContent = failureMessage;
            }
        }

        // Decrypt the message if needed
        async function decryptStoredMessage() {
            const storedData = JSON.parse(sessionStorage.getItem('encryptionKey'));

            if (storedData) {
                const { key, iv } = storedData;
                const encryptedMessage = document.getElementById('message').textContent.split(": ")[1];

                const decryptedMessage = await decryptMessage(encryptedMessage, key, iv);
                document.getElementById('message').textContent = "Decrypted message: " + decryptedMessage;
            }
        }

        // Call decryptStoredMessage when the page is loaded and the message is available
        window.onload = decryptStoredMessage;
    </script>
</body>
