Source Code Algoritma Hill Cipher Matriks Kunci 3x3

Ini source code hill cipher
public int hitungInvers(int[][] matrix) {

        int determinan, x, y, z;

        x = (matrix[0][0] * (matrix[1][1] * matrix[2][2]
                - matrix[1][2] * matrix[2][1]));
        y = (matrix[0][1] * (matrix[1][0] * matrix[2][2]
                - matrix[1][2] * matrix[2][0]));
        z = (matrix[0][2] * (matrix[1][0] * matrix[2][1]
                - matrix[1][1] * matrix[2][0]));
        determinan = x - y + z;

        System.out.println("================== MENGHITUNG INVERS PADA MATRIKS KUNCI =====================");
        System.out.println("Determinan : " + determinan);
        if (determinan == 0) {
            System.out.println("Matrik tidak memiliki invers");
        } else if (determinan < 0) {
            JOptionPane.showMessageDialog(null, "Kunci Tidak Memenuhi Syarat");
        } else {

            int A = (matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2]);
            int B = -(matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0]);
            int C = (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);
            int D = -(matrix[0][1] * matrix[2][2] - matrix[2][1] * matrix[0][2]);
            int E = (matrix[0][0] * matrix[2][2] - matrix[0][2] * matrix[2][0]);
            int F = -(matrix[0][0] * matrix[2][1] - matrix[0][1] * matrix[2][0]);
            int G = (matrix[0][1] * matrix[1][2] - matrix[1][1] * matrix[0][2]);
            int H = -(matrix[0][0] * matrix[1][2] - matrix[0][2] * matrix[1][0]);
            int I = (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]);

            System.out.println(A);
            System.out.println(B);
            System.out.println(C);
            System.out.println(D);
            System.out.println(E);
            System.out.println(F);
            System.out.println(G);
            System.out.println(H);
            System.out.println(I);


            matrikInvers[0][0] = A;
            matrikInvers[0][1] = D;
            matrikInvers[0][2] = G;
            matrikInvers[1][0] = B;
            matrikInvers[1][1] = E;
            matrikInvers[1][2] = H;
            matrikInvers[2][0] = C;
            matrikInvers[2][1] = F;
            matrikInvers[2][2] = I;

            // mencari nilai multiplikative determinan, multiplikatif diperlukan agar hasil invers 
            // tidak menjadi bilangan pecahan. 
            BigInteger Multiplikatif = BigInteger.valueOf(determinan).modInverse(BigInteger.valueOf(modulo));

            int MultiplikatifDet = Multiplikatif.intValue();

            System.out.println("MultiPlikatif = " + MultiplikatifDet);

            for (int i = 0; i < matrikInvers.length; i++) {
                for (int j = 0; j < matrikInvers[i].length; j++) {
                    if (matrikInvers[i][j] < 0) {
                        matrikInvers[i][j] = modulo - (Math.abs(matrikInvers[i][j]) % modulo);
                    } else {
                        matrikInvers[i][j] = matrikInvers[i][j] % modulo;
                    }
                    matrikInvers[i][j] = (int) (matrikInvers[i][j] * MultiplikatifDet);
                    matrikInvers[i][j] = matrikInvers[i][j] % modulo;

                    // System.out.println();
                }
                //System.out.println("");
            }

            System.out.println("Matriks Invers = ");

            // menampilkan invers matrik 
            for (int i = 0; i < matrikInvers.length; i++) {
                for (int j = 0; j ; matrikInvers[i].length; j++) {
                    System.out.print(matrikInvers[i][j] + " ");
                }
                System.out.println("");
            }

        }
        return determinan;
    }

Postingan terkait:

Belum ada tanggapan untuk "Source Code Algoritma Hill Cipher Matriks Kunci 3x3"

Post a Comment