* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cryptografi;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.commons.net.ftp.FTPClient;
import rsa.EncryptionFileVC;
public class EncryptVC extends javax.swing.JFrame {
Dimension UM = Toolkit.getDefaultToolkit().getScreenSize();
String server = "192.168.1.2";
String user = "me";
String pass = "12345";
public EncryptVC() {
initComponents();
this.setTitle("FORM ENCRYPT");
setLocation(UM.width/2-getWidth()/2,UM.height/2-getHeight()/2);
show();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/**
* This method is called from within the constructor 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() {
jLabel3 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
txt_chose = new javax.swing.JTextField();
txt_output = new javax.swing.JTextField();
txtPass = new javax.swing.JTextField();
btn_browse1 = new javax.swing.JButton();
btn_browse2 = new javax.swing.JButton();
btn_encrypt = new javax.swing.JButton();
btnSend = new javax.swing.JButton();
btn_exit = new javax.swing.JButton();
progressbar = new javax.swing.JProgressBar();
jLabel6 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(null);
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel3.setText("Chose File");
getContentPane().add(jLabel3);
jLabel3.setBounds(20, 110, 110, 30);
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jLabel2.setText("FORM ENCRYPTION");
getContentPane().add(jLabel2);
jLabel2.setBounds(210, 30, 180, 30);
jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel4.setText("Output File");
getContentPane().add(jLabel4);
jLabel4.setBounds(20, 160, 100, 30);
jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel5.setText("Password");
getContentPane().add(jLabel5);
jLabel5.setBounds(20, 210, 130, 40);
txt_chose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txt_choseActionPerformed(evt);
}
});
getContentPane().add(txt_chose);
txt_chose.setBounds(150, 110, 350, 40);
txt_output.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txt_outputActionPerformed(evt);
}
});
getContentPane().add(txt_output);
txt_output.setBounds(150, 160, 350, 40);
txtPass.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtPassActionPerformed(evt);
}
});
getContentPane().add(txtPass);
txtPass.setBounds(150, 210, 350, 40);
btn_browse1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btn_browse1.setText("Open");
btn_browse1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_browse1ActionPerformed(evt);
}
});
getContentPane().add(btn_browse1);
btn_browse1.setBounds(520, 110, 110, 30);
btn_browse2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btn_browse2.setText("Save");
btn_browse2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_browse2ActionPerformed(evt);
}
});
getContentPane().add(btn_browse2);
btn_browse2.setBounds(520, 160, 110, 30);
btn_encrypt.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btn_encrypt.setText("Encrypt");
btn_encrypt.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_encryptActionPerformed(evt);
}
});
getContentPane().add(btn_encrypt);
btn_encrypt.setBounds(140, 320, 120, 40);
btnSend.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btnSend.setText("Send");
btnSend.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnSendActionPerformed(evt);
}
});
getContentPane().add(btnSend);
btnSend.setBounds(280, 320, 120, 40);
btn_exit.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
btn_exit.setText("Exit");
btn_exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btn_exitActionPerformed(evt);
}
});
getContentPane().add(btn_exit);
btn_exit.setBounds(420, 320, 110, 40);
getContentPane().add(progressbar);
progressbar.setBounds(150, 270, 460, 20);
getContentPane().add(jLabel6);
jLabel6.setBounds(10, 10, 90, 90);
getContentPane().add(jLabel1);
jLabel1.setBounds(0, 0, 640, 390);
pack();
}// </editor-fold>
private void txt_choseActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txtPassActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void txt_outputActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void btn_browse1ActionPerformed(java.awt.event.ActionEvent evt) {
String path=System.getProperty("user.dir");
JFileChooser datafile = new JFileChooser(path);
FileNameExtensionFilter filter = new FileNameExtensionFilter("File Document", "txt","doc","docx","xls","xlsx","ppt","pptx","pdf");
FileNameExtensionFilter image = new FileNameExtensionFilter("File Images", "jpg","png","bmp");
FileNameExtensionFilter audio = new FileNameExtensionFilter("File Audio", "wav","amr", "mp3");
FileNameExtensionFilter files = new FileNameExtensionFilter("File", "zip","rar");
datafile.setFileFilter(image);
datafile.setFileFilter(files);
datafile.setFileFilter(audio);
datafile.setFileFilter(filter);
datafile.setFileFilter(filter);
int returnVal = datafile.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
File file = datafile.getSelectedFile();
String dir = file.getAbsolutePath();
//System.out.println(dir);
txt_chose.setText(dir);
}
}
private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {
Thread t = new Thread(new Runnable(){
public void run(){
progressbar.setIndeterminate(true);
long start;
long end;
EncryptionFileVC enk = new EncryptionFileVC();
String filename = (txt_chose.getText());//file dipilih
String fileoutput = (txt_output.getText());//directory-folder mai simpan di?
int dot = filename.lastIndexOf(".");
int sep = filename.replace("\\", "/").lastIndexOf("/");
enk.setFile(fileoutput.replace("\\", "/"), filename.substring(sep + 1, dot), filename.substring(dot + 1) );
String nf=enk.getFile();
System.out.println("nf="+nf);
if (!(nf.equals("") && nf.length()>3)){
try {
start = System.currentTimeMillis();
progressbar.setIndeterminate(false);
kirimFTP(nf);
end = System.currentTimeMillis();
JOptionPane.showMessageDialog(null, "Send FTP Sukses in : " + ((end - start) / 1000.0) + " Milidetik", "Message", JOptionPane.INFORMATION_MESSAGE);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Send FTP Fail", "Warning", JOptionPane.ERROR_MESSAGE);
}
}else{
JOptionPane.showMessageDialog(null, "Silakan Enkrip dahulu", "Warning", JOptionPane.ERROR_MESSAGE);
}
}
});
t.start();
}
void kirimFTP(String NF){
int port = 21;
FTPClient ftpClient = new FTPClient();
try {
// connect and login to the server
ftpClient.connect(server, port);
ftpClient.login(user, pass);
// use local passive mode to pass firewall
ftpClient.enterLocalPassiveMode();
System.out.println("Connected");
String remoteDirPath = getNamaFile(NF);
String localDirPath = NF;
// FTPUtil.uploadDirectory(ftpClient, remoteDirPath, localDirPath, "");
FTPUtil.uploadSingleFile(ftpClient, localDirPath,remoteDirPath);
// log out and disconnect from the server
ftpClient.logout();
ftpClient.disconnect();
System.out.println("Disconnected");
} catch (IOException ex) {
ex.printStackTrace();
}
}
String getNamaFile(String fileName){
int dot = fileName.lastIndexOf(".");
int sep = fileName.replace("\\", "/").lastIndexOf("/");
String NF=fileName.substring(sep + 1, dot);
String extendeed= fileName.substring(dot + 1);
NF=NF+"."+extendeed;
return NF;
}
//
private void btn_exitActionPerformed(java.awt.event.ActionEvent evt) {
new Menu_Utama().setVisible(true);
this.dispose();
}
private void btn_browse2ActionPerformed(java.awt.event.ActionEvent evt) {
String path=System.getProperty("user.dir");
JFileChooser savedata = new JFileChooser(path);
savedata.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = savedata.showSaveDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
txt_output.setText(savedata.getSelectedFile().toString());
}
}
private void btn_encryptActionPerformed(java.awt.event.ActionEvent evt) {
Thread t = new Thread(new Runnable(){
public void run(){
progressbar.setIndeterminate(true);
long start;
long end;
String Password = (txtPass.getText().toString());
String filename = (txt_chose.getText());//file dipilih
String fileoutput = (txt_output.getText());//directory-folder mai simpan di?
int dot = filename.lastIndexOf(".");
int sep = filename.replace("\\", "/").lastIndexOf("/");
System.out.println("dot="+dot);//22
System.out.println("sep="+sep);//6
EncryptionFileVC enk = new EncryptionFileVC();
if (!(Password.equals("") || filename.equals(""))){
try {
//enk.setPublicKey(Kunci);//membaca isi file Public key, set= n dan e
//String directory, String filename, String extensi=D:/Lat/en.Sistem Berjalan.doc
enk.setFile(fileoutput.replace("\\", "/"), filename.substring(sep + 1, dot), filename.substring(dot + 1) );
System.out.println("file1="+fileoutput.replace("\\", "/"));//D:/Lat
System.out.println("file2="+filename.substring(sep + 1, dot));//Sistem Berjalan
System.out.println("file3="+filename.substring(dot + 1));//doc
System.out.println("file4="+filename.replace("\\", "/"));
start = System.currentTimeMillis();//menghitung waktu proses dalam detik
enk.encrypt(filename.replace("\\", "/"),Password);//D:/Lat/Sistem Berjalan.doc
progressbar.setIndeterminate(false);
end = System.currentTimeMillis();
//System.out.println("\nWaktu yang diperlukan selama proses adalah " + ((end - start) / 1000.0) + " detik");
JOptionPane.showMessageDialog(null, "Encryption VC Success with duration : " + ((end - start) / 1000.0) + " Milidetik", "Message", JOptionPane.INFORMATION_MESSAGE);
// Bersih();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, "Failed Encryption", "Warning", JOptionPane.ERROR_MESSAGE);
}
}else{
JOptionPane.showMessageDialog(null, "Please entry all coloumn", "Warning", JOptionPane.ERROR_MESSAGE);
}
}
});
t.start();
}
void Bersih(){
txt_chose.setText("");
txt_output.setText("");
txtPass.setText("");
txt_chose.setEnabled(true);
txt_output.setEnabled(true);
txtPass.setEnabled(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new EncryptVC().setVisible(true);
}
});
}
//
// void kirim(String NF){
// FTPSClient client = new FTPSClient("TLS",true);
// try
// {
// System.out.println("Connecting...");
// client.connect("ftp://192.168.1.2",21);//10443
// System.out.println("Connected");
// if(client.login("me", "12345"))
// {
// System.out.println(">> daftar file & folder");
// FTPFile[] files = client.listFiles();
// for(int i=0;i<files.length;i++)
// {
// System.out.println(files[i].toFormattedString());
// }
// client.logout();
// System.out.println("Logout");
// }
//
//
// } catch (SocketException ex) {
// System.out.println(ex.getMessage());
// } catch (IOException ex) {
// System.out.println(ex.getMessage());
// }
// finally
// {
// try {
// client.disconnect();
// } catch (IOException ex) {
// Logger.getLogger(EncryptVC.class.getName()).log(Level.SEVERE, null, ex);
// }
// System.out.println("Disconnected");
// }
// }
//
//
// void send(String NF){
//FTPClient client = new FTPClient();
//FileInputStream fis = null;
//
//try {
// client.connect("ftp://192.168.1.2");
// client.login("me", "12345");
// String filename = NF;
// fis = new FileInputStream(filename);
// client.storeFile(filename, fis);
// client.logout();
//} catch (IOException e) {
// e.printStackTrace();
//} finally {
// try {
// if (fis != null) {
// fis.close();
// }
// client.disconnect();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//}
//
// void sendFTP(String NF){
// String server = "ftp://192.168.1.2";
// int port = 21;
// String user = "me";
// String pass = "12345";
//
// FTPClient ftpClient = new FTPClient();
// try {
//
// ftpClient.connect(server, port);
// ftpClient.login(user, pass);
// ftpClient.enterLocalPassiveMode();
//
// // ftpClient.setFileType(FTP);
// //String path=System.getProperty("user.dir");
// // APPROACH #1: uploads first file using an InputStream
// File firstLocalFile = new File(NF);
//
// String firstRemoteFile = NF;
// InputStream inputStream = new FileInputStream(firstLocalFile);
//
// System.out.println("Start uploading first file");
// boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
// inputStream.close();
// if (done) {
// System.out.println("The first file is uploaded successfully.");
// }
//
// // APPROACH #2: uploads second file using an OutputStream
// File secondLocalFile = new File("E:/Test/Report.doc");
// String secondRemoteFile = "test/Report.doc";
// inputStream = new FileInputStream(secondLocalFile);
//
// System.out.println("Start uploading second file");
// OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
// byte[] bytesIn = new byte[4096];
// int read = 0;
//
// while ((read = inputStream.read(bytesIn)) != -1) {
// outputStream.write(bytesIn, 0, read);
// }
// inputStream.close();
// outputStream.close();
//
// boolean completed = ftpClient.completePendingCommand();
// if (completed) {
// System.out.println("The second file is uploaded successfully.");
// }
//
// } catch (IOException ex) {
// System.out.println("Error: " + ex.getMessage());
// ex.printStackTrace();
// } finally {
// if (ftpClient.isConnected()) {
// try {
// ftpClient.logout();
// } catch (IOException ex) {
// Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
// }
// try {
// ftpClient.disconnect();
// } catch (IOException ex) {
// Logger.getLogger(Encrypt.class.getName()).log(Level.SEVERE, null, ex);
// }
// }
// }
// }
// Variables declaration - do not modify
private javax.swing.JButton btnSend;
private javax.swing.JButton btn_browse1;
private javax.swing.JButton btn_browse2;
private javax.swing.JButton btn_encrypt;
private javax.swing.JButton btn_exit;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JProgressBar progressbar;
private javax.swing.JTextField txtPass;
private javax.swing.JTextField txt_chose;
private javax.swing.JTextField txt_output;
// End of variables declaration
}
package rsa;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import misc.Filename;
/**
*
* @author DINAA
*/
public class EncryptionFileVC {
private String filename;
private String directory;
private String publicFile;
private BigInteger n;
private Integer e;
private String fileNameEncrypt;
private String extension;
/**
* untuk seting file yang akan di enkripsi.
* @param fileName
*/
public void setFile(String directory, String filename, String extens) {
this.fileNameEncrypt = directory + "/" + "en." + filename+ "." + extens;
System.out.println("fileNameEncrypt="+fileNameEncrypt);//D:/Lat/en.Sistem Berjalan.doc
}
public String getFile() {
return this.fileNameEncrypt;
}
String doc2String(String fileName) throws IOException{
String gab="";
Path path = Paths.get(fileName);
byte[] data = Files.readAllBytes(path);
for(int i=0; i < data.length; i++) {
char c=(char)(int)data[i];
gab=gab+c;
}
return gab;
}
public void encrypt(String fileName,String pass) throws IOException {
String isi="";
try {
isi = doc2String(fileName);
} catch (IOException ex) {
Logger.getLogger(DecryptionFileVC.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("ISI="+isi);
System.out.println("ISI="+isi);
String l="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#&%*()-_=:,.'?/$^ ";
String p=isi;
String k=pass;
int pms=l.length();
int pln=p.length();
int ky=k.length();
String y =k;
int m = pln%ky;
for( int i=1; i<pln/ky ; i++){k=k+y;}
k=k+k.substring(0,m);
System.out.println("Key2 Text:\t"+k);
System.out.println("=============================================");
String c="";
for (int j=0; j<pln; j++ ){
char hsl=l.charAt((l.indexOf(k.charAt(j)) + l.indexOf(p.charAt(j)))%pms);
c=c + l.charAt((l.indexOf(k.charAt(j)) + l.indexOf(p.charAt(j)))%pms);
}
System.out.println("VC:\t"+c);
//saveEncryption(c);
saveData(c);
}
void saveString(String dataEncrypt) {
try {
String content = dataEncrypt;
File file = new File(this.fileNameEncrypt);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void saveData(String dataEncrypt) {
String kompress=dataEncrypt;//lz.compress(dataEncrypt, null);
char[]ar2=kompress.toCharArray();
byte[]data3=new byte[ar2.length];
for(int i=0;i<ar2.length;i++){
data3[i]=(byte)(int)ar2[i];
}
try{
this.saveByte(data3);
} catch (Exception e) {
e.printStackTrace();
}
}
private void saveByte(byte[] dataDecrypt) throws IOException {
String fname =this.fileNameEncrypt;
Path path = Paths.get(fname);
Files.write(path, dataDecrypt);
System.out.println("File Berhasil di dekripsi ! lihat di : " +fname);
}
private void saveEncryption(String dataEncrypt) {
try {
String content = dataEncrypt;
File file = new File(this.fileNameEncrypt);////D:/Lat/en.Sistem Berjalan.doc
System.out.println(this.fileNameEncrypt);
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println("File Berhasil di enkripsi ! lihat di : "+this.fileNameEncrypt);
} catch (IOException e) {
e.printStackTrace();
}
}
}
package cryptografi;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
/**
* This utility class implements method for uploading a whole directory from
* local computer to a remote FTP server, based on Apache Commons Net library.
*
* @author www.codejava.net
*
*/
public class FTPUtil {
/**
* Upload a whole directory (including its nested sub directories and files)
* to a FTP server.
*
* @param ftpClient
* an instance of org.apache.commons.net.ftp.FTPClient class.
* @param remoteDirPath
* Path of the destination directory on the server.
* @param localParentDir
* Path of the local directory being uploaded.
* @param remoteParentDir
* Path of the parent directory of the current directory on the
* server (used by recursive calls).
* @throws IOException
* if any network or IO error occurred.
*/
public static void uploadDirectory(FTPClient ftpClient,
String remoteDirPath, String localParentDir, String remoteParentDir)
throws IOException {
System.out.println("LISTING directory: " + localParentDir);
File localDir = new File(localParentDir);
File[] subFiles = localDir.listFiles();
if (subFiles != null && subFiles.length > 0) {
for (File item : subFiles) {
String remoteFilePath = remoteDirPath + "/" + remoteParentDir
+ "/" + item.getName();
if (remoteParentDir.equals("")) {
remoteFilePath = remoteDirPath + "/" + item.getName();
}
if (item.isFile()) {
// upload the file
String localFilePath = item.getAbsolutePath();
System.out.println("About to upload the file: " + localFilePath);
boolean uploaded = uploadSingleFile(ftpClient,
localFilePath, remoteFilePath);
if (uploaded) {
System.out.println("UPLOADED a file to: "
+ remoteFilePath);
} else {
System.out.println("COULD NOT upload the file: "
+ localFilePath);
}
} else {
// create directory on the server
boolean created = ftpClient.makeDirectory(remoteFilePath);
if (created) {
System.out.println("CREATED the directory: "
+ remoteFilePath);
} else {
System.out.println("COULD NOT create the directory: "
+ remoteFilePath);
}
// upload the sub directory
String parent = remoteParentDir + "/" + item.getName();
if (remoteParentDir.equals("")) {
parent = item.getName();
}
localParentDir = item.getAbsolutePath();
uploadDirectory(ftpClient, remoteDirPath, localParentDir,
parent);
}
}
}
}
/**
* Upload a single file to the FTP server.
*
* @param ftpClient
* an instance of org.apache.commons.net.ftp.FTPClient class.
* @param localFilePath
* Path of the file on local computer
* @param remoteFilePath
* Path of the file on remote the server
* @return true if the file was uploaded successfully, false otherwise
* @throws IOException
* if any network or IO error occurred.
*/
public static boolean uploadSingleFile(FTPClient ftpClient,String localFilePath, String remoteFilePath) throws IOException {
File localFile = new File(localFilePath);
InputStream inputStream = new FileInputStream(localFile);
try {
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
return ftpClient.storeFile(remoteFilePath, inputStream);
} finally {
inputStream.close();
}
}
}
