Program Java GUI "Matrik"



import javax.swing.JOptionPane;
public final class Hillchiper extends javax.swing.JFrame {
private static final int m = 26;
private final int[][] M = new int[2][2];
private final int[][] N = new int[2][2];
private final int p[] = new int[100];
private final int c[] = new int[100];
char[] f = new char[100];

public Hillchiper() {
initComponents();
kontrolTbl();}

private static int tambah(int a, int b) {
return (a + b) % m;}
private static int kali(int a, int b) {
return (a * b) % m;}

private static int inversTambah(int a) {
return (m - a);}

private static int inversKali(int a) {
int hasil = -1;
for (int i = 1; i < m; i++) {
if (((a * i) % m) == 1) {
hasil = i;
}}return hasil;}

private static boolean adaInvers(int a) {
boolean hasil = false;
for (int i = 1; i < m; i++) {
if (((a * i) % m) == 1) {
hasil = true;}}
return hasil;}

public void GetM() {
M[0][0] = Integer.parseInt(tf1.getText());
M[0][1] = Integer.parseInt(tf2.getText());
M[1][0] = Integer.parseInt(tf3.getText());
M[1][1] = Integer.parseInt(tf4.getText());}

public int[][] setKunci() {
int K[][] = {
{Integer.parseInt(tf1.getText()),
Integer.parseInt(tf2.getText())},
{Integer.parseInt(tf3.getText()),
Integer.parseInt(tf4.getText())}};
return this.M;}

public int[][] setKunciInvers() {
int K[][] = {
{Integer.parseInt(tf4.getText()), -
Integer.parseInt(tf2.getText())},
{-Integer.parseInt(tf3.getText()),
Integer.parseInt(tf1.getText())}};
return this.N;}

private static boolean CekDeterminan(int[][] M) {
int x = (M[0][0] * M[1][1]) % 26;
int y = (M[0][1] * M[1][1]) % 26;
int z = tambah(x, inversTambah(y));
return adaInvers(z);}

public void kontrolTbl() {
Enkripsi.setEnabled(false);
Dekripsi.setEnabled(false);
Hapus.setEnabled(false);}
public void enablebtn() {
Enkripsi.setEnabled(true);
Dekripsi.setEnabled(true);
Hapus.setEnabled(true);}
private int[] enkrip(int[][] M, int[] p) {
String str;
str = plaintext.getText(); // mengambil nilai string
int i = 0;
int zz = 0;
for (int b = 0; b < str.length() / 2; b++) {
for (int j = 0; j < 2; j++) {
for (int x = 0; x < 2; x++) {
c[i] += M[j][x] * p[x + zz];
// mengalikan kunci dengan nilai konversi huruf ke angka
}
i++;
}
zz += 2;
}
return c;}

private int[] dekrip(int[][] N, int[] c) {
String Btr;
Btr = chiper.getText();
int i = 0;
int zz = 0;
for (int b = 0; b < Btr.length() / 2; b++) {
for (int j = 0; j < 2; j++) {
for (int x = 0; x < 2; x++) {
p[i] += N[j][x] * c[x + zz];
}
i++;
}
zz += 2;
}
return p;}

public int[] setP() {
String str;
int i = 0;
str = plaintext.getText();
for (i = 0; i < str.length(); i++) {
int c1 = str.charAt(i);
p[i] = (c1) - 65;
// Mengisi array p dengan konversi String menjadi bilangan integer
}
return p;}

public int[] setC() {
String str;
int i = 0;
str = plaintext.getText();
for (i = 0; i < str.length(); i++) {
int p1 = str.charAt(i);
c[i] = (p1) - 65;
// Mengisi array p dengan konversi String menjadi bilangan integer
}
return c;}
       pack();
   }// </editor-fold>                        

   private void DekripsiActionPerformed(java.awt.event.ActionEvent evt) {                                         
       // TODO add your handling code here:
       setC();
String Btr;
Btr = plaintext.getText();
enkrip(N, c);
for (int z = 0; z < Btr.length(); z++) {
f[z] = (char) ((p[z] % 26) + 65); // untuk huruf kecil menggunakn angka 97
}
String s = new String(f).trim();
System.out.println("Hasil Dekrip menjadi string = "
+ s);
Oritext.setText(s);
   }                                        

   private void chiperActionPerformed(java.awt.event.ActionEvent evt) {                                       
       // TODO add your handling code here:
   }                                      


   private void CekDeterminanActionPerformed(java.awt.event.ActionEvent evt) {                                              
       // TODO add your handling code here:
      GetM();
if (CekDeterminan(M)) {
JOptionPane.showMessageDialog(null,"Matrix memiliki invers");
} else {
JOptionPane.showMessageDialog(null,"Matrix tidak memiliki invers");
}
if (CekDeterminan(M)) {
enablebtn();
} else {
kontrolTbl();
}
   }                                             

private void HapusActionPerformed(java.awt.event.ActionEvent evt) {                                      
   // TODO add your handling code here:
kontrolTbl();
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
chiper.setText("");
plaintext.setText("");
Oritext.setText("");
}                                     

private void EnkripsiActionPerformed(java.awt.event.ActionEvent evt) {                                         
   // TODO add your handling code here:
   setP();
String str;
str = plaintext.getText();
enkrip(M, p);
for (int z = 0; z < str.length(); z++) {
f[z] = (char) ((c[z] % 26) + 65); // untuk huruf kecil menggunakn angka 97
}
String s = new String(f).trim();
System.out.println("Hasil Enkrip menjadi string = "
+ s);
chiper.setText(s);
}                                        

   public static void main(String args[]) {
       java.awt.EventQueue.invokeLater(new Runnable() {
           public void run() {
               new Hillchiper().setVisible(true);
           }
       });
   }
}

Share on Google Plus

About R Farisy

0 komentar:

Catat Ulasan