Rabu, 04 April 2012

Java Enkripsi

Buat file baru (file - new project - java application). Kemudian klik kanan package - pilih jframe form. Pertama, kita perlu membuat designnya di jframe form. Anda bisa mendesign sesuai selera. Misalnya designnya seperti ini : 
 Jika ingin memberi nama variabel, klik kanan button - change variabel.


codingnya : 

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * enskripsi1.java
 *
 * Created on 19 Jan 12, 12:02:55
 */
package enskripsi;

import javax.swing.*;
import java.io.*;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author soyadina
 */
public class enskripsi1 extends javax.swing.JApplet {

    public String text, namafiletext;
    JFileChooser fc;
    File file;
    boolean fileBaru;
    boolean fileBaru1, buf;
    final String[] EXT = {(".csv")};
    final String[] EXT2 = {(".txt")};

    /** Initializes the applet enskripsi1 */
    @Override
    public void init() {
        fc = new JFileChooser();
        fc.addChoosableFileFilter(new jenisFile(EXT));
        fc.addChoosableFileFilter(new jenisFile(EXT2));

        fc.setAcceptAllFileFilterUsed(false);

        fileBaru = true;
        fileBaru1 = true;
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
        }
    }

    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())));
                        Object a = geser.getValue();
                        buf.append('a' + (halfbyte - Integer.parseInt(a.toString())));
                    }
                    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);

        }
    }

    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        browse2 = new javax.swing.JButton();
        save = new javax.swing.JButton();
        btnSaveAs = new javax.swing.JButton();
        geser = new javax.swing.JSpinner();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        namafile1 = new javax.swing.JTextField();
        ubah = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        isi = new javax.swing.JTextPane();
        Cpass = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        random = new javax.swing.JButton();
        password2 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        save2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        isi2 = new javax.swing.JTextPane();
        pass = new javax.swing.JTextField();
        password = new javax.swing.JButton();
        btnSaveAs1 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        md52 = new javax.swing.JButton();
        random2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem8 = new javax.swing.JMenuItem();
        save3 = new javax.swing.JMenuItem();
        saveas2 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenuItem7 = new javax.swing.JMenuItem();

        jPanel1.setBackground(new java.awt.Color(0, 153, 153));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jPanel2.setBackground(new java.awt.Color(0, 102, 102));
        jPanel2.setForeground(new java.awt.Color(204, 255, 255));
        jPanel2.setFont(new java.awt.Font("Comic Sans MS", 1, 11));

        browse2.setFont(new java.awt.Font("Arial Black", 0, 11));
        browse2.setText("browse");
        browse2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browse2ActionPerformed(evt);
            }
        });

        save.setFont(new java.awt.Font("Arial Black", 0, 11)); // NOI18N
        save.setText("save");
        save.setEnabled(false);
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        btnSaveAs.setFont(new java.awt.Font("Arial Black", 0, 11)); // NOI18N
        btnSaveAs.setText("save as");
        btnSaveAs.setEnabled(false);
        btnSaveAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveAsActionPerformed(evt);
            }
        });

        geser.setEnabled(false);

        jButton4.setFont(new java.awt.Font("Arial Black", 0, 11)); // NOI18N
        jButton4.setText("new");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial Black", 0, 11)); // NOI18N
        jLabel1.setText("geser");

        jButton1.setText("keluar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(save)
                        .addGap(18, 18, 18)
                        .addComponent(btnSaveAs))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(browse2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton4)
                        .addGap(39, 39, 39)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(geser, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(37, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(browse2)
                    .addComponent(geser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jButton4))
                .addGap(30, 30, 30)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(save)
                    .addComponent(btnSaveAs)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(56, Short.MAX_VALUE))
        );

        jPanel3.setBackground(new java.awt.Color(0, 102, 102));
        jPanel3.setFont(new java.awt.Font("Comic Sans MS", 1, 11));

        namafile1.setEnabled(false);

        ubah.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        ubah.setText("MD5");
        ubah.setEnabled(false);
        ubah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ubahActionPerformed(evt);
            }
        });

        isi.setEnabled(false);
        jScrollPane1.setViewportView(isi);

        Cpass.setFont(new java.awt.Font("Arial", 0, 11));
        Cpass.setForeground(new java.awt.Color(0, 51, 255));
        Cpass.setEnabled(false);

        jLabel2.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        jLabel2.setText("browse");

        random.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        random.setText("random");
        random.setEnabled(false);
        random.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                randomActionPerformed(evt);
            }
        });

        password2.setFont(new java.awt.Font("Arial", 0, 11));
        password2.setText("password");
        password2.setEnabled(false);
        password2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                password2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addComponent(password2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                        .addComponent(Cpass, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(48, 48, 48)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                .addComponent(ubah)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                                .addComponent(random))
                            .addComponent(namafile1, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(namafile1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(random)
                    .addComponent(ubah))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Cpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(password2))
                .addContainerGap(41, Short.MAX_VALUE))
        );

        jPanel4.setBackground(new java.awt.Color(0, 102, 102));
        jPanel4.setFont(new java.awt.Font("Comic Sans MS", 1, 11));

        save2.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        save2.setText("save");
        save2.setEnabled(false);
        save2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                save2ActionPerformed(evt);
            }
        });

        isi2.setEnabled(false);
        jScrollPane2.setViewportView(isi2);

        pass.setForeground(new java.awt.Color(0, 102, 255));
        pass.setEnabled(false);

        password.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        password.setText("password");
        password.setEnabled(false);
        password.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                passwordActionPerformed(evt);
            }
        });

        btnSaveAs1.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        btnSaveAs1.setText("save as");
        btnSaveAs1.setEnabled(false);
        btnSaveAs1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveAs1ActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        jLabel4.setText("isi enskripsi");

        md52.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        md52.setText("MD5");
        md52.setEnabled(false);
        md52.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                md52ActionPerformed(evt);
            }
        });

        random2.setFont(new java.awt.Font("Arial", 0, 11)); // NOI18N
        random2.setText("random");
        random2.setEnabled(false);
        random2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                random2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(password)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 67, Short.MAX_VALUE)
                        .addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addGap(46, 46, 46)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(save2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(md52, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(27, 27, 27)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnSaveAs1)
                            .addComponent(random2)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 277, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSaveAs1)
                    .addComponent(save2)
                    .addComponent(jLabel4))
                .addGap(11, 11, 11)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(random2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(md52, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(25, 25, 25)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(password)
                    .addComponent(pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(45, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(156, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(149, 149, 149))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(26, 26, 26)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jMenu1.setText("File");

        jMenuItem3.setText("new");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem3);

        jMenuItem8.setText("open");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem8);

        save3.setText("save ");
        save3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                save3ActionPerformed(evt);
            }
        });
        jMenu1.add(save3);

        saveas2.setText("save as ");
        saveas2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveas2ActionPerformed(evt);
            }
        });
        jMenu1.add(saveas2);

        jMenuItem1.setText("save enskripsi");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("save as enskripsi");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Enskripsi");

        jMenuItem4.setText("MD5");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem4);

        jMenuItem5.setText("random");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem5);

        jMenuItem6.setText("MD5 enskripsi");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem6);

        jMenuItem7.setText("random enskripsi");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem7);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(246, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(42, Short.MAX_VALUE))
        );
    }// </editor-fold>

    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;

    }                                      

    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;
    }                                       

    private void saveActionPerformed(java.awt.event.ActionEvent evt) {                                    
        // TODO add your handling code here:
        if (fileBaru) {
            btnSaveAs.doClick();
        } else {
            SimpanFile();
    }                                   
    }
        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();
                }
            }
        }                                        

        private void ubahActionPerformed(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);

            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);
            }

        }                                   

    private void save2ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
        if (fileBaru1) {
            btnSaveAs1.doClick();
        } else {
            SimpanFile1();
        }
    }                                    

    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());
        }
    }                                       

    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();
            }
        }
    }                                         

    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);
    }                                     

    private void save3ActionPerformed(java.awt.event.ActionEvent evt) {                                     
        // TODO add your handling code here:
        if (fileBaru) {
            btnSaveAs.doClick();
        } else {
            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 saveas2ActionPerformed(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();
            }
        }
    }                                      

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        if (fileBaru) {
            btnSaveAs1.doClick();
        } else {
            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();
            fileBaru = false;
        }
    }                                         

    private void jMenuItem2ActionPerformed(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();
            }
        }
    }                                         

    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());
        }
    }                                        

    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);
        }
    }                                   

    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);
    }                                      

    private void jMenuItem3ActionPerformed(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;
    }                                         

    private void jMenuItem4ActionPerformed(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);

        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);
        }
    }                                         

    private void jMenuItem5ActionPerformed(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);
    }                                         

    private void jMenuItem6ActionPerformed(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);
        }
    }                                         

    private void jMenuItem7ActionPerformed(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);
    }                                         

    private void jMenuItem8ActionPerformed(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;

    }                                         

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
        System.exit(0);
    }                                       
    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;
    }
    // Variables declaration - do not modify
    private javax.swing.JTextField Cpass;
    private javax.swing.JButton browse2;
    private javax.swing.JButton btnSaveAs;
    private javax.swing.JButton btnSaveAs1;
    private javax.swing.JSpinner geser;
    private javax.swing.JTextPane isi;
    private javax.swing.JTextPane isi2;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton md52;
    private javax.swing.JTextField namafile1;
    private javax.swing.JTextField pass;
    private javax.swing.JButton password;
    private javax.swing.JButton password2;
    private javax.swing.JButton random;
    private javax.swing.JButton random2;
    private javax.swing.JButton save;
    private javax.swing.JButton save2;
    private javax.swing.JMenuItem save3;
    private javax.swing.JMenuItem saveas2;
    private javax.swing.JButton ubah;
    // End of variables declaration
}



selanjutnya, buat class baru, misalnya bernama jenisFile :



Codingya :

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package enskripsi;

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

/**
 *
 * @author soyadina
 */
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;
    }
}


Selamat Mencoba !!

 




0 komentar:

Posting Komentar