Lab 4

lab 4

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Lab 4
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  lab 4
// @author       Felipe Leyton
// @match        https://cripto.tiiny.site/
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // Agregar la librería CryptoJS con SRI al documento HTML
    var script = document.createElement('script');
    script.src = 'https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js';
    script.integrity = 'sha512-E8QSvWZ0eCLGk4km3hxSsNmGWbLtSCSUcewDQPQWZF6pEU8GlT8a5fF32wOl1i8ftdMhssTrF/OhyGWwonTcXA==';
    script.crossOrigin = 'anonymous';
    document.head.appendChild(script);

    script.onload = function(){
    // Parte 1: Obtener la llave
    var bodyText = document.body.innerText;

    function buscarClave(texto) {
        var key = '';
        var sentences = texto.split('.');
        sentences.forEach(function(sentence) {
            var trimmedSentence = sentence.trim();
            if (trimmedSentence.length > 0) {
                key += trimmedSentence.charAt(0);
            }
        });
        return key;
    }

    var key = buscarClave(bodyText);

    if (key) {
        console.log('La llave es: ' + key);
    } else {
        console.log('No se encontró la llave oculta.');
        return;
    }

    // Parte 2: Detectar la cantidad de mensajes cifrados
    let mensajesCifrados = document.querySelectorAll('[class^="M"]');

    console.log('Los mensajes cifrados son: ' + mensajesCifrados.length);

    // Parte 3: Obtener cada mensaje cifrado y descifrarlo
    mensajesCifrados.forEach((mensajeCifrado) => {
        let mensajeCifradoBase64 = mensajeCifrado.id;
        let mensajeCifradoBytes =
            CryptoJS.enc.Base64.parse(mensajeCifradoBase64);
        let mensajeDescifradoBytes = CryptoJS.TripleDES.decrypt(
            { ciphertext: mensajeCifradoBytes },
            CryptoJS.enc.Utf8.parse(key),
            { mode: CryptoJS.mode.ECB }
        );
        let mensajeDescifrado = mensajeDescifradoBytes.toString(
           CryptoJS.enc.Utf8
        );
        console.log(mensajeCifradoBase64 + " " + mensajeDescifrado);
        mensajeCifrado.innerText = mensajeDescifrado;
    });
    }
})();