- Back to Home »
- Enkripsi Program , Java , Java Applet »
- Membuat Program Enkripsi dengan Java Applet
Posted by : Unknown
Saturday, January 28, 2012
Ini adalah cara membuat suatu Program Enkripsi dengan Java Applet. Nah, bagi suka dengan Java, mungkin inilah hal yang perlu agan coba. Nah, langsung simak penejelasan saya....
Nanti program jadinya akan terlihat seperti ini:
Nah, cara membuat program tersebut adalah sebagai berikut:
- Buat dulu file Java Applet dan beri nama file tersebut pada pagkage agan
- Lalu buat tampilan seperti diatas
- Beri nama setiap item - item tersebut seperti berikut ini:
4. Setelah itu masukkan codingnya:
- Masukkan coding ini dibawah pagkage...
import java.io.BufferedInputStream;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.DataInputStream;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 java.io.BufferedWriter;import java.io.PrintWriter;import java.io.FileWriter;import javax.swing.JOptionPane;import javax.swing.*;import java.io.*;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.filechooser.FileFilter;
- Lalu tulis method2 yang dibutuhkan berikut:
public class ta_java extends javax.swing.JFrame {
public ta_java() {
initComponents();//Saat di awal aplikasi//Judul = Untitledthis.setTitle("Untitled - NOTES");//Atur Fillter jenis file dengan Handle fcfc = new JFileChooser();fc.addChoosableFileFilter(new jenisFile(EXT));fc.addChoosableFileFilter(new jenisFile(EXT2));//Filter Bawaan (All Files) di disable-kanfc.setAcceptAllFileFilterUsed(false);//variabel fileBaru diinisialisai dengan TRUEfileBaru = true;}JFileChooser fc;File file;boolean fileBaru;final String[] EXT = { ".csv" };final String[] EXT2 = { ".txt", ".java" };public String text,namafiletext;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) ;}class jenisFile extends FileFilter {private String[] s;jenisFile(String[] sArg) {s = sArg;}// Tentukan file apa yang akan ditampilkanpublic boolean accept(File fArg) {if (fArg.isDirectory())return true;// Jika Extension dari file sesuai syarat, tampilkanfor (int i = 0; i < s.length; ++i)if (fArg.getName().toLowerCase().indexOf(s[i].toLowerCase()) > 0)return true;// selain kedua hal di atas, anggap falsereturn false;}public String getDescription() {String tmp = "";for (int i = 0; i < s.length; ++i)tmp += "*" + s[i] + " ";return tmp;}}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(isi1.getText());pw.close();this.setTitle(file.getName()+" - NOTES" );fileBaru = false;}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);}}public static void main(String args[]) {java.awt.EventQueue.invokeLater(new Runnable() {public void run() {new ta_java().setVisible(true);}});}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;}
- Masukkan coding untuk buka file:
private void bukaActionPerformed(java.awt.event.ActionEvent evt) {JFileChooser fc= new JFileChooser();JFrame jf= new JFrame();int ret= fc.showOpenDialog(jf.add(fc));if(ret==JFileChooser.APPROVE_OPTION) {File file=fc.getSelectedFile();text=readFile(file);isi1.setText(text);namafiletext=file.getPath().toString();nama_file.setText(namafiletext);}}
- Kemudian coding untuk Simpan File:
private void simpanActionPerformed(java.awt.event.ActionEvent evt) {if (fileBaru)simpan.doClick();elseSimpanFile();}
- Coding Simpan Sebagai:
private void simpan_sbgActionPerformed(java.awt.event.ActionEvent evt) {if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){file = fc.getSelectedFile();if (!file.exists() || okToReplace())SimpanFile();}}
- Coding New File:
private void baruActionPerformed(java.awt.event.ActionEvent evt) {isi1.setText("");isi1.requestFocus();this.setTitle("Untitled - NOTES");fileBaru = true;}
- Coding Keluar:
private void keluarActionPerformed(java.awt.event.ActionEvent evt) {System.exit(0);}
- Coding Ubah:
private void ubahActionPerformed(java.awt.event.ActionEvent evt) {isi1.setEnabled(true);}
- Coding MD5_1:
private void md5_1ActionPerformed(java.awt.event.ActionEvent evt) {save2.setEnabled(true);password2.setEnabled(true);pass2.setEnabled(true);isi2.setEnabled(true);saveas2.setEnabled(true);md5_2.setEnabled(true);random2.setEnabled(true);Enskripsimd5 md5 = new Enskripsimd5();try {isi2.setText(md5.MD5(isi1.getText()));} catch (NoSuchAlgorithmException ex) {Logger.getLogger(ta_java.class.getName()).log(Level.SEVERE, null, ex);} catch (UnsupportedEncodingException ex) {Logger.getLogger(ta_java.class.getName()).log(Level.SEVERE, null);}}
- Coding Random1:
private void random1ActionPerformed(java.awt.event.ActionEvent evt) {save2.setEnabled(true);password1.setEnabled(true);pass2.setEnabled(true);isi2.setEnabled(true);saveas2.setEnabled(true);md5_2.setEnabled(true);random2.setEnabled(true);String randomKey = "";double randomNumber;for (int i = 0; i < isi1.getText().length(); i++) {randomNumber = Math.random() * 25 + 65;randomKey = randomKey + (char) randomNumber;}isi2.setText(randomKey);}
- Coding Save2:
private void save2ActionPerformed(java.awt.event.ActionEvent evt) {if (fileBaru) {saveas2.doClick();} else {SimpanFile();}}
- Coding Saveas2:
private void saveas2ActionPerformed(java.awt.event.ActionEvent evt) {if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {file = fc.getSelectedFile();if (!file.exists() || okToReplace()) {SimpanFile();}}}
- Coding Random2:
private void random2ActionPerformed(java.awt.event.ActionEvent evt) {String randomKey = "";double randomNumber;for (int i = 0; i < isi2.getText().length(); i++) {randomNumber = Math.random() * 25 + 65;randomKey = randomKey + (char) randomNumber;}isi1.setText(randomKey);}
- Coding Password1:
private void password1ActionPerformed(java.awt.event.ActionEvent evt) {if (pass2.getText().equals(pass1.getText())) {msg("password anda benar : " + pass1.getText());isi1.setText(isi2.getText());} else {msg("password anda salah : " + pass1.getText());}}
- Coding Password2:
private void password2ActionPerformed(java.awt.event.ActionEvent evt) {if (pass1.getText().equals(pass2.getText())) {msg("password anda benar : " + pass2.getText());isi2.setText(isi1.getText());} else {msg("password anda salah : " + pass2.getText());}}
Setelah itu coba di RUN. Jika ada coding yang masih error, coba cari sendiri dulu atau hubungi & add Facebook ane : Wahyu Nardianto . Kalau mau project ane, hubungi juga facebook ane nanti ane kirim email project program ane yang sudah jadi ini. Terima Kasih