-3
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>File Encryption</title>
</head>
<body>
    <h1>File Encryption</h1>

    <!-- File Upload Input -->
    <label for="file">Upload a File:</label>
    <input type="file" id="file"><br>

    <!-- Public Key Upload Input -->
    <label for="public-key">Upload Public Key (PEM format):</label>
    <input type="file" id="public-key"><br>

    <!-- Generate Session Key Button -->
    <button id="generate-session-key">Generate Session Key</button>

    <!-- Encrypt File Button -->
    <button id="encrypt-file">Encrypt File</button>

    <!-- Download Encrypted File Link -->
    <a id="download-encrypted-file" style="display: none;" download="encrypted_file.bin">Download Encrypted File</a>

    <!-- Download Encrypted Session Key Link -->
    <a id="download-encrypted-session-key" style="display: none;" download="encrypted_session_key.bin">Download Encrypted Session Key</a>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/elliptic/6.5.4/elliptic.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/forge/0.10.0/forge.min.js"></script>
    <script>
        let fileData;
        let publicKeyPEM;
        let sessionKey;

        document.getElementById("file").addEventListener("change", function(event) {
            const file = event.target.files[0];
            const reader = new FileReader();
            reader.onload = function () {
                fileData = new Uint8Array(reader.result);
            };
            reader.readAsArrayBuffer(file);
        });

        document.getElementById("public-key").addEventListener("change", function(event) {
            const publicKeyFile = event.target.files[0];
            const reader = new FileReader();
            reader.onload = function () {
                publicKeyPEM = reader.result;
            };
            reader.readAsText(publicKeyFile);
        });

        document.getElementById("generate-session-key").addEventListener("click", function() {
            // Generate a random session key (AES key)
            sessionKey = CryptoJS.lib.WordArray.random(16); // 128-bit key
            alert("Session Key Generated!");
        });

        document.getElementById("encrypt-file").addEventListener("click", function() {
            if (!fileData) {
                alert("Please select a file to encrypt.");
                return;
            }

            if (!publicKeyPEM) {
                alert("Please upload a public key in PEM format.");
                return;
            }

            if (!sessionKey) {
                alert("Please generate a session key.");
                return;
            }

            // Encrypt the file content using the session key (AES)
            const encryptedFile = CryptoJS.AES.encrypt(CryptoJS.enc.Hex.parse(bytesToHexString(fileData)), sessionKey, {
                mode: CryptoJS.mode.ECB,
            });

            // Encrypt the session key using the recipient's public key (RSA)
            const publicKey = forge.pki.publicKeyFromPem(publicKeyPEM);
            const encryptedSessionKey = publicKey.encrypt(sessionKey.toString(), "RSA-OAEP");

            // Download the encrypted file
            const encryptedFileBlob = new Blob([encryptedFile.toString()], { type: "application/octet-stream" });
            const encryptedFileUrl = URL.createObjectURL(encryptedFileBlob);
            const downloadEncryptedFileLink = document.getElementById("download-encrypted-file");
            downloadEncryptedFileLink.href = encryptedFileUrl;
            downloadEncryptedFileLink.style.display = "block";

            // Download the encrypted session key
            const encryptedSessionKeyBlob = new Blob([hexStringToBytes(encryptedSessionKey.toString())], { type: "application/octet-stream" });
            const encryptedSessionKeyUrl = URL.createObjectURL(encryptedSessionKeyBlob);
            const downloadEncryptedSessionKeyLink = document.getElementById("download-encrypted-session-key");
            downloadEncryptedSessionKeyLink.href = encryptedSessionKeyUrl;
            downloadEncryptedSessionKeyLink.style.display = "block";
        });

        // Function to convert an ArrayBuffer to a hex string
        function bytesToHexString(bytes) {
            return Array.from(new Uint8Array(bytes), byte => byte.toString(16).padStart(2, '0')).join('');
        }

        // Function to convert a hex string to bytes
        function hexStringToBytes(hexString) {
            const bytes = [];
            for (let i = 0; i < hexString.length; i += 2) {
                bytes.push(parseInt(hexString.substr(i, 2), 16));
            }
            return new Uint8Array(bytes);
        }
    </script>
</body>
</html>

In details section i have provided what i have done with complete code
i even added debugging statements
I am not able to figure out what the problem is
ERROR:
-----------------------
Uncaught Error: Encrypted message length is invalid.

at c.rsa.decrypt

at Object.decrypt (forge.

at HTML ButtonE1ement. (receiver. html:85 :48)

This is my final year project I need help

0 Answers0