2012-06-24 25 views
-1

我正在製作一個php應用程序,我已經在2到60人的服務器上生成了需要用智能卡(使用智能卡讀卡器)digitaly簽名的pdf文件。php javascript java applet服務器上的智能卡簽署pdf

一個簡單的按鈕,上面寫着

註冊...

然後,如果用戶在閱讀器應用程序沒有智能卡說:

插入卡,

一個新的彈出:

輸入引腳,

,所有結束了:

文件簽署。

可以這樣做嗎?

回答

0
package compensatemeonline8; 

import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.Rectangle; 
import com.itextpdf.text.pdf.*; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.security.*; 
import java.security.cert.CertificateException; 
import java.util.Enumeration; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JOptionPane; 
import java.io.File; 
import java.lang.reflect.Method; 


public class CompensateMeOnlineApplet extends java.applet.Applet { 
private static final long serialVersionUID = -5290769300644275624L; 
String alias = new String(); 

@Override 
public void init() { 
    try { 

     try { 
      java.awt.EventQueue.invokeAndWait(new Runnable() { 
       @Override 
       public void run() { 
        initComponents(); 
       } 
      }); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     File file = new File(".."); 
     System.out.println("where the pdf you should :" 
       + file.getAbsolutePath()); 

     KeyStore keyStore = null; 
     Provider pjacapi = Security.getProvider("CAPI"); 
     final Provider pmscapi = Security.getProvider("SunMSCAPI"); 

     System.out.println("pmscapi:" + pmscapi); 

     if (pmscapi != null) { 
      AccessController 
        .doPrivileged(new PrivilegedExceptionAction<Boolean>() { 
         @Override 
         public Boolean run() throws Exception { 
          pmscapi.setProperty("Signature.SHA1withRSA", 
            "sun.security.mscapi.RSASignature$SHA1"); 
          return true; 
         } 
        }); 

      keyStore = KeyStore.getInstance("Windows-MY", pmscapi); 
      System.out.println("keystore: " + keyStore); 

     } else if (pjacapi != null) { 
      keyStore = KeyStore.getInstance("CAPI"); 
     } 
     if (keyStore != null) { 
      keyStore.load(null, null); 
      Enumeration<String> enums = keyStore.aliases(); 

      while (enums.hasMoreElements()) { 
       this.jComboBox1.addItem((String) enums.nextElement()); 
      } 
     } 

    } catch (IOException ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } catch (NoSuchAlgorithmException ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } catch (CertificateException ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } catch (KeyStoreException ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } catch (Exception ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } 
} 

/** 
* This method is called from within the init() method to initialize the 
* form. WARNING: Do NOT modify this code. The content of this method is 
* always regenerated by the Form Editor. 
*/ 
// <editor-fold defaultstate="collapsed" desc="Generated Code"> 
private void initComponents() { 

    jComboBox1 = new javax.swing.JComboBox(); 
    jButton1 = new javax.swing.JButton(); 
    jButton2 = new javax.swing.JButton(); 

    setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR)); 

    jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(
      new String[] { "Choose signature to sign with" })); 

    jButton1.setText("Sign"); 
    jButton1.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton1ActionPerformed(evt); 
     } 
    }); 

    jButton2.setText("Exit"); 
    jButton2.addActionListener(new java.awt.event.ActionListener() { 
     public void actionPerformed(java.awt.event.ActionEvent evt) { 
      jButton2ActionPerformed(evt); 
     } 
    }); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this); 
    this.setLayout(layout); 
    layout.setHorizontalGroup(layout 
      .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(
        layout.createSequentialGroup() 
          .addContainerGap() 
          .addGroup(
            layout.createParallelGroup(
              javax.swing.GroupLayout.Alignment.LEADING) 
              .addGroup(
                layout.createSequentialGroup() 
                  .addComponent(
                    jComboBox1, 
                    javax.swing.GroupLayout.PREFERRED_SIZE, 
                    javax.swing.GroupLayout.DEFAULT_SIZE, 
                    javax.swing.GroupLayout.PREFERRED_SIZE) 
                  .addPreferredGap(
                    javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) 
                  .addComponent(
                    jButton1, 
                    javax.swing.GroupLayout.PREFERRED_SIZE, 
                    75, 
                    javax.swing.GroupLayout.PREFERRED_SIZE) 
                  .addGap(0, 
                    89, 
                    Short.MAX_VALUE)) 
              .addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING, 
                layout.createSequentialGroup() 
                  .addGap(0, 
                    0, 
                    Short.MAX_VALUE) 
                  .addComponent(
                    jButton2))) 
          .addContainerGap())); 
    layout.setVerticalGroup(layout 
      .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(
        layout.createSequentialGroup() 
          .addContainerGap() 
          .addGroup(
            layout.createParallelGroup(
              javax.swing.GroupLayout.Alignment.BASELINE) 
              .addComponent(
                jComboBox1, 
                javax.swing.GroupLayout.PREFERRED_SIZE, 
                javax.swing.GroupLayout.DEFAULT_SIZE, 
                javax.swing.GroupLayout.PREFERRED_SIZE) 
              .addComponent(jButton1)) 
          .addPreferredGap(
            javax.swing.LayoutStyle.ComponentPlacement.RELATED, 
            23, Short.MAX_VALUE) 
          .addComponent(jButton2).addContainerGap())); 
}// </editor-fold> 

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 
    alias = (String) jComboBox1.getSelectedItem(); 
    if (alias.equals("Choose signature to sign with")) { 
     JOptionPane.showMessageDialog(jComboBox1, 
       "Must select a signature to sign with!"); 
     // System.exit(1); 
     return; 
    } 
    // JOptionPane.showMessageDialog(jComboBox1, alias); 
    try { 
     potpisi(); 
     JOptionPane.showMessageDialog(jComboBox1, "Document signed"); 

     // TODO add your handling code here: 
    } catch (KeyStoreException ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } catch (IOException ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } catch (NoSuchAlgorithmException ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } catch (CertificateException ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } catch (UnrecoverableKeyException ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } catch (DocumentException ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } catch (InvalidKeyException ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } catch (SignatureException ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } catch (Exception ex) { 
     Logger.getLogger(CompensateMeOnlineApplet.class.getName()).log(
       Level.SEVERE, null, ex); 
    } 
} 

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 
    System.exit(1); // TODO add your handling code here: 
} 

// Variables declaration - do not modify 
private javax.swing.JButton jButton1; 
private javax.swing.JButton jButton2; 
private javax.swing.JComboBox jComboBox1; 

// End of variables declaration 
@Override 
public void start() { 

} 

public void potpisi() throws KeyStoreException, IOException, 
     NoSuchAlgorithmException, CertificateException, 
     UnrecoverableKeyException, DocumentException, InvalidKeyException, 
     SignatureException, Exception { 

    KeyStore ks = null; 
    Provider pjacapi = Security.getProvider("CAPI"); 
    final Provider pmscapi = Security.getProvider("SunMSCAPI"); 
    if (pmscapi != null) { 
     AccessController 
       .doPrivileged(new PrivilegedExceptionAction<Boolean>() { 
        @Override 
        public Boolean run() throws Exception { 
         pmscapi.setProperty("Signature.SHA1withRSA", 
           "sun.security.mscapi.RSASignature$SHA1"); 
         return true; 
        } 
       }); 

     ks = KeyStore.getInstance("Windows-MY", pmscapi); 
    } else if (pjacapi != null) { 
     ks = KeyStore.getInstance("CAPI"); 
    } 
    if (ks != null) { 
     ks.load(null, null); 
    } 


    ks.load(null, null); 
    // ovo smo ubacili 
    Enumeration en = ks.aliases(); 


    PrivateKey key = (PrivateKey) ks 
      .getKey(alias, "password".toCharArray()); 

    java.security.cert.Certificate[] chain = ks.getCertificateChain(alias); 

    PdfReader reader = new PdfReader(
      "Compensate.pdf"); 
    FileOutputStream fout = new FileOutputStream("signed.pdf"); 
    PdfStamper stp = PdfStamper.createSignature(reader, fout, '\0', null, 
      true); 
    PdfSignatureAppearance appearance = stp.getSignatureAppearance(); 

    appearance.setCrypto(null, chain, null, 
      PdfSignatureAppearance.SELF_SIGNED); 

    appearance.setReason("Potpis kompenzacije"); 
    appearance.setLocation("Foobar"); 
    appearance.setVisibleSignature(new Rectangle(100, 100, 200, 200), 1, 
      "jedan"); 

    appearance.setExternalDigest(new byte[128], null, "RSA"); 
    appearance.preClose(); 
    Signature signature = Signature.getInstance("SHA1withRSA"); 
    signature.initSign(key); 
    byte buf[] = new byte[8192]; 
    int n; 
    InputStream inp = appearance.getRangeStream(); 
    while ((n = inp.read(buf)) > 0) { 
     signature.update(buf, 0, n); 
    } 
    PdfPKCS7 sig = appearance.getSigStandard().getSigner(); 
    sig.setExternalDigest(signature.sign(), null, "RSA"); 
    PdfDictionary dic = new PdfDictionary(); 
    dic.put(PdfName.CONTENTS, 
      new PdfString(sig.getEncodedPKCS1()).setHexWriting(true)); 

    appearance.close(dic); 

} 

} 
+0

''http://www.compensatemeonline.com/applet/japplet.html。無法提供代碼或手冊...;(''這裏是代碼... –

1

您需要在用戶的計算機上運行,​​並允許其脫離常規沙箱,如簽名的Java小程序。有了這樣的事情,一些JavaScript + AJAX和來回與您的服務器通信,你當然可以做你正在談論的。

您可能需要從用戶計算機上傳文件。

例如

Understanding Java Card 2.0

How to write a java smartcard applet

official Java Card technology site

我,是比利時公民,也有一個智能卡型的id-card,可用於簽署通過網絡文件或提供訪問的東西通過一個applet

這很容易,現在這是另一個問題。 :)

+0

是的,我需要一個簽名的小,但我不知道我將需要PHP的一部分,或者我可以做一些其他way.I知道TCPDF PDF庫實現簽約PDF文件,但是,因爲我需要更多的人來簽署一個相同的文件我不知道是否正確的方式去... –

+0

總而言之,在我的情況下,它將在客戶端通過使用php tcpdf生成PDF文件與空簽名的地方簽署,在客戶端和上傳,如果有人有一些更好的解決方案... –

+0

我想知道如果我可以簽署,保存並上傳使用JavaScript文件Api在瀏覽器中打開的pdf。 –