Lampiran 4 : Form Enkripsi (BlowfishEnc.java)

Lampiran 4 : Form Enkripsi (BlowfishEnc.java) - LAMPIRAN

===

4. Lampiran 4 : Form Enkripsi (BlowfishEnc.java)

/* * BlowfishEnc.java * @ Prasetyo Bawono * @ [email protected] * nb: Semua konstruktor swing tidak disertakan. * Full Source: http://ono.web.id/fugu

package fugu.mode;

import fugu.FuguApp; import java.awt.Color; import java.awt.Dimension; import java.awt.Toolkit; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStreamReader; import java.security.Security; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.filechooser.FileNameExtensionFilter; import org.jdesktop.application.Application; import org.jdesktop.application.ResourceMap;

/** * * @author prasetyo bawono */

public class BlowfishEnc extends javax.swing.JDialog {

final static int TESTBUFSIZE = 100000; final static int TESTLOOPS = 10000;

/** Creates new form Encryption */ public BlowfishEnc(java.awt.Frame parent, boolean modal){

super(parent, modal); initComponents();

= Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = this.getSize();

Dimension

screenSize screenSize

frameSize.width = screenSize.width; }

frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); this.setTitle("Encryption Mode");

this.setLocation((screenSize.width

// Define Clickable textAreaPlain.setEnabled(false); ResourceMap

= Application.getInstance(fugu.FuguApp.class).getContext().getResourceMap(B lowfishEnc.class);

resourceMap

textAreaPlain.setBackground(resourceMap.getColor("textAreaCipher.backgrou nd"));

btnNewPlainText.setEnabled(true); btnSavePlainText.setEnabled(false); btnOpenPlainText.setEnabled(false); btnEncryptPlainText.setEnabled(false); btnSaveCipherText.setEnabled(false); btnResetPrivKey.setEnabled(false); txtPassword.setEnabled(false);

btnNewPlainText.requestFocus();

private void btnNewPlainTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_btnNewPlainTextActionPerformed

// TODO add your handling code here: if (evt.getSource() == this.btnNewPlainText){

// Define Clickable textAreaPlain.setEnabled(true); textAreaPlain.setBackground(Color.WHITE);

btnNewPlainText.setEnabled(false); btnSavePlainText.setEnabled(true); btnOpenPlainText.setEnabled(true); btnEncryptPlainText.setEnabled(true); btnResetPrivKey.setEnabled(true); txtPassword.setEnabled(true);

// setAuthPlainText(true); this.textAreaPlain.setText(""); this.textAreaCipher.setText(""); this.txtPassword.setText(""); this.tfBenchmark.setText(""); this.tfCipherSize.setText("");

} }//GEN-LAST:event_btnNewPlainTextActionPerformed

private void btnSavePlainTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_btnSavePlainTextActionPerformed

if (evt.getSource() == this.btnSavePlainText){ JFileChooser savePlainText = new JFileChooser(); FileNameExtensionFilter

= new FileNameExtensionFilter("Text Documents (*.txt)", "txt", "java"); savePlainText.setFileFilter(filter); savePlainText.setDialogTitle("Save PlainText to File"); int option = savePlainText.showSaveDialog(this); // Jika OK if (option == JFileChooser.APPROVE_OPTION){

filter

try {

// Buat buffered writer untuk menulis BufferedWriter

BufferedWriter(new FileWriter(savePlainText.getSelectedFile().getPath())); // BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(savePlainText.getSelectedFile().getPath()),"UTF-8"));

out

new

out.write(this.textAreaPlain.getText()); out.close();

} catch (Exception ex) {

System.out.println(ex.getMessage()); } JOptionPane.showMessageDialog(null,

"Sukses Menyimpan File

File: "+savePlainText.getSelectedFile(),"PlainText

"+savePlainText.getSelectedFile().getName()+"\nLokasi

Saved!", JOptionPane.INFORMATION_MESSAGE); } else JOptionPane.showMessageDialog(null,

"Something Happened?", "Gagal Menyimpan File", JOptionPane.ERROR_MESSAGE); }

}//GEN-LAST:event_btnSavePlainTextActionPerformed

private void menuNotepadQuitActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_menuNotepadQuitActionPerformed

// TODO add your handling code here:

FuguApp.getApplication().ShowMainMenu(); FuguApp.getInstanceMainMenu().setAuthentication(true); this.dispose();

}//GEN-LAST:event_menuNotepadQuitActionPerformed

private void btnOpenPlainTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_btnOpenPlainTextActionPerformed

JFileChooser openPlainText = new JFileChooser(); openPlainText.setFileSelectionMode(JFileChooser.FILES_ONLY); openPlainText.setDialogTitle("Open Plain Text File(s)");

= new FileNameExtensionFilter("Text Documents (*.txt)", "txt", "java"); openPlainText.setFileFilter(filterFile); try { if (openPlainText.showOpenDialog(this)==JFileChooser.APPROVE_OPTION) { File

FileNameExtensionFilter

filterFile

= openPlainText.getSelectedFile(); if(selectedFile.canRead() && selectedFile.exists()){

selectedFile

= new FileInputStream(selectedFile); InputStreamReader in= new InputStreamReader(fis); char[] buffer = new char[4194304]; // 4Mb

FileInputStream

fis

buffer int n = in.read(buffer); String plainText = new String(buffer,0,n); textAreaPlain.setText(plainText);

long sizePlain = selectedFile.length(); String s = String.valueOf(sizePlain); tfPlainSize.setText(s); in.close();

} } } catch (Exception e) {

e.printStackTrace(); } }//GEN-LAST:event_btnOpenPlainTextActionPerformed

private void btnResetPrivKeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_btnResetPrivKeyActionPerformed

// TODO add your handling code here: if (evt.getSource() == this.btnResetPrivKey)

this.txtPassword.setText(""); }//GEN-LAST:event_btnResetPrivKeyActionPerformed this.txtPassword.setText(""); }//GEN-LAST:event_btnResetPrivKeyActionPerformed

evt) {//GEN-FIRST:event_btnEncryptPlainTextActionPerformed

// Encrypt: // ======== // Plain(String) -> Plain(String->Byte Array[]) -> Cipher(Byte

Array[]->String) -> Cipher(String)

long charCount = textAreaPlain.getText().length(); String s = String.valueOf(charCount); tfPlainSize.setText(s); if (evt.getSource()==this.btnEncryptPlainText) {

if (textAreaPlain.getText().length()>0) { if(txtPassword.getPassword().length==0){ try { JOptionPane.showMessageDialog(null, "Private key tidak boleh kosong!", "Error Message", JOptionPane.ERROR_MESSAGE); btnResetPrivKey.setEnabled(true); txtPassword.setEnabled(true); txtPassword.requestFocus();

} catch (IllegalArgumentException e) { System.err.print(e);

if(txtPassword.getPassword().length<8 && txtPassword.getPassword().length>0){ try { JOptionPane.showMessageDialog(null, "Private key minimal 8 karakter", "Error", JOptionPane.ERROR_MESSAGE); btnResetPrivKey.setEnabled(true); txtPassword.setEnabled(true); txtPassword.requestFocus();

} catch (Exception e) {

System.err.print(e);

if (txtPassword.getPassword().length>=8) { Security.addProvider(new

org.bouncycastle.jce.provider.BouncyCastleProvider()); String textPlainStr = textAreaPlain.getText(); char[] textPassword = txtPassword.getPassword(); org.bouncycastle.jce.provider.BouncyCastleProvider()); String textPlainStr = textAreaPlain.getText(); char[] textPassword = txtPassword.getPassword();

= (new String(textPassword)).getBytes();

textPasswordByte

// ECB MODE if (rbECB.isSelected()) {

try { long ecbStart; long ecbEnd; long ecbTime; ecbStart = System.currentTimeMillis(); SecretKeySpec

= new SecretKeySpec(textPasswordByte, "Blowfish");

key

cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding","BC");

Cipher

// Encryption cipher.init(Cipher.ENCRYPT_MODE, key); byte[]

= cipher.doFinal(textPlainStrByte);

cipherText

ctLength = cipher.update(textPlainStrByte, 0, textPlainStrByte.length, cipherText, 0);

int

cipher.doFinal(cipherText, ctLength);

ctLength

textAreaCipher.setText(new

String(cipherText));

ctLengthStr = Integer.toString(ctLength); tfCipherSize.setText(ctLengthStr);

String

// Benchmark for performance ecbEnd = System.currentTimeMillis(); //

Encryption processing time (ms) ecbTime = (ecbEnd - ecbStart); // nano to milli String b = String.valueOf(ecbTime); // 9223372036854775807 9223372036854775807

tfBenchmark.setText(b);

} catch (Exception e) { System.err.println(e); } }

// CBC MODE // CBC MODE

try { long cbcStart; long cbcEnd; long cbcTime; // Inisialization Vector byte[] ivBytes = new byte[]{

0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00

// Define Algorithm and Mode cbcStart = System.currentTimeMillis(); SecretKeySpec

= new SecretKeySpec(textPasswordByte, "Blowfish");

key

= new IvParameterSpec(ivBytes);

IvParameterSpec

ivSpec

= Cipher.getInstance("Blowfish/CBC/PKCS5Padding", "BC");

Cipher cipher

// Encryption cipher.init(Cipher.ENCRYPT_MODE,

key, ivSpec);

= cipher.doFinal(textPlainStrByte);

byte[]

cipherText

ctLength = cipher.update(textPlainStrByte, 0, textPlainStrByte.length, cipherText, 0);

int

cipher.doFinal(cipherText, ctLength);

ctLength

textAreaCipher.setText(new

String(cipherText));

ctLengthStr = Integer.toString(ctLength); tfCipherSize.setText(ctLengthStr);

String

// Benchmark for performance cbcEnd = System.currentTimeMillis(); //

Encryption processing time (ms) cbcTime = (cbcEnd - cbcStart); String b = String.valueOf(cbcTime); //

tfBenchmark.setText(b); } catch (Exception e) { System.err.println(e); }

// OFB MODE if (rbOFB.isSelected()){

try { long ofbStart; long ofbEnd; long ofbTime; // Initialization Vector IV byte[] ivBytes = new byte[]{

0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00

// Define Algorithm ofbStart = System.currentTimeMillis(); SecretKeySpec

= new SecretKeySpec(textPasswordByte, "Blowfish");

key

= new IvParameterSpec(ivBytes);

IvParameterSpec

ivSpec

= Cipher.getInstance("Blowfish/OFB/NoPadding", "BC");

Cipher cipher

// Encryption cipher.init(Cipher.ENCRYPT_MODE,

key, ivSpec);

= cipher.doFinal(textPlainStrByte);

byte[]

cipherText

ctLength = cipher.update(textPlainStrByte, 0, textPlainStrByte.length, cipherText, 0);

int

cipher.doFinal(cipherText, ctLength);

ctLength

textAreaCipher.setText(new

String(cipherText));

ctLengthStr = Integer.toString(ctLength); tfCipherSize.setText(ctLengthStr);

String

// Benchmark for performance ofbEnd = System.currentTimeMillis(); // Encryption processing time (ms) ofbTime = (ofbEnd - ofbStart); String b = String.valueOf(ofbTime); //

tfBenchmark.setText(b);

} catch (Exception e) { System.err.println(e); } }

// CFB MODE if (rbCFB.isSelected()){

try { long cfbStart; long cfbEnd; long cfbTime; // Initialization Vector IV byte[] ivBytes = new byte[]{

0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00

// Define Algorithm cfbStart = System.currentTimeMillis(); SecretKeySpec

= new SecretKeySpec(textPasswordByte, "Blowfish");

key

= new IvParameterSpec(ivBytes);

IvParameterSpec

ivSpec

= Cipher.getInstance("Blowfish/CFB/NoPadding", "BC");

Cipher cipher

// Encryption cipher.init(Cipher.ENCRYPT_MODE,

key, ivSpec);

= cipher.doFinal(textPlainStrByte);

byte[]

cipherText

ctLength = cipher.update(textPlainStrByte, 0, textPlainStrByte.length, cipherText, 0);

int

cipher.doFinal(cipherText, ctLength);

ctLength

textAreaCipher.setText(new

String(cipherText));

ctLengthStr = Integer.toString(ctLength); tfCipherSize.setText(ctLengthStr);

String

// Benchmark for performance cfbEnd = System.currentTimeMillis(); //

Encryption processing time (ms) cfbTime = (cfbEnd - cfbStart);

String b = String.valueOf(cfbTime); // 9223372036854775807 9223372036854775807

tfBenchmark.setText(b);

} catch (Exception e) { System.err.println(e); } } JOptionPane.showMessageDialog(null,

"Enkripsi Berhasil..", "Encryption Successfully", JOptionPane.INFORMATION_MESSAGE); btnResetPrivKey.setEnabled(false); txtPassword.setEnabled(false);

} } else {

JOptionPane.showMessageDialog(null, "Plainteks tidak boleh kosong", "Error Message", JOptionPane.ERROR_MESSAGE); } } btnNewPlainText.setEnabled(true); btnSavePlainText.setEnabled(false); btnOpenPlainText.setEnabled(false); btnEncryptPlainText.setEnabled(false);

btnSaveCipherText.setEnabled(true); btnSaveCipherText.requestFocus();

}//GEN-LAST:event_btnEncryptPlainTextActionPerformed

private void btnSaveCipherTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_btnSaveCipherTextActionPerformed

if (evt.getSource() == this.btnSaveCipherText){ JFileChooser saveCipherText = new JFileChooser(); FileNameExtensionFilter

= new FileNameExtensionFilter("CipherText File (*.cip)", "cip"); saveCipherText.setFileFilter(filter); saveCipherText.setDialogTitle("Save CipherText to File"); int option = saveCipherText.showSaveDialog(this); // Jika OK if (option == JFileChooser.APPROVE_OPTION){

filter

try {

// Buat buffered writer untuk menulis BufferedWriter

BufferedWriter(new FileWriter(saveCipherText.getSelectedFile().getPath())); // BufferedWriter outs = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(saveCipherText.getSelectedFile().getPath()),"ASCII"));

outs

new

outs.write(this.textAreaCipher.getText()); outs.write(this.textAreaCipher.getText());

System.out.println(ex.getMessage()); } JOptionPane.showMessageDialog(null,

"Sukses Menyimpan File

File: "+saveCipherText.getSelectedFile(),"CipherText

"+saveCipherText.getSelectedFile().getName()+"\nLokasi

Saved!", JOptionPane.INFORMATION_MESSAGE); } else

JOptionPane.showMessageDialog(null, "Something Happened?", "Gagal Menyimpan File", JOptionPane.ERROR_MESSAGE); } }//GEN-LAST:event_btnSaveCipherTextActionPerformed

/** * @param args the command line arguments */ public static void main(String args[]) {

java.awt.EventQueue.invokeLater(new Runnable() { public void run() { BlowfishEnc

BlowfishEnc(new javax.swing.JFrame(), true); dialog.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(java.awt.event.WindowEvent

System.exit(0); } }); dialog.setVisible(true);

} }); }

// Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnEncryptPlainText; private javax.swing.JButton btnNewPlainText; private javax.swing.JButton btnOpenPlainText; private javax.swing.JButton btnResetPrivKey; private javax.swing.JButton btnSaveCipherText; private javax.swing.JButton btnSavePlainText; private javax.swing.ButtonGroup buttonGroup1; private javax.swing.JLabel fugu; 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.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JMenuBar jMenuBar1; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JLabel lblEncryption; private javax.swing.JLabel lblOpenFile; private javax.swing.JLabel lblPrivKey; private javax.swing.JLabel lblPrivateKey; private javax.swing.JLabel lblSaveTo; private javax.swing.JMenu menuNotepadFile; private javax.swing.JMenuItem menuNotepadQuit; private javax.swing.JPanel panelCipherText; private javax.swing.JPanel panelPlainText; private javax.swing.JRadioButton rbCBC; private javax.swing.JRadioButton rbCFB; private javax.swing.JRadioButton rbECB; private javax.swing.JRadioButton rbOFB; private javax.swing.JTextArea textAreaCipher; private javax.swing.JTextArea textAreaPlain; private javax.swing.JTextField tfBenchmark; private javax.swing.JTextField tfCipherSize; private javax.swing.JTextField tfPlainSize; private javax.swing.JPasswordField txtPassword; // End of variables declaration//GEN-END:variables

public void setAuthPlainText(boolean value){ textAreaPlain.setEnabled(value); if(value==false){

btnNewPlainText.setEnabled(true); btnSavePlainText.setEnabled(false); btnOpenPlainText.setEnabled(true); btnEncryptPlainText.setEnabled(false);

} else{

btnSavePlainText.setEnabled(true); btnEncryptPlainText.setEnabled(true);