做数字签名不用什么代码啊,通过jdk的几个工具就可以了啊.
首先使用genkey产生密钥,然后使用jar将要强名的压缩包打包,然后使用jarsigner,通过你产生的密钥进行签名就可以了.

解决方案 »

  1.   

    OK
    这是一个我在屈城氏项目中创造的基于RSA加密会话密钥的源码,
    先造出两把KEY,PUBKEY和PRIVKEY,然后PRIVKEY加密
    用IRVKEY加密会话密钥,再用会话密钥加密信息
    信息的组成部会如下会话密钥长度(int)+会话密钥+iv(16位固定)+加密的信息解密时先把PRIVKEY解密(基于口令,口令是"isdnymk")然后拿privkey解密会话密钥,再解密信息,
    package fileencryptorrsa;import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.security.*;
    import java.security.spec.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    import java.io.*;
    import java.util.*;
    public class mainframe extends JFrame {
      JPanel contentPane;
      JButton ok_btn = new JButton();  //Construct the frame
      public mainframe() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }  //Component initialization
      private void jbInit() throws Exception  {
        contentPane = (JPanel) this.getContentPane();
        ok_btn.setBounds(new Rectangle(187, 179, 119, 34));
        ok_btn.setText("ok");
        ok_btn.addActionListener(new mainframe_ok_btn_actionAdapter(this));
        contentPane.setMaximumSize(new Dimension(2147483647, 2147483647));
        contentPane.setLayout(null);
        this.setSize(new Dimension(400, 300));
        this.setTitle("Frame Title");
        contentPane.add(ok_btn, null);
      }
      private String privateKeyFileName="privkey.bin";
      private String password="isdnymk";
      private String publicKeyFileName="pubkey.bin";
      private int ITERATIONS=1000;
      private String fileName="a.txt";
      private String encryptFile="ea.txt";
      private String decryptFile="da.txt";
      //Overridden so we can exit when window is closed
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if (e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }  private void createKey(){
        try
        {
          KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA");
          keyPairGenerator.initialize(1024);
          KeyPair keyPair=keyPairGenerator.genKeyPair();
          byte[] pubKeyBytes=keyPair.getPublic().getEncoded();
          FileOutputStream fos=new FileOutputStream(publicKeyFileName);
          fos.write(pubKeyBytes);
          fos.close();
          byte[] privateKeyBytes=keyPair.getPrivate().getEncoded();
          byte[] encryptedPrivateKeyBytes=passwordEncrypt(password.toCharArray(),privateKeyBytes);
          fos=new FileOutputStream(privateKeyFileName);
          fos.write(encryptedPrivateKeyBytes);
          fos.close();
        }catch(Exception ex){
          JOptionPane.showMessageDialog(null,"error:"+ex);
        }
      }
      private byte[] passwordEncrypt(char[] password,byte[] plaintext){
        try
        {
          byte[] salt=new byte[8];
          Random random=new Random();
          random.nextBytes(salt);
          PBEKeySpec keySpec=new PBEKeySpec(password);
          SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
          SecretKey key=keyFactory.generateSecret(keySpec);
          PBEParameterSpec paramSpec=new PBEParameterSpec(salt,ITERATIONS);
          Cipher cipher=Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
          cipher.init(Cipher.ENCRYPT_MODE,key,paramSpec);
          byte[] ciphertext=cipher.doFinal(plaintext);
          ByteArrayOutputStream baos=new ByteArrayOutputStream();
          baos.write(salt);
          baos.write(ciphertext);
          return baos.toByteArray();
        }catch(Exception ex){
          JOptionPane.showMessageDialog(null,"error:"+ex);
        }
        return null;
      }
      public byte[] passwordDecrypt(char[] password,byte[] ciphertext){
        try
        {
          byte[] salt=new byte[8];
          ByteArrayInputStream bais=new ByteArrayInputStream(ciphertext);
          bais.read(salt,0,8);
          byte[] remainingCiphertext=new byte[ciphertext.length-8];
          bais.read(remainingCiphertext,0,ciphertext.length-8);
          PBEKeySpec keySpec=new PBEKeySpec(password);
          SecretKeyFactory keyFactory=SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC");
          SecretKey key=keyFactory.generateSecret(keySpec);
          PBEParameterSpec paramSpec=new PBEParameterSpec(salt,ITERATIONS);
          Cipher cipher=Cipher.getInstance("PBEWithSHAAndTwofish-CBC");
          cipher.init(cipher.DECRYPT_MODE,key,paramSpec);
          return cipher.doFinal(remainingCiphertext);
        }catch(Exception ex){
          JOptionPane.showMessageDialog(null,"error:"+ex);
        }
        return null;
      }
      

  2.   

    public void encrypt(String fileInput){
        try
       {
         FileInputStream fis=new FileInputStream(publicKeyFileName);
         ByteArrayOutputStream baos=new ByteArrayOutputStream();
         int theByte=0;
         while((theByte=fis.read())!=-1)
         {
           baos.write(theByte);
         }
         fis.close();
         byte[] keyBytes=baos.toByteArray();
         baos.close();
         X509EncodedKeySpec keySpec=new X509EncodedKeySpec(keyBytes);
         KeyFactory keyFactory=KeyFactory.getInstance("RSA");
         PublicKey publicKey=keyFactory.generatePublic(keySpec);
         DataOutputStream output=new DataOutputStream(new FileOutputStream(encryptFile));
         Cipher rsaCipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
         rsaCipher.init(Cipher.ENCRYPT_MODE,publicKey);
         KeyGenerator rijndaelKeyGenerator=KeyGenerator.getInstance("Rijndael");
         rijndaelKeyGenerator.init(256);
         Key rijndaelKey=rijndaelKeyGenerator.generateKey();
         byte[] encodedKeyBytes=rsaCipher.doFinal(rijndaelKey.getEncoded());
         output.writeInt(encodedKeyBytes.length);
         output.write(encodedKeyBytes);
         SecureRandom random=new SecureRandom();
         byte[] iv=new byte[16];
         random.nextBytes(iv);
         output.write(iv);
         IvParameterSpec spec=new IvParameterSpec(iv);
         Cipher symmetricCipher=Cipher.getInstance("Rijndael/CBC/PKCS5Padding");
         symmetricCipher.init(Cipher.ENCRYPT_MODE,rijndaelKey,spec);
         CipherOutputStream cos=new CipherOutputStream(output,symmetricCipher);
         FileInputStream input=new FileInputStream(fileName);
         theByte=0;
         while((theByte=input.read())!=-1){
           cos.write(theByte);
         }
         input.close();
         cos.close();
         return;
       }catch(Exception ex){
         JOptionPane.showMessageDialog(null,"error:"+ex);
       }
      }  private void decrypt(String fileInput){
        try
        {
          FileInputStream fis=new FileInputStream(privateKeyFileName);
          ByteArrayOutputStream baos=new ByteArrayOutputStream();
          int theByte=0;
          while((theByte=fis.read())!=-1)
          {
            baos.write(theByte);
          }
          fis.close();
          byte[] keyBytes=baos.toByteArray();
          baos.close();
          keyBytes=passwordDecrypt(password.toCharArray(),keyBytes);
          PKCS8EncodedKeySpec keySpec=new PKCS8EncodedKeySpec(keyBytes);
          KeyFactory keyFactory=KeyFactory.getInstance("RSA");
          PrivateKey privateKey=keyFactory.generatePrivate(keySpec);
          Cipher rsaCipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
          DataInputStream dis=new DataInputStream(new FileInputStream(fileInput));
          byte[] encryptedKeyBytes=new byte[dis.readInt()];
          dis.readFully(encryptedKeyBytes);
          rsaCipher.init(Cipher.DECRYPT_MODE,privateKey);
          byte[] rijndaelKeyBytes=rsaCipher.doFinal(encryptedKeyBytes);
          SecretKey rijndaelKey=new SecretKeySpec(rijndaelKeyBytes,"Rijndael");
          byte[] iv=new byte[16];
          dis.read(iv);
          IvParameterSpec spec=new IvParameterSpec(iv);
          Cipher cipher=Cipher.getInstance("Rijndael/CBC/PKCS5Padding");
          cipher.init(Cipher.DECRYPT_MODE,rijndaelKey,spec);
          CipherInputStream cis=new CipherInputStream(dis,cipher);
          FileOutputStream fos=new FileOutputStream(decryptFile);
          theByte=0;
          while((theByte=cis.read())!=-1){
            fos.write(theByte);
          }
          cis.close();
          fos.close();
          return;
        }catch(Exception ex){
          JOptionPane.showMessageDialog(null,"error:"+ex);
        }
      }
      void ok_btn_actionPerformed(ActionEvent e) {
        try
        {
          this.createKey();
          this.encrypt("a.txt");
          this.decrypt("ea.txt");
        }catch(Exception ex){
          JOptionPane.showMessageDialog(null,"error:"+ex);
        }
      }
    }class mainframe_ok_btn_actionAdapter implements java.awt.event.ActionListener {
      mainframe adaptee;  mainframe_ok_btn_actionAdapter(mainframe adaptee) {
        this.adaptee = adaptee;
      }
      public void actionPerformed(ActionEvent e) {
        adaptee.ok_btn_actionPerformed(e);
      }
    }