Chiffrement et déchiffrement de chaîne selon la technique donnée

<script>

function encryption(s) {

    let l = s.length;

    let b = Math.ceil(Math.sqrt(l));

    let a = Math.floor(Math.sqrt(l));

    let encrypted = '';

    if (b * a < l) {

        if (Math.min(b, a) == b) {

            b = b + 1;

        }

        else {

            a = a + 1;

        }

    }

    

    

    let arr = new Array();

    for (let i = 0; i < a; i++) {

        let temp = [];

        for (let j = 0; j < b; j++) {

            temp.push([])

        }

        arr.push(temp)

    }

    for (let i = 0; i < a; i++) {

        for (let j = 0; j < b; j++) {

            arr[i][j] = " "

        }

    }

    let k = 0;

    

    for (let j = 0; j < a; j++) {

        for (let i = 0; i < b; i++) {

            if (k < l) {

                arr[j][i] = s[k];

            }

            k++;

        }

    }

    

    

    for (let j = 0; j < b; j++) {

        for (let i = 0; i < a; i++) {

            encrypted = encrypted +

                arr[i][j];

        }

    }

    return encrypted;

}

function decryption(s) {

    let l = s.length;

    let b = Math.ceil(Math.sqrt(l));

    let a = Math.floor(Math.sqrt(l));

    let decrypted = '';

    

    

    let arr = new Array();

    for (let i = 0; i < a; i++) {

        let temp = [];

        for (let j = 0; j < b; j++) {

            temp.push([])

        }

        arr.push(temp)

    }

    for (let i = 0; i < a; i++) {

        for (let j = 0; j < b; j++) {

            arr[i][j] = " "

        }

    }

    let k = 0;

    

    for (let j = 0; j < b; j++) {

        for (let i = 0; i < a; i++) {

            if (k < l) {

                arr[j][i] = s[k];

            }

            k++;

        }

    }

    

    

    for (let j = 0; j < a; j++) {

        for (let i = 0; i < b; i++) {

            decrypted = decrypted + arr[i][j];

        }

    }

    return decrypted;

}

let s = "Geeks For Geeks";

let encrypted;

let decrypted;

encrypted = encryption(s);

document.write(encrypted + "<br>");

decrypted = decryption(encrypted);

document.write(decrypted);

</script>

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Aller en haut