a

mail@pastecode.io avatar
unknown
javascript
2 years ago
6.8 kB
2
Indexable
Never
var dataArr = [];
var listAscii = {
    "a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7, "i": 8, "j": 9,
    "k": 10, "l": 11, "m": 12, "n": 13, "o": 14, "p": 15, "q": 16, "r": 17, "s": 18, "t": 19,
    "u": 20, "v": 21, "w": 22, "x": 23, "y": 24, "z": 25,
    "A": 100, "B": 101, "C": 102, "D": 103, "E": 104, "F": 105, "G": 106, "H": 107, "I": 108, "J": 109,
    "K": 110, "L": 111, "M": 112, "N": 113, "O": 114, "P": 115, "Q": 116, "R": 117, "S": 118, "T": 119,
    "U": 120, "V": 121, "W": 122, "X": 123, "Y": 124, "Z": 125
};

// var listAscii = {
//     "a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7, "i": 8, "j": 9,
//     "k": 10, "l": 11, "m": 12, "n": 13, "o": 14, "p": 15, "q": 16, "r": 17, "s": 18, "t": 19,
//     "u": 20, "v": 21, "w": 22, "x": 23, "y": 24, "z": 25,
//     "A": -1, "B": -2, "C": -3, "D": -4, "E": -5, "F": -6, "G": -7, "H": -8, "I": -9, "J": -10,
//     "K": -11, "L": -12, "M": -13, "N": -14, "O": -15, "P": -16, "Q": -17, "R": -18, "S": -19, "T": -20,
//     "U": -21, "V": -22, "W": -23, "X": -24, "Y": -25, "Z": -26
// };

function enkripsi(){
    let deskripsi = document.getElementById("addDeskripsi").value.split("");
    let tempKunci = document.getElementById("addKey").value;
    let kunci = tempKunci.padEnd(deskripsi.length, tempKunci);
    
    // UBAH KATA2 JADI ANGKA ASCII
    let asciiDeskripsi = [];
    let flag = 0;
    for (let i = 0; i < deskripsi.length; i++) {
        for(let key in listAscii){
            if (deskripsi[i] == key) {
                asciiDeskripsi.push(listAscii[key]);
                flag = 1;
            }
        }
        if (flag == 0) {
            asciiDeskripsi.push(deskripsi[i]);
        }
        flag = 0;
    }
    console.log(asciiDeskripsi);

    // UBAH KUNCI KE ASCII
    let asciiKunci = [];
    for (let i = 0; i < kunci.length; i++) {
        for(let key in listAscii){
            if (kunci[i] == key) {
                asciiKunci.push(listAscii[key]);
            }
        }
    }
    console.log(asciiKunci);

    // HITUNG KALO LBH DR 25 DIMOD
    let asciiEnkripsi = [];
    for (let i = 0; i < asciiKunci.length; i++) {
        if (typeof asciiDeskripsi[i] == "string") {
            asciiEnkripsi.push(asciiDeskripsi[i]);
        }
        else{
            if (asciiDeskripsi[i] > 99) {
                let jumlah = asciiDeskripsi[i] + asciiKunci[i];
                if (jumlah > 125) {
                    jumlah %= 25;
                    jumlah += 99;
                }
                asciiEnkripsi.push(jumlah);
            }
            else{
                let jumlah = asciiDeskripsi[i] + asciiKunci[i];
                if (jumlah >= 25) {
                    jumlah %= 26;
                }
                asciiEnkripsi.push(jumlah);
            }
        }
    }
    console.log(asciiEnkripsi);

    // ANGKA ASCII JADI HURUF
    let hasilEnkripsi = "";
    for (let i = 0; i < asciiEnkripsi.length; i++) {
        if (typeof asciiEnkripsi[i] == "string") {
            hasilEnkripsi += asciiEnkripsi[i];
        }
        else{
            for(let key in listAscii){
                if (asciiEnkripsi[i] == listAscii[key]) {
                    hasilEnkripsi += key;
                }
            }
        }
    }
    dataArr.push(hasilEnkripsi);
    console.log(dataArr);
    dekripsi(hasilEnkripsi, tempKunci);
}


function dekripsi(pkalimat, pKunci){

    // UBAH KUNCI JADI PANJANG
    let kunci = pKunci.padEnd(pkalimat.length, pKunci);
    let asciiKunci = [];
    for (let i = 0; i < kunci.length; i++) {
        for(let key in listAscii){
            if (kunci[i] == key) {
                asciiKunci.push(listAscii[key]);
            }
        }
    }
    console.log(asciiKunci);

    // UBAH KALIMAT RANDOM JADI ASCII
    let asciiEnkripsi = [];
    let flag = 0;
    for (let i = 0; i < pkalimat.length; i++) {
        for(let key in listAscii){
            if (pkalimat[i] == key) {
                // if (listAscii[key] < 0) {
                //     asciiEnkripsi.push(listAscii[key] - asciiKunci[i]);
                //     flag = 1;
                // }
                // else{
                    // let jumlah = listAscii[key] + 25;
                    // if (jumlah >= 25) {
                    //     asciiEnkripsi.push(jumlah % 25);
                    // }
                    // else{
                    //     asciiEnkripsi.push(jumlah);
                    // }
                    // flag = 1;
                //}
                if (listAscii[key] > 99) {
                    let jumlah = listAscii[key] + 25;
                    if (jumlah >= 125) {
                        asciiEnkripsi.push((jumlah % 25) + 100);
                    }
                    else{
                        asciiEnkripsi.push(jumlah + 100);
                    }
                    flag = 1;
                }
                else{
                    let jumlah = listAscii[key] + 26;
                    if (jumlah > 25) {
                        asciiEnkripsi.push(jumlah % 26);
                    }
                    else{
                        asciiEnkripsi.push(jumlah);
                    }
                    flag = 1;
                }
            }
        }
        if (flag == 0) {
            asciiEnkripsi.push(pkalimat[i]);
        }
        flag = 0;
    }
    console.log(asciiEnkripsi);

    // DAPETIN ASCII ASLI KALIMAT
    let asciiDeskripsi = [];
    for (let i = 0; i < asciiKunci.length; i++) {
        if (typeof asciiEnkripsi[i] == "string") {
            asciiDeskripsi.push(asciiEnkripsi[i]);
            // console.log(asciiEnkripsi[i]);
        }
        else{
            let jumlah = asciiEnkripsi[i] - asciiKunci[i];
            // if (jumlah == 0 || jumlah == 100) {
            //     jumlah += 25;
            // }
            // else 
            if (jumlah < 100 && jumlah > 25) {
                jumlah %= 25;
                jumlah += 100;
            }
            else{
                if (jumlah < 0) {
                    jumlah += 26;
                }
            }
            asciiDeskripsi.push(jumlah);
        }
    }
    console.log(asciiDeskripsi);

    // UBAH ASCII JADI KALIMAT
    let hasilKalimat = "";
    for (let i = 0; i < asciiDeskripsi.length; i++) {
        if (typeof asciiDeskripsi[i] == "string") {
            hasilKalimat += asciiDeskripsi[i];
        }
        else{
            for(let key in listAscii){
                if (asciiDeskripsi[i] == listAscii[key]) {
                    hasilKalimat += key;
                }
            }
        }
    }
    console.log(hasilKalimat);
}