Lampiran 5 : Form Dekripsi (BlowfishDec.java)
Lampiran 5 : Form Dekripsi (BlowfishDec.java) - LAMPIRAN
===
5. Lampiran 5 : Form Dekripsi (BlowfishDec.java)
/* * BlowfishEnc.java * @ Prasetyo Bawono * @ jendalsepit@gmail.com * nb: Semua konstruktor swing tidak disertakan. * Full Source: http://ono.web.id/fugu * Created on Apr 15, 2009, 8:30:05 AM */
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 BlowfishDec extends javax.swing.JDialog {
/** Creates new form Encryption */ public BlowfishDec(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("Decryption 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"));
txtPassword.setEnabled(false); btnResetPrivKey.setEnabled(false);
btnResetCipherText.setEnabled(false); btnSaveCipherText.setEnabled(false); btnOpenCipherText.setEnabled(true); btnDecryptPlainText.setEnabled(false); btnSavePlainText.setEnabled(false);
btnOpenCipherText.requestFocus(); }
private void btnResetCipherTextActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: if (evt.getSource() == this.btnResetCipherText)
this.textAreaCipher.setText(""); this.textAreaPlain.setText(""); this.txtPassword.setText("");
btnResetCipherText.setEnabled(false); btnSaveCipherText.setEnabled(false); btnOpenCipherText.setEnabled(true); btnDecryptPlainText.setEnabled(false); txtPassword.setEnabled(false); btnResetPrivKey.setEnabled(false); btnSavePlainText.setEnabled(false); textAreaPlain.setEnabled(false); btnResetCipherText.setEnabled(false); btnSaveCipherText.setEnabled(false); btnOpenCipherText.setEnabled(true); btnDecryptPlainText.setEnabled(false); txtPassword.setEnabled(false); btnResetPrivKey.setEnabled(false); btnSavePlainText.setEnabled(false); textAreaPlain.setEnabled(false);
// TODO add your handling code here: if (evt.getSource() == this.btnSaveCipherText){
JFileChooser saveCipherText = new JFileChooser(); FileNameExtensionFilter
= new FileNameExtensionFilter(".cip CipherText File", "cip", "CIP"); saveCipherText.setFileFilter(filter); saveCipherText.setDialogTitle("Save as CipherText"); 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())); out.write(this.textAreaCipher.getText()); out.close();
out
new
} catch (Exception ex) {
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
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 btnOpenCipherTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_btnOpenCipherTextActionPerformed
// TODO add your handling code here:
JFileChooser openCipherText = new JFileChooser(); openCipherText.setFileSelectionMode(JFileChooser.FILES_ONLY); openCipherText.setDialogTitle("Open CipherText File");
= new FileNameExtensionFilter("CipherText File (*.cip)", "cip", "CIP"); openCipherText.setFileFilter(filterFile); try {
FileNameExtensionFilter
filterFile
if(openCipherText.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){ File
= openCipherText.getSelectedFile(); if (selectedFile.canRead() && selectedFile.exists()) {
selectedFile
= new FileInputStream(selectedFile); InputStreamReader in= new InputStreamReader(fis); char[] buffer = new char[4194304]; int n = in.read(buffer); String CipherText = new String(buffer,0,n); textAreaCipher.setText(CipherText);
FileInputStream
fis
long sizeCipher = selectedFile.length(); String s = String.valueOf(sizeCipher); tfCipherSize.setText(s); in.close();
} } } catch (Exception e) { e.printStackTrace(); }
txtPassword.setEnabled(true); btnResetPrivKey.setEnabled(true); btnSaveCipherText.setEnabled(true); btnOpenCipherText.setEnabled(false);
btnResetCipherText.setEnabled(true); btnDecryptPlainText.setEnabled(true);
}//GEN-LAST:event_btnOpenCipherTextActionPerformed
private void btnResetPrivKeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_btnResetPrivKeyActionPerformed
// TODO add your handling code here: if (evt.getSource() == this.btnResetPrivKey) // TODO add your handling code here: if (evt.getSource() == this.btnResetPrivKey)
private void btnDecryptPlainTextActionPerformed(java.awt.event.ActionEvent
evt) {//GEN-FIRST:event_btnDecryptPlainTextActionPerformed
// Decrypt: // ======== // Cipher(String) -> Cipher(String->Byte Array[]) -> Plain(Byte
Array[]->String) -> Plain(String) if(evt.getSource()==this.btnDecryptPlainText){ if (txtPassword!=null){ Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
String textCipherStr = textAreaCipher.getText(); char[] textPassword = txtPassword.getPassword();
byte[] textCipherByte = textCipherStr.getBytes(); byte[]
= (new String(textPassword)).getBytes();
textPasswordByte
textAreaPlain.setBackground(Color.WHITE); textAreaPlain.setEnabled(true); btnSavePlainText.setEnabled(true);
// ECB MODE if (rbECB.isSelected()) {
try {
// Start Counting long ecbStart; long ecbEnd; long ecbTime; ecbStart = System.currentTimeMillis();
// Define Algorithm & its Mode SecretKeySpec
= new SecretKeySpec(textPasswordByte, "Blowfish");
key
= Cipher.getInstance("Blowfish/ECB/PKCS7Padding","BC");
Cipher
cipher
// Decryption cipher.init(Cipher.DECRYPT_MODE, key); byte[]
plainText = cipher.doFinal(textCipherByte); plainText = cipher.doFinal(textCipherByte);
String ptLengthStr = Integer.toString(ptLength); tfPlainSize.setText(ptLengthStr);
// Benchmark for performance ecbEnd = System.currentTimeMillis(); //
Encryption processing time (ms)
ecbTime = (ecbEnd - ecbStart); String b = String.valueOf(ecbTime); tfTimeDec.setText(b);
} catch (Exception e) {
System.out.println(e); } }
// CBC MODE if (rbCBC.isSelected()) {
try {
// Start Counting long cbcStart; long cbcEnd; long cbcTime; cbcStart = System.currentTimeMillis();
// Inisialitazion Vector byte[] ivBytes = new byte[]{
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
// Define Algorithm and Mode SecretKeySpec
= new SecretKeySpec(textPasswordByte, "Blowfish");
key
= new IvParameterSpec(ivBytes);
IvParameterSpec
ivSpec
= Cipher.getInstance("Blowfish/CBC/PKCS5Padding","BC");
Cipher
cipher
// Decryption cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); byte[]
plainText = cipher.doFinal(textCipherByte); plainText = cipher.doFinal(textCipherByte);
String ptLengthStr = Integer.toString(ptLength); tfPlainSize.setText(ptLengthStr);
// Benchmark for performance cbcEnd = System.currentTimeMillis(); //
Encryption processing time (ms)
cbcTime = (cbcEnd - cbcStart); String b = String.valueOf(cbcTime); tfTimeDec.setText(b);
} catch (Exception e) {
System.out.println(e); } }
// OFB MODE if (rbOFB.isSelected()) {
try {
// Start Counting long ofbStart; long ofbEnd; long ofbTime; ofbStart = System.currentTimeMillis();
// Inisialitazion Vector byte[] ivBytes = new byte[]{
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
// Define Algorithm and Mode SecretKeySpec
= new SecretKeySpec(textPasswordByte, "Blowfish");
key
= new IvParameterSpec(ivBytes);
IvParameterSpec
ivSpec
= Cipher.getInstance("Blowfish/OFB/NoPadding","BC");
Cipher
cipher
// Decryption cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); // Decryption cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
byte[]
int ptLength = cipher.update(textCipherByte, 0, textCipherByte.length, plainText, 0); ptLength += cipher.doFinal(plainText, ptLength); String plain = new String(plainText); textAreaPlain.setText(plain);
String ptLengthStr = Integer.toString(ptLength); tfPlainSize.setText(ptLengthStr);
// Benchmark for performance ofbEnd = System.currentTimeMillis(); //
Encryption processing time (ms)
ofbTime = (ofbEnd - ofbStart); String b = String.valueOf(ofbTime); tfTimeDec.setText(b);
} catch (Exception e) {
System.out.println(e); } }
// CFB MODE if (rbCFB.isSelected()) {
try {
// Start Counting long cfbStart; long cfbEnd; long cfbTime; cfbStart = System.currentTimeMillis();
// Inisialitazion Vector byte[] ivBytes = new byte[]{
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00
// Define Algorithm and Mode SecretKeySpec
= new SecretKeySpec(textPasswordByte, "Blowfish");
key
= new IvParameterSpec(ivBytes);
IvParameterSpec
ivSpec
= Cipher.getInstance("Blowfish/CFB/NoPadding","BC");
Cipher
cipher
// Decryption // Decryption
plainText = cipher.doFinal(textCipherByte);
int ptLength = cipher.update(textCipherByte, 0, textCipherByte.length, plainText, 0); ptLength += cipher.doFinal(plainText, ptLength); String plain = new String(plainText); textAreaPlain.setText(plain);
String ptLengthStr = Integer.toString(ptLength); tfPlainSize.setText(ptLengthStr);
// Benchmark for performance cfbEnd = System.currentTimeMillis(); //
Encryption processing time (ms)
cfbTime = (cfbEnd - cfbStart); String b = String.valueOf(cfbTime); tfTimeDec.setText(b);
} catch (Exception e) {
System.err.println(e); } } JOptionPane.showMessageDialog(null,
"Dekripsi Berhasil..", "Decryption Successfully", JOptionPane.INFORMATION_MESSAGE); } if(txtPassword.getPassword().length<8){
JOptionPane.showMessageDialog(null, "Private key minimal
8 karakter", "Error", JOptionPane.ERROR_MESSAGE); } if (txtPassword.getPassword().length==0) {
JOptionPane.showMessageDialog(null, "Private key tidak boleh kosong!", "Error Message", JOptionPane.ERROR_MESSAGE); }
btnResetPrivKey.setEnabled(false);; txtPassword.setEnabled(false);
}//GEN-LAST:event_btnDecryptPlainTextActionPerformed
private void btnSavePlainTextActionPerformed(java.awt.event.ActionEvent evt) {//GEN- FIRST:event_btnSavePlainTextActionPerformed
// TODO add your handling code here: if (evt.getSource() == this.btnSavePlainText){
JFileChooser savePlainText = new JFileChooser();
= new FileNameExtensionFilter("Document File (*.txt)", "txt", "TXT"); savePlainText.setFileFilter(filter); savePlainText.setDialogTitle("Save PlainText to File"); int option = savePlainText.showSaveDialog(this); // Jika OK if (option == JFileChooser.APPROVE_OPTION){
// Buat buffered writer untuk menulis BufferedWriter
BufferedWriter(new FileWriter(savePlainText.getSelectedFile().getPath())); out.write(this.textAreaPlain.getText()); out.close();
out
new
} 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
/** * @param args the command line arguments */ public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() { public void run() { BlowfishDec
BlowfishDec(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 btnDecryptPlainText; private javax.swing.JButton btnOpenCipherText; private javax.swing.JButton btnResetCipherText; 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 tfCipherSize; private javax.swing.JTextField tfPlainSize; private javax.swing.JTextField tfTimeDec; private javax.swing.JPasswordField txtPassword; // End of variables declaration//GEN-END:variables