Java tutorial
/* * 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 id.govca.detachedsignature.ui; import id.govca.detachedsignature.CMSController; import id.govca.detachedsignature.FileHelper; import id.govca.detachedsignature.StringFormatException; import id.govca.detachedsignature.UnmatchedSignatureException; import java.awt.Color; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; import java.security.GeneralSecurityException; import java.security.InvalidKeyException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.ParseException; import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.swing.JFileChooser; import javax.swing.JTextArea; import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.filechooser.FileSystemView; import org.bouncycastle.cms.CMSException; import org.bouncycastle.cms.CMSSignedData; import org.bouncycastle.operator.OperatorCreationException; import org.bouncycastle.util.encoders.Hex; /** * * @author Rachmawan */ public class SignatureVerifier extends javax.swing.JFrame { /** * Creates new form SignatureVerifier */ private PrintStream standardOut; public SignatureVerifier() { initComponents(); PrintStream printStream = new PrintStream(new CustomOutputStream(txtArea_Log)); // keeps reference of standard output stream standardOut = System.out; // re-assigns standard output stream and error output stream System.setOut(printStream); System.setErr(printStream); } /** * 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">//GEN-BEGIN:initComponents private void initComponents() { panelInput = new javax.swing.JPanel(); txt_ImageFile = new javax.swing.JTextField(); txt_CMSFile = new javax.swing.JTextField(); lbl_imageFile = new javax.swing.JLabel(); lbl_CMSFile = new javax.swing.JLabel(); btn_ImgSearch = new javax.swing.JButton(); btn_CMSSearch = new javax.swing.JButton(); btn_Verify = new javax.swing.JButton(); jScrollPane1 = new javax.swing.JScrollPane(); txtArea_Log = new javax.swing.JTextArea(); txt_VerifyStatus = new javax.swing.JTextField(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); panelInput.setBorder(javax.swing.BorderFactory.createTitledBorder("Input")); txt_ImageFile.setEditable(false); txt_CMSFile.setEditable(false); lbl_imageFile.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N lbl_imageFile.setText("File Citra"); lbl_CMSFile.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N lbl_CMSFile.setText("File PKCS7"); btn_ImgSearch.setText("..."); btn_ImgSearch.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_ImgSearchActionPerformed(evt); } }); btn_CMSSearch.setText("..."); btn_CMSSearch.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_CMSSearchActionPerformed(evt); } }); btn_Verify.setText("VERIFY"); btn_Verify.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btn_VerifyActionPerformed(evt); } }); javax.swing.GroupLayout panelInputLayout = new javax.swing.GroupLayout(panelInput); panelInput.setLayout(panelInputLayout); panelInputLayout.setHorizontalGroup(panelInputLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panelInputLayout.createSequentialGroup() .addGroup(panelInputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panelInputLayout.createSequentialGroup().addContainerGap() .addGroup(panelInputLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lbl_imageFile).addComponent(lbl_CMSFile)) .addGap(50, 50, 50) .addGroup(panelInputLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(txt_ImageFile, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE) .addComponent(txt_CMSFile)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(panelInputLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(btn_ImgSearch).addComponent(btn_CMSSearch))) .addGroup(panelInputLayout.createSequentialGroup().addGap(253, 253, 253) .addComponent(btn_Verify))) .addContainerGap(19, Short.MAX_VALUE))); panelInputLayout.setVerticalGroup(panelInputLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(panelInputLayout.createSequentialGroup().addContainerGap().addGroup(panelInputLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(txt_ImageFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(lbl_imageFile).addComponent(btn_ImgSearch)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(panelInputLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lbl_CMSFile) .addComponent(txt_CMSFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(btn_CMSSearch)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btn_Verify) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); txtArea_Log.setEditable(false); txtArea_Log.setColumns(20); txtArea_Log.setRows(5); jScrollPane1.setViewportView(txtArea_Log); txt_VerifyStatus.setEditable(false); txt_VerifyStatus.setHorizontalAlignment(javax.swing.JTextField.CENTER); txt_VerifyStatus.setText("STATUS"); txt_VerifyStatus.setToolTipText(""); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(panelInput, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jScrollPane1).addComponent(txt_VerifyStatus)) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(panelInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(txt_VerifyStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void btn_ImgSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_ImgSearchActionPerformed // TODO add your handling code here: JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()); jfc.addChoosableFileFilter(new FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp")); int returnValue = jfc.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE_OPTION) { File selectedFile = jfc.getSelectedFile(); txt_ImageFile.setText(selectedFile.getAbsolutePath()); } }//GEN-LAST:event_btn_ImgSearchActionPerformed private void btn_CMSSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CMSSearchActionPerformed // TODO add your handling code here: // TODO add your handling code here: JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()); jfc.addChoosableFileFilter(new FileNameExtensionFilter("DER Files", "der")); int returnValue = jfc.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE_OPTION) { File selectedFile = jfc.getSelectedFile(); txt_CMSFile.setText(selectedFile.getAbsolutePath()); } }//GEN-LAST:event_btn_CMSSearchActionPerformed private void btn_VerifyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_VerifyActionPerformed // TODO add your handling code here: txtArea_Log.setText(""); // Verify against root certificate String root_cert_path = "D:\\Tugas PTIK\\Certificate Authority\\E-voting\\RootCA1.cer"; // If the PKCS7 File was generated from Android phone File file = new File(txt_ImageFile.getText()); byte[] fileData = new byte[(int) file.length()]; DataInputStream dis = null; try { dis = new DataInputStream(new FileInputStream(file)); dis.readFully(fileData); dis.close(); } catch (FileNotFoundException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } // If the PKCS7 File was generated from Android desktop //byte[] fileData = FileHelper.binaryFileReader(txt_ImageFile.getText()); System.out.println("***VERIFYING***"); MessageDigest digest01 = null; try { digest01 = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException ex) { Logger.getLogger(SignatureVerifier.class.getName()).log(Level.SEVERE, null, ex); } byte[] input_rep = fileData; byte[] myhash = digest01.digest(input_rep); String hash_str_rep = Hex.toHexString(myhash); System.out.format("%-32s%s\n", "Digest of Content", hash_str_rep); CMSController cms_control = new CMSController(); cms_control.setRoot_cert_path(root_cert_path); byte[] cms_from_file = null; try { cms_from_file = FileHelper.binaryFileReader(txt_CMSFile.getText()); } catch (IOException ex) { Logger.getLogger(SignatureVerifier.class.getName()).log(Level.SEVERE, null, ex); } CMSSignedData cms_obj; try { cms_obj = new CMSSignedData(cms_from_file); boolean b = cms_control.VerifyCMS(cms_obj, hash_str_rep); if (b) { txt_VerifyStatus.setText("SIGNATURE VERIFIED"); txt_VerifyStatus.setBackground(Color.GREEN); System.out.println("---SIGNATURE VERIFIED---"); } } catch (CMSException | IOException | OperatorCreationException | UnmatchedSignatureException | StringFormatException | ParseException | GeneralSecurityException ex) { Logger.getLogger(SignatureVerifier.class.getName()).log(Level.SEVERE, null, ex); txt_VerifyStatus.setText("SIGNATURE VERIFICATION FAILED"); txt_VerifyStatus.setBackground(Color.RED); System.out.println("---SIGNATURE VERIFICATION FAILED---"); } }//GEN-LAST:event_btn_VerifyActionPerformed /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(SignatureVerifier.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(SignatureVerifier.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(SignatureVerifier.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(SignatureVerifier.class.getName()) .log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new SignatureVerifier().setVisible(true); } }); } public class CustomOutputStream extends OutputStream { private final JTextArea textArea; public CustomOutputStream(JTextArea textArea) { this.textArea = textArea; } @Override public void write(int b) throws IOException { // redirects data to the text area textArea.append(String.valueOf((char) b)); // scrolls the text area to the end of data textArea.setCaretPosition(textArea.getDocument().getLength()); } } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btn_CMSSearch; private javax.swing.JButton btn_ImgSearch; private javax.swing.JButton btn_Verify; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JLabel lbl_CMSFile; private javax.swing.JLabel lbl_imageFile; private javax.swing.JPanel panelInput; private javax.swing.JTextArea txtArea_Log; private javax.swing.JTextField txt_CMSFile; private javax.swing.JTextField txt_ImageFile; private javax.swing.JTextField txt_VerifyStatus; // End of variables declaration//GEN-END:variables }