Thursday, March 5, 2015

Program java Caesar chiper



KODE CAESAR CHIPER

//**ADD** KODE

public class NewJFrame extends javax.swing.JFrame {

    /**
     * Creates new form NewJFrame
     */
    public NewJFrame() {
        initComponents();
    }


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

//**
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
K=Integer.parseInt(jTextField1.getText().trim());
plainText=Plain.getText();
cipherText=caesarCipher(plainText,K);
Cipher.setText(cipherText);
        // TODO add your handling code here:
}                                       

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                        
K=Integer.parseInt(jTextField1.getText().trim());
String cipherText=Cipher.getText();
oriText=dekripCipher(cipherText);
Original.setText(oriText);
        // TODO add your handling code here:
}                                       

private int K;
private String plainText,cipherText,oriText;
private char[]Kar={'A','B','C','D','E', 'F','G','H','I','J',
                   'K','L','M','N','O', 'P','Q','R','S','T',
                   'U','V','W','X','Y', 'Z'};

    // Variables declaration - do not modify                    
    private javax.swing.JTextArea Cipher;
    private javax.swing.JTextArea Original;
    private javax.swing.JTextArea Plain;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                  

private String caesarCipher(String plainText, int K) {
String hasil="";
int[] KodeAsal=new int[26];
for(int i=0;i<plainText.length();i++){
    char C=plainText.charAt(i);
    if((C>='A')&&(C<='Z') ) C=Kar [ enkrip( C,K) ];
    hasil+=C;
   }
   return hasil;
}
 
private int enkrip(char c, int K )
    {  return (((int)c-65)+K)%26;
     }

private String dekripCipher(String cipherText) {
String hasil="";
for(int i=0;i<cipherText.length();i++)
{char D=cipherText.charAt(i);
     if((D>='A')&&(D<='Z') ) D=Kar [ dekrip( D,K) ];
    hasil+=D;
  }
  return hasil;
}


private int dekrip(char D, int K) {
    int pos=(int)D-65-K;
    if(pos<0)pos=pos+26;
        return( (pos)%26);
     }
   

}


No comments:

Post a Comment