Method中的IllegalBlockSizeException

时间:2014-10-21 09:26:26

标签: java

按钮代码:

private void DecryptBActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        final String plainText;
        if (!areKeysPresent()) {
        // Method generates a pair of keys using the RSA algorithm and stores it
        // in their respective files
        generateKey();
      }
        if(dtextbox.getText().equals(""))
        {
            final JPanel panel = new JPanel();
            JOptionPane.showMessageDialog(panel, "Could not open file", "Error", JOptionPane.ERROR_MESSAGE);
            //JOptionPane.showMessageDialog(JOptionPane.ERROR_MESSAGE, "Error: No File Selected");
            return;
        }
        /*if(saveintb.getText().equals(""))
        {
            final JPanel panel = new JPanel();
            JOptionPane.showMessageDialog(panel, "Could not open file", "Error", JOptionPane.ERROR_MESSAGE);
            //JOptionPane.showMessageDialog(JOptionPane.ERROR_MESSAGE, "Error: No File Selected");
            return;
        }*/
        String ext="";
        int mid= filename.lastIndexOf(".");      
        ext=filename.substring(mid+1,filename.length());
        String fileString = "";
        try {                                        
            // TODO add your handling code here:
            int i;
            FileInputStream fin = null;
            try {
                fin = new FileInputStream(filename);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(EncryptForm.class.getName()).log(Level.SEVERE, null, ex);
            }
            do{
                i=fin.read();
                if(i!=-1)
                {
                    fileString +=(char) i;
                }
            }while(i!=-1);
            fin.close();
            //JOptionPane.showMessageDialog(null, fileString);
        } catch (IOException ex) {
        Logger.getLogger(EncryptForm.class.getName()).log(Level.SEVERE, null, ex);
        }
        byte[] bytes = fileString.getBytes();
        System.out.println("String to Bytes0: "+bytes);
        if(ext.equals("RSA"))
        {
            try {
                ObjectInputStream inputStream;
 
 
                inputStream = new ObjectInputStream(new FileInputStream(PRIVATE_KEY_FILE));
                final PrivateKey privateKey = (PrivateKey) inputStream.readObject();
                byte[] byteData = fileString.getBytes();
                JOptionPane.showMessageDialog(null, byteData);
 
                try
                {     
                   JOptionPane.showMessageDialog(null, privateKey); 
                   System.out.println("Private key: "+privateKey);
                    plainText = decrypt(byteData, privateKey);
                }
                catch(Exception ex)
                {
                    System.out.println(ex);
                    JOptionPane.showMessageDialog(null, ex);
                }
                pText = plainText;
            } catch (IOException | ClassNotFoundException ex) {
                Logger.getLogger(DecryptForm.class.getName()).log(Level.SEVERE, null, ex);
            }
            JOptionPane.showMessageDialog(null, "RSA Decryption Complete");
        }
        FileOutputStream fop = null;
    File file;   
    try {   
        file = new File("c:\\JavaFiles\\dfile.txt");            
        fop = new FileOutputStream(file);           
// if file doesnt exists, then create it
 
        if (!file.exists()) {               
            file.createNewFile();           
        }
 // get the content in bytes                    
        byte[] contentInBytes = pText.getBytes();           
        fop.write(contentInBytes);          
        fop.flush();            
        fop.close(); 
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fop != null) {
                fop.close();            
                        }       
                } catch (IOException e) {           
                    e.printStackTrace();        
                } 
    }//RSA Ends
    }

解密方法

  public static String decrypt(byte[] text, PrivateKey key) {
    byte[] dectyptedText = null;
    try {
      // get an RSA cipher object and print the provider
      final Cipher cipher = Cipher.getInstance(ALGORITHM);
      // decrypt the text using the private key
      cipher.init(Cipher.DECRYPT_MODE, key);
      System.out.println("Text in Decrypt"+ text);
      dectyptedText = cipher.doFinal(text);
 
    } catch (Exception ex) {
      ex.printStackTrace();
      System.out.println("Blah blah: "+ex);
    }
    System.out.println("Decrypter Text: " +dectyptedText);
    return new String(dectyptedText);
  }

代码在上面。

每次抛出异常" javax.crypto.IllegalBlockSizeException:数据不得超过128个字节"在解密方法中。因此,它返回空值。

0 个答案:

没有答案
相关问题