Sabtu, 04 Februari 2012

Coding untuk memulai membuat enskripsi java applet

Buat file java applet di Neatbeans dan tentukan nama file dan package:
2.       Jika sudah tulis coding di bawah ini di bawah package:

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.DataInputStream;
import javax.swing.JOptionPane;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.*;
import java.io.*;
import javax.swing.filechooser.FileFilter;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

3.       Jika sudah tulis method dan coding di dalam source java applet:
Cari coding public class dan tulis codingnya
public class enskripsi1 extends javax.swing.JApplet {

    public String text, namafiletext;
    JFileChooser fc;
    File file;
    boolean fileBaru;
    boolean fileBaru1;
    final String[] EXT = {(".csv")};
    final String[] EXT2 = {(".txt")};
cari coding “public void init() {“ dan tulis coding di bawah ini:

fc = new JFileChooser();
        fc.addChoosableFileFilter(new jenisFile(EXT));
        fc.addChoosableFileFilter(new jenisFile(EXT2));

        fc.setAcceptAllFileFilterUsed(false);

        fileBaru = true;
        fileBaru1 = true;

buat method di bawah ini:

private void msg(String pesan) {

        JOptionPane.showMessageDialog(this, pesan, "Informasi",
                JOptionPane.INFORMATION_MESSAGE);
    }

    private boolean okToReplace() {

        return (JOptionPane.showConfirmDialog(this, "File " + file.getName() + " Sudah Ada\n"
                + "Akan Ditimpa ?",
                "Peringatan", JOptionPane.YES_NO_OPTION) == 0);
    }

    private void SimpanFile() {
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
        } catch (IOException e) {
            msg("Gagal Simpan File : '" + file.getName());
            return;
        }
        pw.print(isi.getText());
        pw.close();
        fileBaru = false;
    }

    private void SimpanFile1() {
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
        } catch (IOException e) {
            msg("Gagal Simpan File : '" + file.getName());
            return;
        }
        pw.print(isi2.getText());
        pw.close();
        fileBaru1 = false;
    }

    private void setTitle(String string) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    class Enskripsimd5 {

        private String convertToHex(byte[] data) {
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < data.length; i++) {
                int halfbyte = (data[i] >>> 4) & 0x0F;
                int two_halfs = 0;
                do {
                    if ((0 <= halfbyte) && (halfbyte <= 9)) {
                        buf.append((char) ('0' + halfbyte));
                    } else {
                        buf.append((char) ('a' + (halfbyte - (int) geser.getValue())));
                    }
                    halfbyte = data[i] & 0x0F;
                } while (two_halfs++ < 1);
            }
            return buf.toString();
        }

        public String MD5(String text)
                throws NoSuchAlgorithmException, UnsupportedEncodingException {
            MessageDigest md;
            md = MessageDigest.getInstance("MD5");
            byte[] md5hash = new byte[32];
            md.update(text.getBytes("iso-8859-1"), 0, text.length());
            md5hash = md.digest();
            return convertToHex(md5hash);

        }
    }

4.       Klik dua kali pada button browse dan tulis coding:

private void browse2ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
       
        JFileChooser fc = new JFileChooser();
        JFrame jf = new JFrame();
        int hasil = fc.showOpenDialog(this);
        fileBaru = false;
        if (hasil == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
try {
isi.read(new FileInputStream(file), null);
} catch (IOException e) {
msg("Gagal Buka File : "+file.getName() );
return ;
}

           
            save.setEnabled(true);
            btnSaveAs.setEnabled(true);
            geser.setEnabled(true);
            namafile1.setEnabled(true);
            isi.setEnabled(true);
            geser.setEnabled(true);
            ubah.setEnabled(true);
            Cpass.setEnabled(true);
            random.setEnabled(true);
            password2.setEnabled(true);

            File file = fc.getSelectedFile();
            text = readFile(file);
            isi.setText(text);
            namafiletext = file.getPath().toString();
            namafile1.setText(namafiletext);
        
        }
        fileBaru=false;
       
    }                                      

5.       Klik dua kali pada button save pada pada panel atas:

private void saveActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        if (fileBaru) {
            btnSaveAs.doClick();
        } else {
            SimpanFile();
    }                                    
    }

6.       Klik dua kali pada button save as panel atas:

private void btnSaveAsActionPerformed(java.awt.event.ActionEvent evt) {                                         
            // TODO add your handling code here:
            if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
                file = fc.getSelectedFile();
                if (!file.exists() || okToReplace()) {
                    SimpanFile();
                }
            }
        }  

7.       Klik dua kali pada button new panel atas dan tulis codingnya:
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        save.setEnabled(true);
        btnSaveAs.setEnabled(true);
        geser.setEnabled(true);
        namafile1.setEnabled(true);
        isi.setEnabled(true);
        geser.setEnabled(true);
        ubah.setEnabled(true);
        Cpass.setEnabled(true);
        random.setEnabled(true);
        password2.setEnabled(true);

        isi.setText("");
        isi2.setText("");
        namafile1.setText("");
        isi.requestFocus();
        fileBaru = true;
    }         

8.      + Klik dua kali pada button keluar:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        System.exit(0);
    }

9.       Klik dua kali pada button MD5 panel kiri:

save2.setEnabled(true);
            password.setEnabled(true);
            pass.setEnabled(true);
            isi2.setEnabled(true);
            btnSaveAs1.setEnabled(true);
            md52.setEnabled(true);
            random2.setEnabled(true);

            Enskripsimd5 md5 = new Enskripsimd5();
            try {
                isi2.setText(md5.MD5(isi.getText()));

            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(enskripsi1.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedEncodingException ex) {
      +          Logger.getLogger(enskripsi1.class.getName()).log(Level.SEVERE, null);
            }

        }                                   

10.   Klik dua kali pada button random panel kiri:
private void randomActionPerformed(java.awt.event.ActionEvent evt) {                                      
        // TODO add your handling code here:
        save2.setEnabled(true);
            password.setEnabled(true);
            pass.setEnabled(true);
            isi2.setEnabled(true);
            btnSaveAs1.setEnabled(true);
            md52.setEnabled(true);
            random2.setEnabled(true);
        String randomKey = "";
        double randomNumber;
        for (int i = 0; i < isi.getText().length(); i++) {
            randomNumber = Math.random() * 25 + 65;
            randomKey = randomKey + (char) randomNumber;
        }

        isi2.setText(randomKey);
    }                                     

11.   Klik dua kali pada button password panel kiri:
private void password2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        if (pass.getText().equals(Cpass.getText())) {
            msg("password anda benar : " + Cpass.getText());
            isi.setText(isi2.getText());
        } else {
            msg("password anda salah : " + Cpass.getText());
        }
    } 

12.   Klik dua kali pada button save pada panel kanan:
private void save2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
        if (fileBaru1) {
            btnSaveAs1.doClick();
        } else {
            SimpanFile1();
        }
    }    

13.   Klik dua kali pada button save as panel kanan:
private void btnSaveAs1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
            if (!file.exists() || okToReplace()) {
                SimpanFile1();
            }
        }
    }   

14.   Klik dua kali pada button MD5 panel kanan:
private void md52ActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        Enskripsimd5 md5 = new Enskripsimd5();
            try {
                isi.setText(md5.MD5(isi2.getText()));

            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(enskripsi1.class.getName()).log(Level.SEVERE, null, ex);
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(enskripsi1.class.getName()).log(Level.SEVERE, null);
            }
    }   

15.   Klik dua kali pada button random panel kanan:
private void random2ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        String randomKey = "";
        double randomNumber;
        for (int i = 0; i < isi2.getText().length(); i++) {
            randomNumber = Math.random() * 25 + 65;
            randomKey = randomKey + (char) randomNumber;
        }

        isi.setText(randomKey);
    }      

16.   Klik dua kali pada button password panel kanan:
private void passwordActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        if (Cpass.getText().equals(pass.getText())) {
            msg("password anda benar : " + pass.getText());
            isi2.setText(isi.getText());
        } else {
            msg("password anda salah : " + pass.getText());
        }
    } 

17.   Tulis coding ini di bawah:
private String readFile(File file) {
        String result = "";
        try {
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            DataInputStream dis = new DataInputStream(bis);
            while (dis.available() != 0) {
                result += dis.readLine() + "\n";
            }
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
        return result;
    }

buat class dengan new class dan tulis codingnya:

package enskripsi;
import java.io.File;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileFilter;

class jenisFile extends FileFilter {

   private String[] s;
jenisFile(String[] sArg) {
s = sArg;
}
// Tentukan file apa yang akan ditampilkan
public boolean accept(File fArg) {
if (fArg.isDirectory())
return true;
for (int i = 0; i < s.length; ++i)
if (fArg.getName().toLowerCase().indexOf(s[i].toLowerCase()) > 0)
return true;
// selain kedua hal di atas, anggap false
return false;
}
public String getDescription() {
String tmp = "";
for (int i = 0; i < s.length; ++i)
tmp += "*" + s[i] + " ";
return tmp;
}
 
}

Tidak ada komentar:

Posting Komentar