Gambar diatas adalah program enkripsi dengan Java Applet
Untuk Codingnya kita lihat dibawah ini:
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;
public class ta_java extends javax.swing.JFrame {
public ta_java() {
initComponents();
this.setTitle("Untitled - NOTES");
fc = new JFileChooser();
fc.addChoosableFileFilter(new jenisFile(EXT));
fc.addChoosableFileFilter(new jenisFile(EXT2));
fc.setAcceptAllFileFilterUsed(false);
fileBaru = 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;
}
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;
return 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;
}
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);
}
}
private void simpanActionPerformed(java.awt.event.ActionEvent evt) {
if (fileBaru)
simpan.doClick();
else
SimpanFile();
}
private void simpan_sbgActionPerformed(java.awt.event.ActionEvent evt) {
if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
file = fc.getSelectedFile();
if (!file.exists() || okToReplace())
SimpanFile();
}
}
private void baruActionPerformed(java.awt.event.ActionEvent evt) {
isi1.setText("");
isi1.requestFocus();
this.setTitle("Untitled - NOTES");
fileBaru = true;
}
private void keluarActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void ubahActionPerformed(java.awt.event.ActionEvent evt) {
isi1.setEnabled(true);
}
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);
}
}
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);
}
private void save2ActionPerformed(java.awt.event.ActionEvent evt) {
if (fileBaru) {
saveas2.doClick();
} else {
SimpanFile();
}
}
private void saveas2ActionPerformed(java.awt.event.ActionEvent evt) {
if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
if (!file.exists() || okToReplace()) {
SimpanFile();
}
}
}
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);
}
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());
}
}
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());
}
}
ini di repost dari blog teman saya, rista
Untuk Codingnya kita lihat dibawah ini:
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;
public class ta_java extends javax.swing.JFrame {
public ta_java() {
initComponents();
this.setTitle("Untitled - NOTES");
fc = new JFileChooser();
fc.addChoosableFileFilter(new jenisFile(EXT));
fc.addChoosableFileFilter(new jenisFile(EXT2));
fc.setAcceptAllFileFilterUsed(false);
fileBaru = 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;
}
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;
return 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;
}
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);
}
}
private void simpanActionPerformed(java.awt.event.ActionEvent evt) {
if (fileBaru)
simpan.doClick();
else
SimpanFile();
}
private void simpan_sbgActionPerformed(java.awt.event.ActionEvent evt) {
if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
file = fc.getSelectedFile();
if (!file.exists() || okToReplace())
SimpanFile();
}
}
private void baruActionPerformed(java.awt.event.ActionEvent evt) {
isi1.setText("");
isi1.requestFocus();
this.setTitle("Untitled - NOTES");
fileBaru = true;
}
private void keluarActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void ubahActionPerformed(java.awt.event.ActionEvent evt) {
isi1.setEnabled(true);
}
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);
}
}
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);
}
private void save2ActionPerformed(java.awt.event.ActionEvent evt) {
if (fileBaru) {
saveas2.doClick();
} else {
SimpanFile();
}
}
private void saveas2ActionPerformed(java.awt.event.ActionEvent evt) {
if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
if (!file.exists() || okToReplace()) {
SimpanFile();
}
}
}
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);
}
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());
}
}
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());
}
}
ini di repost dari blog teman saya, rista
minta source codenya ya? makasih...
BalasHapus