Laboratorio 4 cripto

Busca key's y desencripta mensaje

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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());
})();