Laboratorio 4 cripto

Busca key's y desencripta mensaje

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Laboratorio 4 cripto
// @namespace    https://www.example.com
// @version      1.0
// @description  Busca key's y desencripta mensaje
// @match        *
// @grant none
// @require https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js
// ==/UserScript==

(function() {
    'use strict';

    function leerPrimerCaracterMayusculaDeCadaOracion() {
        var textoPagina = document.body.innerText;

        var oraciones = textoPagina.split('.');

        var caracteresJuntos = '';

        for (var i = 0; i < oraciones.length; i++) {
            var oracion = oraciones[i].trim();

            if (oracion !== '' && oracion.length > 0) {
                caracteresJuntos += oracion.charAt(0);
            }
        }

        return caracteresJuntos
    }

    function encuentraEtiquetasDivPorFormato(key) {
        var elementosDiv = document.getElementsByTagName('div');

        var objeto = [];

        var formatoRegex = /^M\d+$/;

        for (var i = 0; i < elementosDiv.length; i++) {
            var div = elementosDiv[i];

            if (formatoRegex.test(div.classList[0])) {
                var mensajeDes = desencriptarMensaje(div.getAttribute('id'), key);
                agregarTextoAlFinalDelBody(mensajeDes)
                objeto.push(div.getAttribute('id') + ' ' + mensajeDes)
            }
        }

        return objeto
    }

    function desencriptarMensaje(mensajeCifrado, llave) {
        const formattedKey = CryptoJS.enc.Utf8.parse(llave);

        const options = {
            mode: CryptoJS.mode.ECB,
            padding: CryptoJS.pad.Pkcs7
        };

        const ciphertext = CryptoJS.enc.Base64.parse(mensajeCifrado);

        const decrypted = CryptoJS.TripleDES.decrypt({ ciphertext }, formattedKey, options);

        const mensajeDesencriptado = decrypted.toString(CryptoJS.enc.Utf8);

        return mensajeDesencriptado;
    }

    function agregarTextoAlFinalDelBody(texto) {
        var div = document.createElement("div");
        var contenido = document.createTextNode(texto);
        div.appendChild(contenido);
        document.body.appendChild(div);
    }

    function resultados(){
        const key = leerPrimerCaracterMayusculaDeCadaOracion();
        const objeto = encuentraEtiquetasDivPorFormato(key);
        console.log('La llave es: ' + key);
        console.log('Los mensajes cifrados son: ' + objeto.length);
        for (var i = 0; i < objeto.length; i++) {
            console.log(objeto[i])
        }
    }

    window.addEventListener('load', resultados());
})();